java中封裝JDBC工具類(lèi)的實(shí)例分析
對(duì)于能夠重復(fù)使用的代碼,我們最好的方法是對(duì)它們進(jìn)行封裝,然后在下次使用的使用就可以直接調(diào)用了。本篇所要提到的是JDBC工具類(lèi),相信大家在學(xué)習(xí)java時(shí)都接觸過(guò)。那么對(duì)于封裝它的方法,本篇先對(duì)工具類(lèi)進(jìn)行簡(jiǎn)單的說(shuō)明,列出有關(guān)的封裝步驟,然后帶來(lái)相關(guān)的實(shí)例。
1、說(shuō)明在java開(kāi)發(fā)過(guò)程中,代碼中時(shí)常用到一些Scanner、Random一樣的類(lèi),他們是鍵盤(pán)錄入,生成隨機(jī)數(shù)的類(lèi),像一個(gè)工具一樣,在java中被稱(chēng)為工具類(lèi)。
2、步驟封裝JDBC工具類(lèi)
加入獲取數(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工具類(lèi) * 有獲取連接的方法 * @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/'; //用戶(hù)名 private static String user = 'root'; //密碼 private static String password = '123456'; //靜態(tài)代碼塊 注冊(cè)驅(qū)動(dòng) //類(lèi)加載的時(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(); // 釋放查詢(xún)結(jié)果集對(duì)象 if (reuslt != null) { try {reuslt.close(); } catch (SQLException e) {e.printStackTrace(); } } }}
到此這篇關(guān)于java中封裝JDBC工具類(lèi)的實(shí)例分析的文章就介紹到這了,更多相關(guān)java中如何封裝JDBC工具類(lèi)內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. jsp實(shí)現(xiàn)登錄界面2. .NET使用YARP通過(guò)編碼方式配置域名轉(zhuǎn)發(fā)實(shí)現(xiàn)反向代理3. 淺議PHP程序開(kāi)發(fā)中的模板選擇4. .NET 8新預(yù)覽版使用 Blazor 組件進(jìn)行服務(wù)器端呈現(xiàn)(項(xiàng)目體驗(yàn))5. JSP實(shí)現(xiàn)簡(jiǎn)單人事管理系統(tǒng)6. 關(guān)于JSP用戶(hù)登錄連接數(shù)據(jù)庫(kù)詳情7. html form表單基礎(chǔ)入門(mén)案例講解8. 詳解盒子端CSS動(dòng)畫(huà)性能提升9. 不要在HTML中濫用div10. CSS可以做的幾個(gè)令你嘆為觀止的實(shí)例分享
