免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
JAVA連接、操作數(shù)據(jù)庫(kù)的DBHelper
  1 import java.sql.Connection;
  2 import java.sql.DriverManager;
  3 import java.sql.PreparedStatement;
  4 import java.sql.ResultSet;
  5 import java.sql.Statement;
  6 
  7 /**
  8  * 數(shù)據(jù)庫(kù)工具類,負(fù)責(zé)完成打開、關(guān)閉數(shù)據(jù)庫(kù),執(zhí)行查詢或更新
  9  * @author MKing
 10  *
 11  */
 12 public class DbHelper {
 13     /**
 14      * 數(shù)據(jù)庫(kù)URL
 15      */
 16     private static final String URL = "jdbc:mysql://localhost:3306/bookstore";
 17     /**
 18      * 登錄用戶名
 19      */
 20     private static final String USER = "root";
 21     /**
 22      * 登錄密碼
 23      */
 24     private static final String PASSWORD = "12345";
 25     
 26     private static Connection connection = null;
 27     private static Statement statement = null;
 28 
 29     private static DbHelper helper = null;
 30 
 31     static {
 32         try {
 33             Class.forName("com.mysql.jdbc.Driver");
 34         } catch (ClassNotFoundException e) {
 35             e.printStackTrace();
 36         }
 37     }
 38 
 39     private DbHelper() throws Exception {
 40         connection = DriverManager.getConnection(URL, USER, PASSWORD);
 41         statement = connection.createStatement();
 42     }
 43 
 44     /**
 45      * 返回單例模式的數(shù)據(jù)庫(kù)輔助對(duì)象
 46      * 
 47      * @return
 48      * @throws Exception 
 49      */
 50     public static DbHelper getDbHelper() throws Exception {
 51         if (helper == null || connection == null || connection.isClosed())
 52             helper = new DbHelper();
 53         return helper;
 54     }
 55 
 56     /**
 57      * 執(zhí)行查詢
 58      * @param sql 要執(zhí)行的SQL語(yǔ)句
 59      * @return  查詢的結(jié)果集對(duì)象
 60      * @throws Exception
 61      */
 62     public ResultSet executeQuery(String sql) throws Exception {
 63         if (statement != null) {
 64             return statement.executeQuery(sql);
 65         }
 66 
 67         throw new Exception("數(shù)據(jù)庫(kù)未正常連接");
 68     }
 69 
 70     /**
 71      * 執(zhí)行查詢
 72      * @param sql  要執(zhí)行的帶參數(shù)的SQL語(yǔ)句
 73      * @param args  SQL語(yǔ)句中的參數(shù)值
 74      * @return  查詢的結(jié)果集對(duì)象
 75      * @throws Exception
 76      */
 77     public ResultSet executeQuery(String sql, Object...args) throws Exception {
 78         if (connection == null || connection.isClosed()) {
 79             DbHelper.close();
 80             throw new Exception("數(shù)據(jù)庫(kù)未正常連接");
 81         }
 82         PreparedStatement ps = connection.prepareStatement(sql);
 83         int index = 1;
 84         for (Object arg : args) {
 85             ps.setObject(index, arg);
 86             index++;
 87         }
 88         
 89         return ps.executeQuery();
 90     }
 91     
 92     /**
 93      * 執(zhí)行更新
 94      * @param sql  要執(zhí)行的SQL語(yǔ)句
 95      * @return  受影響的記錄條數(shù)
 96      * @throws Exception
 97      */
 98     public int executeUpdate(String sql) throws Exception {
 99         if (statement != null) {
100             return statement.executeUpdate(sql);
101         }
102         throw new Exception("數(shù)據(jù)庫(kù)未正常連接");
103     }
104     
105     /**
106      * 執(zhí)行更新
107      * @param sql  要執(zhí)行的SQL語(yǔ)句
108      * @param args  SQL語(yǔ)句中的參數(shù)
109      * @return  受影響的記錄條數(shù)
110      * @throws Exception
111      */
112     public int executeUpdate(String sql, Object...args) throws Exception {
113         if (connection == null || connection.isClosed()) {
114             DbHelper.close();
115             throw new Exception("數(shù)據(jù)庫(kù)未正常連接");
116         }
117         PreparedStatement ps = connection.prepareStatement(sql);
118         int index = 1;
119         for (Object arg : args) {
120             ps.setObject(index, arg);
121             index++;
122         }
123         return ps.executeUpdate();
124     }
125     
126     /**
127      * 獲取預(yù)編譯的語(yǔ)句對(duì)象
128      * @param sql  預(yù)編譯的語(yǔ)句
129      * @return  預(yù)編譯的語(yǔ)句對(duì)象
130      * @throws Exception
131      */
132     public PreparedStatement prepareStatement(String sql) throws Exception {
133         return connection.prepareStatement(sql);
134     }
135     
136     /**
137      * 關(guān)閉對(duì)象,同時(shí)將關(guān)閉連接
138      */
139     public static void close() {
140         try {
141             if (statement != null)
142                 statement.close();
143             if (connection != null) 
144                 connection.close();
145         } catch (Exception e) {
146             e.printStackTrace();
147         } finally {
148             helper = null;
149         }
150     }
151 }
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
自己做數(shù)據(jù)庫(kù)連接池(c3p0)
java連接Oracle數(shù)據(jù)庫(kù)的工具類
編寫一個(gè)代碼以顯示這些詳細(xì)信息。
微信公眾平臺(tái)開發(fā)(數(shù)據(jù)庫(kù)連接)
四種連接數(shù)據(jù)庫(kù)的方法(DriverManager、DataSource子類、DBCP、c3...
oracle Blob 文件讀寫
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服