單例模式在JDBC資料庫連線操作裡的應用
阿新 • • 發佈:2019-01-10
設計模式之單例模式一般應用在在資料庫操作裡,資料庫操作就要經常建立例項,然後進行資料庫操作,所有就可以
將資料庫操作的方法,進行封裝,然後採用單例模式進行設計,然後採用單例模式之後,就可以節約系統資源,對於
一些需要頻繁建立和銷燬的物件單例模式無疑可以提高系統的效能
先編寫資料庫配置檔案config.properties
host=localhost
port=3306
database=tourism_system
username=root
password=111
DBHelpUtil.java:
/** * * 資料庫連線的類,配置資訊儲存在config.properties裡 * */ public class DBHelperUtil { //靜態成員變數,支援單態模式 private static DBHelperUtil manager = null; //配置資原始檔 private PropertyResourceBundle bundle; //JDBC驅動 private static String jdbcDriver = null; //主機 private String host = ""; //資料庫埠 private String port = ""; //資料庫名稱 private String database = ""; //資料庫使用者名稱 private String username = ""; //資料庫密碼 private String password =""; //資料庫連線字串 private String connStr = ""; //連線物件 private Connection conn = null; //PrepareStatement物件 private PreparedStatement pstm = null; //CallableStatement物件 private CallableStatement cstm = null; /** * 私有構造物件,不可以例項化 * @throws IOException */ public DBHelperUtil() throws IOException{ bundle = new PropertyResourceBundle(DBHelperUtil.class.getResourceAsStream("config.properties")); this.host = getString("host"); this.database = getString("database"); this.port = getString("port"); this.username = getString("username"); this.password = getString("password"); jdbcDriver = "com.mysql.jdbc.Driver"; //資料庫連線的url,設定了編碼為UTF-8 connStr = "jdbc:mysql://"+host+":"+port+"/"+database+"?useUnicode=true&characterEncoding=UTF-8"; } /** * 讀取配置檔案中的值 * @param * key 配置檔案的key * @return * key對應的值 */ private String getString(String key){ return this.bundle.getString(key); } /** * 單態模式獲取例項 * * @return SqlManager物件 * @throws IOException * @throws ClassNotFoundException */ public static DBHelperUtil createInstance() throws IOException, ClassNotFoundException{ if (manager == null) { manager = new DBHelperUtil(); manager.initDB(); } return manager; } /** * 初始化連線引數,由指定的DBType生成 * * @throws ClassNotFoundException */ public void initDB() throws ClassNotFoundException{ Class.forName(jdbcDriver); } /** * 連線資料庫 * @throws SQLException */ public void connectDB() throws SQLException{ conn = DriverManager.getConnection(connStr,username,password); conn.setAutoCommit(false);// 設定自動提交為false } /** * 關閉資料庫,釋放記憶體 * @throws SQLException */ public void close() throws SQLException { if (pstm != null) { pstm.close(); } if (cstm != null) { cstm.close(); } if (conn != null) { conn.close(); } } /** * 設定PrepareStatement物件中Sql語句中的引數 * @param sql * sql語句 * @param params * 引數列表 * @throws SQLException */ @SuppressWarnings("unused") private void setPrepareStatementParams(String sql, Object[] params) throws SQLException{ pstm = conn.prepareStatement(sql); // 獲取物件 if (params != null) { for (int i = 0; i < params.length; i++) // 遍歷引數列表填充引數 { pstm.setObject(i + 1, params[i]); } } } /** * 執行查詢 * * @param sql * sql語句 * @param params * 引數列表 * @return 返回ResultSet型別的查詢結果 * @throws SQLException */ public ResultSet executeQuery(String sql, Object[] params) throws SQLException{ // 執行查詢資料庫介面 ResultSet rs = null; manager.setPrepareStatementParams(sql, params); // 填充引數 rs = pstm.executeQuery(); // 執行查詢操作 return rs; } /** * 更新資料庫操作 * * @param sql * sql語句 * @param params * 引數列表 * @return 執行操作的結果 * @throws SQLException */ public boolean executeUpdate(String sql, Object[] params)throws SQLException { // 執行無返回資料的資料查詢,返回值是被改變的書庫的資料庫項數 boolean result = false; manager.setPrepareStatementParams(sql, params); // 填充引數 pstm.executeUpdate(); // 執行更新 manager.commitChange(); result = true; return result; } /** * 提交資訊到資料庫 * @throws SQLException */ private void commitChange() throws SQLException { conn.commit(); } }
呼叫工具類:
先建立例項,createInstance,然後連線資料庫,呼叫方法就可以