java中封裝JDBC工具類的實例分析
對于能夠重復(fù)使用的代碼,我們最好的方法是對它們進行封裝,然后在下次使用的使用就可以直接調(diào)用了。本篇所要提到的是JDBC工具類,相信大家在學(xué)習(xí)java時都接觸過。那么對于封裝它的方法,本篇先對工具類進行簡單的說明,列出有關(guān)的封裝步驟,然后帶來相關(guān)的實例。
1、說明在java開發(fā)過程中,代碼中時常用到一些Scanner、Random一樣的類,他們是鍵盤錄入,生成隨機數(shù)的類,像一個工具一樣,在java中被稱為工具類。
2、步驟封裝JDBC工具類
加入獲取數(shù)據(jù)庫連接對象的方法
加入釋放連接的方法
3、實例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ù)庫連接的方法 * @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ù)庫連接對象 * @param stmt 執(zhí)行SQL語句的對象 * @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ù)庫連接對象 * @param stmt 執(zhí)行SQL語句的對象 * @param resultSet 執(zhí)行SQL語句的返回的結(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();}}}
實例擴展:
public class JDBCUtil { //連接對象 private Connection connection = null; //數(shù)據(jù)庫操作對象 private PreparedStatement ps = null; //數(shù)據(jù)庫連接地址 private static String url = 'jdbc:mysql://localhost:3306/'; //用戶名 private static String user = 'root'; //密碼 private static String password = '123456'; //靜態(tài)代碼塊 注冊驅(qū)動 //類加載的時候,只執(zhí)行一次 static{ try { Class.forName('com.mysql.jdbc.Driver'); } catch (ClassNotFoundException e) { e.printStackTrace(); } } //獲取連接對象 public Connection getConnection(){ //Connection conn = null; try { connection = DriverManager.getConnection(url,user,password); } catch (SQLException e) { e.printStackTrace(); System.out.println('數(shù)據(jù)庫連接失敗....'); } System.out.println('數(shù)據(jù)庫連接成功...'); return connection; } //獲取數(shù)據(jù)庫操作對象 public PreparedStatement createPreparedStatement(String sql){ connection = getConnection(); try { ps = connection.prepareStatement(sql); } catch (SQLException e) { e.printStackTrace(); } return ps; } //釋放資源 public void close(){ //釋放連接對象 if (connection != null) { try {connection.close(); } catch (SQLException e) {e.printStackTrace(); } } //釋放數(shù)據(jù)庫操作對象 if (ps != null) { try {ps.close(); } catch (SQLException e) {e.printStackTrace(); } } System.out.println('釋放資源成功...'); } //方法的重載 public void close(ResultSet reuslt){ // 調(diào)用釋放資源的方法 close(); // 釋放查詢結(jié)果集對象 if (reuslt != null) { try {reuslt.close(); } catch (SQLException e) {e.printStackTrace(); } } }}
到此這篇關(guān)于java中封裝JDBC工具類的實例分析的文章就介紹到這了,更多相關(guān)java中如何封裝JDBC工具類內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
