1. 程式人生 > >java(JDBC連線資料庫)[完整版封裝]

java(JDBC連線資料庫)[完整版封裝]

  1. import java.sql.CallableStatement;  
  2. import java.sql.Connection;  
  3. import java.sql.DriverManager;  
  4. import java.sql.PreparedStatement;  
  5. import java.sql.ResultSet;  
  6. import java.sql.ResultSetMetaData;  
  7. import java.sql.SQLException;  
  8. import java.util.ArrayList;  
  9. import java.util.HashMap;  
  10. import java.util.List;  
  11. import java.util.Map;  
  12. /** 
  13.  * 資料庫連線類 
  14.  * 說明:封裝了 無參,有參,儲存過程的呼叫 
  15.  * @author iflytek 
  16.  * 
  17.  */
  18. publicclass ConnectionDB {  
  19.     /** 
  20.      * 資料庫驅動類名稱 
  21.      */
  22.     privatestaticfinal String DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";  
  23.     /** 
  24.      * 連線字串
     
  25.      */
  26.     privatestaticfinal String URLSTR = "jdbc:sqlserver://localhost:1433; databaseName=Northwind";  
  27.     /** 
  28.      * 使用者名稱 
  29.      */
  30.     privatestaticfinal String USERNAME = "sa";  
  31.     /** 
  32.      * 密碼 
  33.      */
  34.     privatestaticfinal String USERPASSWORD = "111111";  
  35.     /** 
  36.      * 建立資料庫連線物件
     
  37.      */
  38.     private Connection connnection = null;  
  39.     /** 
  40.      * 建立PreparedStatement物件 
  41.      */
  42.     private PreparedStatement preparedStatement = null;  
  43.     /** 
  44.      * 建立CallableStatement物件 
  45.      */
  46.     private CallableStatement callableStatement = null;  
  47.     /** 
  48.      * 建立結果集物件 
  49.      */
  50.     private ResultSet resultSet = null;  
  51.     static {  
  52.         try {  
  53.             // 載入資料庫驅動程式
  54.             Class.forName(DRIVER);  
  55.         } catch (ClassNotFoundException e) {  
  56.             System.out.println("載入驅動錯誤");  
  57.             System.out.println(e.getMessage());  
  58.         }  
  59.     }  
  60.     /** 
  61.      * 建立資料庫連線 
  62.      * @return 資料庫連線 
  63.      */
  64.     public Connection getConnection() {  
  65.         try {  
  66.             // 獲取連線
  67.             connnection = DriverManager.getConnection(URLSTR, USERNAME,  
  68.                     USERPASSWORD);  
  69.         } catch (SQLException e) {  
  70.             System.out.println(e.getMessage());  
  71.         }  
  72.         return connnection;  
  73.     }  
  74.     /** 
  75.      * insert update delete SQL語句的執行的統一方法 
  76.      * @param sql SQL語句 
  77.      * @param params 引數陣列,若沒有引數則為null 
  78.      * @return 受影響的行數 
  79.      */
  80.     publicint executeUpdate(String sql, Object[] params) {  
  81.         // 受影響的行數
  82.         int affectedLine = 0;  
  83.         try {  
  84.             // 獲得連線
  85.             connnection = this.getConnection();  
  86.             // 呼叫SQL 
  87.             preparedStatement = connnection.prepareStatement(sql);  
  88.             // 引數賦值
  89.             if (params != null) {  
  90.                 for (int i = 0; i < params.length; i++) {  
  91.                     preparedStatement.setObject(i + 1, params[i]);  
  92.                 }  
  93.             }  
  94.             // 執行
  95.             affectedLine = preparedStatement.executeUpdate();  
  96.         } catch (SQLException e) {  
  97.             System.out.println(e.getMessage());  
  98.         } finally {  
  99.             // 釋放資源
  100.             closeAll();  
  101.         }  
  102.         return affectedLine;  
  103.     }  
  104.     /** 
  105.      * SQL 查詢將查詢結果直接放入ResultSet中 
  106.      * @param sql SQL語句 
  107.      * @param params 引數陣列,若沒有引數則為null 
  108.      * @return 結果集 
  109.      */
  110.     private ResultSet executeQueryRS(String sql, Object[] params) {  
  111.         try {  
  112.             // 獲得連線
  113.             connnection = this.getConnection();  
  114.             // 呼叫SQL
  115.             preparedStatement = connnection.prepareStatement(sql);  
  116.             // 引數賦值
  117.             if (params != null) {  
  118.                 for (int i = 0; i < params.length; i++) {  
  119.                     preparedStatement.setObject(i + 1, params[i]);  
  120.                 }  
  121.             }  
  122.             // 執行
  123.             resultSet = preparedStatement.executeQuery();  
  124.         } catch (SQLException e) {  
  125.             System.out.println(e.getMessage());  
  126.         }  
  127.         return resultSet;  
  128.     }  
  129.     /** 
  130.      * 獲取結果集,並將結果放在List中 
  131.      *  
  132.      * @param sql 
  133.      *            SQL語句 
  134.      * @return List 
  135.      *                       結果集 
  136.      */
  137.     public List<Object> excuteQuery(String sql, Object[] params) {  
  138.         // 執行SQL獲得結果集
  139.         ResultSet rs = executeQueryRS(sql, params);  
  140.         // 建立ResultSetMetaData物件
  141.         ResultSetMetaData rsmd = null;  
  142.         // 結果集列數
  143.