国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

java中封裝JDBC工具類(lèi)的實(shí)例分析

瀏覽:76日期:2022-08-16 08:36:08

對(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)!

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 华蓥市| 武邑县| 涟源市| 于田县| 巴林左旗| 固安县| 隆回县| 柳江县| 德阳市| 姜堰市| 房山区| 石首市| 兰溪市| 大方县| 新泰市| 扎囊县| 巩义市| 仁怀市| 句容市| 三门县| 禹城市| 潜江市| 上虞市| 双柏县| 建湖县| 平舆县| 北京市| 亚东县| 龙里县| 庆城县| 齐齐哈尔市| 大同县| 临西县| 台北市| 紫阳县| 公安县| 博乐市| 义乌市| 宾阳县| 嵩明县| 襄垣县|