java中封裝JDBC工具類的實(shí)例分析
對(duì)于能夠重復(fù)使用的代碼,我們最好的方法是對(duì)它們進(jìn)行封裝,然后在下次使用的使用就可以直接調(diào)用了。本篇所要提到的是JDBC工具類,相信大家在學(xué)習(xí)java時(shí)都接觸過(guò)。那么對(duì)于封裝它的方法,本篇先對(duì)工具類進(jìn)行簡(jiǎn)單的說(shuō)明,列出有關(guān)的封裝步驟,然后帶來(lái)相關(guān)的實(shí)例。
1、說(shuō)明在java開發(fā)過(guò)程中,代碼中時(shí)常用到一些Scanner、Random一樣的類,他們是鍵盤錄入,生成隨機(jī)數(shù)的類,像一個(gè)工具一樣,在java中被稱為工具類。
2、步驟封裝JDBC工具類
加入獲取數(shù)據(jù)庫(kù)連接對(duì)象的方法
加入釋放連接的方法
3、實(shí)例package com.qianfeng.util;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;/** * JDBC工具類 * 有獲取連接的方法 * @author dushine */public class JDBCUtil {/** * 獲取數(shù)據(jù)庫(kù)連接的方法 * @return Connection conn * @throws SQLException */public static Connection getConnection() throws SQLException {String url = 'jdbc:mysql://localhost:3306/class?useSSL=false';String user = 'root';String password = 'root';Connection conn = DriverManager.getConnection(url,user,password);return conn;}/** * 釋放連接的方法 * @param conn * @throws SQLException */public static void releaseSourse(Connection conn) throws SQLException {if (conn != null) {conn.close();}}/** * 釋放連接的方法 * @param conn 數(shù)據(jù)庫(kù)連接對(duì)象 * @param stmt 執(zhí)行SQL語(yǔ)句的對(duì)象 * @throws SQLException */public static void releaseSourse(Connection conn,Statement stmt) throws SQLException {if (stmt != null) {stmt.close();}if (conn != null) {conn.close();}}/** * 釋放連接的方法 * @param conn 數(shù)據(jù)庫(kù)連接對(duì)象 * @param stmt 執(zhí)行SQL語(yǔ)句的對(duì)象 * @param resultSet 執(zhí)行SQL語(yǔ)句的返回的結(jié)果集 * @throws SQLException */public static void releaseSourse(Connection conn,Statement stmt,ResultSet resultSet) throws SQLException {if (resultSet != null) {resultSet.close();}if (stmt != null) {stmt.close();}if (conn != null) {conn.close();}}}
實(shí)例擴(kuò)展:
public class JDBCUtil { //連接對(duì)象 private Connection connection = null; //數(shù)據(jù)庫(kù)操作對(duì)象 private PreparedStatement ps = null; //數(shù)據(jù)庫(kù)連接地址 private static String url = 'jdbc:mysql://localhost:3306/'; //用戶名 private static String user = 'root'; //密碼 private static String password = '123456'; //靜態(tài)代碼塊 注冊(cè)驅(qū)動(dòng) //類加載的時(shí)候,只執(zhí)行一次 static{ try { Class.forName('com.mysql.jdbc.Driver'); } catch (ClassNotFoundException e) { e.printStackTrace(); } } //獲取連接對(duì)象 public Connection getConnection(){ //Connection conn = null; try { connection = DriverManager.getConnection(url,user,password); } catch (SQLException e) { e.printStackTrace(); System.out.println('數(shù)據(jù)庫(kù)連接失敗....'); } System.out.println('數(shù)據(jù)庫(kù)連接成功...'); return connection; } //獲取數(shù)據(jù)庫(kù)操作對(duì)象 public PreparedStatement createPreparedStatement(String sql){ connection = getConnection(); try { ps = connection.prepareStatement(sql); } catch (SQLException e) { e.printStackTrace(); } return ps; } //釋放資源 public void close(){ //釋放連接對(duì)象 if (connection != null) { try {connection.close(); } catch (SQLException e) {e.printStackTrace(); } } //釋放數(shù)據(jù)庫(kù)操作對(duì)象 if (ps != null) { try {ps.close(); } catch (SQLException e) {e.printStackTrace(); } } System.out.println('釋放資源成功...'); } //方法的重載 public void close(ResultSet reuslt){ // 調(diào)用釋放資源的方法 close(); // 釋放查詢結(jié)果集對(duì)象 if (reuslt != null) { try {reuslt.close(); } catch (SQLException e) {e.printStackTrace(); } } }}
到此這篇關(guān)于java中封裝JDBC工具類的實(shí)例分析的文章就介紹到這了,更多相關(guān)java中如何封裝JDBC工具類內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. javascript xml xsl取值及數(shù)據(jù)修改第1/2頁(yè)2. python opencv 實(shí)現(xiàn)讀取、顯示、寫入圖像的方法3. JavaScript Tab菜單實(shí)現(xiàn)過(guò)程解析4. jsp+mysql實(shí)現(xiàn)網(wǎng)頁(yè)的分頁(yè)查詢5. ASP.NET MVC通過(guò)勾選checkbox更改select的內(nèi)容6. 存儲(chǔ)于xml中需要的HTML轉(zhuǎn)義代碼7. 使用AJAX(包含正則表達(dá)式)驗(yàn)證用戶登錄的步驟8. Python使用oslo.vmware管理ESXI虛擬機(jī)的示例參考9. Android實(shí)現(xiàn)圖片自動(dòng)切換功能(實(shí)例代碼詳解)10. ThinkPHP5 通過(guò)ajax插入圖片并實(shí)時(shí)顯示(完整代碼)
