Java中Dao模式中Dao的資料庫操作(BaseDao的寫法)

來源:互聯網
上載者:User

標籤:str   turn   param   .sql   protected   not   ace   資料庫訪問   try   

Dao模式是Java面向設計時的一種模式。而Dao中的BaseDao無非是對資料進行CURD(增刪改查),下面是最常用的串連,增刪改查的方法。 

  1 package dao;  2   3 import java.sql.*;  4   5 /**  6  * 資料庫訪問類  7  */  8 public class BaseDao {  9     //資料庫驅動 10     private static String driver = "com.mysql.jdbc.Driver"; 11     //資料庫連接地址 12     private static String url = "jdbc:mysql://127.0.0.1:3306/flight?useSSL=False"; 13     //資料庫使用者名稱 14     private static String user = "root"; 15     //資料庫密碼 16     private static String password = "123456"; 17  18     //資料庫連接 19     protected Connection conn = null; 20     //statement對象 21     protected PreparedStatement pstmt = null; 22  23     /** 24      * 串連資料庫 25      * 26      * @return 資料庫連接 27      * @throws ClassNotFoundException 未找到驅動類異常 28      * @throws SQLException           資料庫異常 29      */ 30     protected Connection getConn() throws ClassNotFoundException, SQLException { 31         //載入驅動 32         Class.forName(driver); 33         //擷取串連 34         conn = DriverManager.getConnection(url, user, password); 35         return conn; 36     } 37  38     /** 39      * 資料庫查詢方法 40      * 41      * @param sql    先行編譯sql語句 42      * @param params 先行編譯參數 43      * @return 查詢結果集 44      * @throws SQLException 資料庫異常 45      */ 46     protected ResultSet executeQuerySQL(String sql, Object[] params) throws SQLException { 47         ResultSet rs = null; 48         try { 49             conn = getConn(); 50         } catch (ClassNotFoundException e) { 51             e.printStackTrace(); 52         } 53         pstmt = conn.prepareStatement(sql); 54         if (params != null) { 55             for (int i = 0; i < params.length; i++) { 56                 pstmt.setObject(i + 1, params[i]); 57             } 58         } 59         rs = pstmt.executeQuery(); 60         return rs; 61     } 62  63     /** 64      * 資料庫增刪改方法 65      * 66      * @param sql    先行編譯sql語句 67      * @param params 先行編譯參數 68      * @return 資料庫影響行數 69      * @throws SQLException 資料庫異常 70      */ 71     protected int executeUpdate(String sql, Object[] params) throws SQLException { 72         try { 73             conn = getConn(); 74         } catch (ClassNotFoundException e) { 75             e.printStackTrace(); 76         } 77         int num = 0; 78         pstmt = conn.prepareStatement(sql); 79         if (params != null) { 80             for (int i = 0; i < params.length; i++) { 81                 pstmt.setObject(i + 1, params[i]); 82             } 83         } 84         num = pstmt.executeUpdate(); 85  86         return num; 87     } 88  89     /** 90      * 關閉資料庫連接 91      * @param conn 資料庫連接 92      * @param pstmt  statement對象 93      * @param rs  ResultSet對象 94      */ 95     protected void closeALL(Connection conn, PreparedStatement pstmt, ResultSet rs) { 96         try { 97             if (rs != null) { 98                 rs.close(); 99             }100             if (pstmt != null) {101                 pstmt.close();102             }103             if (conn != null) {104                 conn.close();105             }106         } catch (SQLException e) {107             e.printStackTrace();108         }109     }110 }

 

 

Java中Dao模式中Dao的資料庫操作(BaseDao的寫法)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.