⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jdbcbean.java

📁 JAVA 开发的一个企业网站 JAVABEAN+JSP +ACCE
💻 JAVA
字号:
package mfkvit;

import java.sql.*;
import java.util.*;

import javax.swing.*;

public class JdbcBean extends JPanel {
    private Connection 	con 	= null;				//数据库连接
 private ResultSet 	rs   	= null;				//原始结果集
 private Statement     stmt      = null;
 //连接参数
 private final String        DBURL   = "jdbc:odbc:basic";
 private final String        DBUSER  ="";
 private final String        DBPASSWORD  ="";
 private ArrayList result = new ArrayList(); //存放结果集
 String[] colName = null;
 private int colLen = 0; //列数
 private String empty	= "";	//替换结果集中的空字符


 public JdbcBean() {
 }

 private void ini() throws SQLException {
   try {
     Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
     if ( (con == null) || con.isClosed())
       con = DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=D:/work/xjy/WebRoot/WEB-INF/basic.mdb");
     stmt = con.createStatement();
   }
   catch (SQLException e) {
     throw e;
   }catch(Exception e){
     System.out.println("load driver error");
   }

 }

 public ArrayList getRS() {
   return result;
 }


 /**
  * 执行数据查询,结果集以数组(每一行的内容)形式存放在ArrayList中
  *
  * @param sqlStatement String 查询的SQL语句
  * @retrurn rownum
  *
  */
 public int execQuery(String sqlStatement) throws SQLException {
   int recounter = 0;
   clearResult();
   try {
     ini();
     rs = stmt.executeQuery(sqlStatement);
     ResultSetMetaData rsmd = rs.getMetaData();
     String self = null;
     colLen = rsmd.getColumnCount() ;
     while (rs.next()) {
       String[] colValue = new String[colLen];
       for (int i = 0; i < colLen; i++) {
         colValue[i] = ( (self = rs.getString(i + 1 )) == null ?empty :
                        self.trim().length() == 0 ? empty : self.trim());
       }
       result.add(colValue);
       recounter++;
     }
     return recounter;
   }
   catch (SQLException e) {
     throw e;
   }
   finally {
     closeConn();
   }
 }

 /**
  * 执行数据查询,结果集以HashMap(每一行的内容)形式存放在ArrayList中
  *
  * @param sqlStatement String 	查询的SQL语句
  * @retrurn rownum
  */
 public int execQueryName(String sqlStatement) throws SQLException {
   int recounter = 0;
   clearResult();
   try {
     ini();
     rs = stmt.executeQuery(sqlStatement);
     ResultSetMetaData rsmd = rs.getMetaData();
     String self = null;
     colLen = rsmd.getColumnCount() ;
     colName = new String[colLen];
     for (int i = 0; i < colLen; i++) {
       colName[i] = rsmd.getColumnName(i + 1);
     }
     while (rs.next()) {
       HashMap colValue = new HashMap();
       for (int i = 0; i < colLen; i++) {
         colValue.put(colName[i],
                      (self = rs.getString(i + 1 )) == null ? empty :
                      self.trim().length() == 0 ? empty : self.trim());
       }
       result.add(colValue);
       recounter++;
     }
     return recounter;
   }
   catch (SQLException e) {
     throw e;
   }
   finally {
     closeConn();
   }
 }

 /**
  * 执行一条没有返回结果集的SQL
  *
  * @param sqlStatement String SQL脚本
  * @retrurn int 成功与否标志
  *
  */
 public int execUpdate(String sqlStatement) throws SQLException {
   try {
     ini();
     int recounter = stmt.executeUpdate(sqlStatement);
     return recounter;
   }
   catch (SQLException e) {
     throw e;
   }
   finally {
     closeConn();
   }
 }
 /**
  * 清空自定义的结果集
  */
 public void clearResult() {
   if (result.size() > 0)
     result.clear();
 }


 // Release the SQL statement resources
 private void closeConn() throws SQLException {
   try {
     if (con != null) {
       stmt.close();
       con.close();
       stmt = null;
       con = null;
     }
   }
   catch (SQLException e) {
     throw e;
   }
 }

    /**
     * execUpdate
     */
    public void execUpdate() {
    }


}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -