最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

封裝了一個Java數(shù)據(jù)庫訪問管理類

 更新時間:2009年02月22日 02:28:54   作者:  
剛剛試著用JDBC,仿著原來C#的寫法寫了這段代碼,自己覺得還是挺粗糙的,還煩請路過的朋友推薦一個寫得較好較完整的相關(guān)例程以便學(xué)習(xí)。謝謝!
復(fù)制代碼 代碼如下:

package com.groundhog.codingmouse;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* 數(shù)據(jù)庫管理類
* @author CodingMouse
* 2009.2.20
*/
public final class DBManager {
/**
* 數(shù)據(jù)庫連接對象
*/
private Connection dbConnection = null;
/**
* 數(shù)據(jù)庫命令執(zhí)行對象
*/
private PreparedStatement preStatement = null;
/**
* 結(jié)果集對象
*/
private ResultSet rsSet = null;
/**
* 數(shù)據(jù)庫驅(qū)動版本號
*/
private static String driverVersion = null;
/**
* 數(shù)據(jù)庫服務(wù)器登錄用戶名和密碼字符串常量(默認(rèn)值均
為'sa')
*/
private static String databaseUser = "sa";
private static String databasePassword = "sa";
/**
* 數(shù)據(jù)庫驅(qū)動完整類名字符串常量
*/
private static final String
DRIVER_CLASS_SQLSERVER2000 =
"com.microsoft.jdbc.sqlserver.SQLServerDriver"; // SQL
Server 2000 直連
private static final String
DRIVER_CLASS_SQLSERVER2005 =
"com.microsoft.sqlserver.jdbc.SQLServerDriver"; // SQL
Server 2005 直連
private static final String
DRIVER_CLASS_BRIDGECONNECT = "sun.jdbc.odbc.JdbcOdbcDriver";
// ODBC 橋連
/**
* 數(shù)據(jù)庫連接字符串常量
*/
private static final String
DATABASE_URL_SQLSERVER2000 =
"jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=stuD
B"; // SQL Server 2000 直連
private static final String
DATABASE_URL_SQLSERVER2005 =
"jdbc:sqlserver://127.0.0.1:1433;DatabaseName=stuDB";
// SQL Server 2005 直連
private static final String
DATABASE_URL_BRIDGECONNECT = "jdbc:odbc:stuDBSource";
// ODBC 橋連
/**
* 定義類自身的實(shí)例靜態(tài)變量(作用于單例[件]模式的應(yīng)用)
*/
private static DBManager connectionManager = null;
/**
* 私有化默認(rèn)構(gòu)造(作用于單例[件]模式的應(yīng)用,防止類被直
接使用new關(guān)鍵字實(shí)例化)
*/
private DBManager() {
super();
}
/**
* 獲取數(shù)據(jù)庫連接管理類實(shí)例的方法(單例[件]模式的應(yīng)用)
* @param version 數(shù)據(jù)庫驅(qū)動版本號,取值:(version =
2000 | version = 2005 | version = odbc)
* @param user 數(shù)據(jù)庫服務(wù)器登錄用戶名
* @param password 數(shù)據(jù)庫服務(wù)器登錄密碼
* @return 數(shù)據(jù)庫連接管理對象
* @throws Exception 參數(shù)錯誤異常
*/
public static DBManager getInstance(
String version,
String user,
String password)
throws Exception {
if (!(version == "2000" || version == "2005"
|| version == "odbc")) {
throw new Exception("數(shù)據(jù)庫驅(qū)動版本號
不正確,取值只能是“2000/2005/odbc”!");
}
// 保存數(shù)據(jù)庫驅(qū)動版本號
driverVersion = version;
if (user == null || user.equals("")) {
throw new Exception("數(shù)據(jù)庫服務(wù)器登錄
用戶名不能為空!");
}
// 保存數(shù)據(jù)庫服務(wù)器登錄用戶名和密碼
databaseUser = user;
databasePassword = password;
// 應(yīng)用單例[件]模式確保類本身只有一個實(shí)例
if (connectionManager == null) {
connectionManager = new DBManager();
}
// 返回類本身的實(shí)例
return connectionManager;
}
/**
* 獲取數(shù)據(jù)庫連接的方法
* @return 數(shù)據(jù)庫連接對象
*/
private Connection getConnection() {
try {
Class.forName(
driverVersion ==
"2000"
?
DRIVER_CLASS_SQLSERVER2000
: (driverVersion ==
"2005"
?
DRIVER_CLASS_SQLSERVER2005
:
DRIVER_CLASS_BRIDGECONNECT));
this.dbConnection =
DriverManager.getConnection(
driverVersion ==
"2000"
?
DATABASE_URL_SQLSERVER2000
: (driverVersion ==
"2005"
?
DATABASE_URL_SQLSERVER2005
:
DATABASE_URL_BRIDGECONNECT),
databaseUser,
databasePassword);
} catch (ClassNotFoundException ex) {
System.err.println("未找到SQL Server
" + driverVersion + "數(shù)據(jù)庫驅(qū)動類:" + ex.getMessage());
// 在控制臺輸出異常堆棧信息
// ex.printStackTrace();
} catch (Exception ex) {
System.err.println("獲取數(shù)據(jù)庫連接錯
誤:" + ex.getMessage());
// 在控制臺輸出異常堆棧信息
// ex.printStackTrace();
}
// 返回?cái)?shù)據(jù)庫連接對象
return this.dbConnection;
}
/**
* 獲取數(shù)據(jù)庫命令執(zhí)行對象的方法
* @param sql 要執(zhí)行的SQL命令拼裝語句字符串
* @return 數(shù)據(jù)庫命令執(zhí)行對象
*/
private PreparedStatement getPreparedStatement
(String sql) {
try {
// 根據(jù)獲取的數(shù)據(jù)庫連接對象創(chuàng)建數(shù)據(jù)庫
命令執(zhí)行對象
this.preStatement = getConnection
().prepareStatement(sql);
} catch (Exception ex) {
System.err.println("獲取數(shù)據(jù)庫命令執(zhí)
行對象錯誤:" + ex.getMessage());
// 在控制臺輸出異常堆棧信息
// ex.printStackTrace();
}
// 返回?cái)?shù)據(jù)庫命令執(zhí)行對象
return this.preStatement;
}
/**
* 執(zhí)行更新語句(Insert|Update|Delete)
* @param sql 要執(zhí)行的SQL命令拼裝語句字符串
* @return 受影響的行數(shù)
*/
public int executeUpdate(String sql){
try {
// 置空結(jié)果集對象的原有內(nèi)容
this.rsSet = null;
// 執(zhí)行語句并返回受影響行數(shù)
return this.getPreparedStatement
(sql).executeUpdate();
} catch (SQLException e) {
System.err.println("更新數(shù)據(jù)錯誤:" +
e.getMessage());
return 0;
}finally{
// 關(guān)閉數(shù)據(jù)庫連接資源
closeDBResource();
}
}
/**
* 執(zhí)行查詢語句(Select)
* @param sql 要執(zhí)行的SQL命令拼裝語句字符串
* @return 查詢后的結(jié)果集對象
*/
public ResultSet executeQuery(String sql){
try {
// 置空結(jié)果集對象的原有內(nèi)容
this.rsSet = null;
// 執(zhí)行sql語句獲得結(jié)果集
this.rsSet =
this.getPreparedStatement(sql).executeQuery();
} catch (SQLException e) {
System.err.println("查詢數(shù)據(jù)錯誤:" +
e.getMessage());
}
// 返回結(jié)果集對象
return this.rsSet;
}
/**
* 獲取執(zhí)行指定sql語句后的返回結(jié)果集的記錄條數(shù)
* @param sql 要執(zhí)行的SQL命令拼裝語句字符串
* @return 查詢結(jié)果得到的記錄條數(shù)
*/
public int getResultSetCount(String sql) {
// 保存得到指定的sql語句執(zhí)行后返回記錄行數(shù)的計(jì)數(shù)器變量
int count = 0;
try {
// 置空結(jié)果集對象的原有內(nèi)容
this.rsSet = null;
// 執(zhí)行sql語句獲得結(jié)果集
this.rsSet = this.getPreparedStatement
(sql).executeQuery();
// 遍歷結(jié)果集并累加計(jì)數(shù)器
while (this.rsSet.next()) {
count++;
}
} catch (SQLException e) {
e.printStackTrace();
}
return count;
}
/**
* 關(guān)閉數(shù)據(jù)庫連接資源(包括結(jié)果集對象、命令執(zhí)行對象、連
接對象)
*/
public void closeDBResource() {
try {
closeResultSet();
closePreparedStatement();
closeConnection();
} catch (SQLException sqlEx) {
System.err.println(sqlEx.getMessage
());
// 在控制臺輸出異常堆棧信息
// sqlEx.printStackTrace();
}
}
/**
* 關(guān)閉結(jié)果集對象的方法
* @throws SQLException
*/
private void closeResultSet() throws SQLException {
try {
if (this.rsSet != null) {
this.rsSet.close();
this.rsSet = null;
}
} catch (SQLException sqlEx) {
throw new SQLException("關(guān)閉結(jié)果集對
象錯誤:" + sqlEx.getMessage());
// 在控制臺輸出異常堆棧信息
// sqlEx.printStackTrace();
}
}
/**
* 關(guān)閉數(shù)據(jù)庫命令執(zhí)行對象的方法
* @throws SQLException
*/
private void closePreparedStatement() throws
SQLException {
try {
if (this.preStatement != null) {
this.preStatement.close();
this.preStatement = null;
}
} catch (SQLException sqlEx) {
throw new SQLException("關(guān)閉數(shù)據(jù)庫命
令執(zhí)行對象錯誤:" + sqlEx.getMessage());
// 在控制臺輸出異常堆棧信息
// sqlEx.printStackTrace();
}
}
/**
* 關(guān)閉數(shù)據(jù)庫連接的方法
* @throws SQLException
*/
private void closeConnection() throws SQLException {
try {
if (this.dbConnection != null && (!
this.dbConnection.isClosed())) {
this.dbConnection.close();
}
} catch (SQLException sqlEx) {
throw new SQLException("關(guān)閉數(shù)據(jù)庫連
接錯誤:" + sqlEx.getMessage());
// 在控制臺輸出異常堆棧信息
// sqlEx.printStackTrace();
}
}
}

相關(guān)文章

  • springboot @JsonSerialize的使用講解

    springboot @JsonSerialize的使用講解

    這篇文章主要介紹了springboot @JsonSerialize的使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • java線程同步操作實(shí)例詳解

    java線程同步操作實(shí)例詳解

    這篇文章主要介紹了java線程同步操作,結(jié)合實(shí)例形式分析了Java線程同步與鎖機(jī)制相關(guān)原理、操作技巧與注意事項(xiàng),需要的朋友可以參考下
    2018-09-09
  • Java操作XML工具類XmlUtil詳解

    Java操作XML工具類XmlUtil詳解

    這篇文章主要為大家詳細(xì)介紹了Java操作XML工具類XmlUtil的使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-12-12
  • Java前端Layer.open.btn驗(yàn)證無效解決方法

    Java前端Layer.open.btn驗(yàn)證無效解決方法

    在本篇文章里我們給大家整理了一篇關(guān)于Java前端Layer.open.btn驗(yàn)證無效解決方法以及實(shí)例代碼,需要的朋友們可以參考學(xué)習(xí)下。
    2019-09-09
  • Java多種獲取項(xiàng)目路徑下的文件方式

    Java多種獲取項(xiàng)目路徑下的文件方式

    文章介紹了在Java項(xiàng)目中獲取resources文件夾下文件的InputStream的多種方法,包括使用類加載器、上下文類加載器、系統(tǒng)屬性和Paths類
    2024-12-12
  • java?環(huán)境配置(2023年詳細(xì)教程)

    java?環(huán)境配置(2023年詳細(xì)教程)

    這篇文章首先為了完善我的知識體系,今后一些軟件的安裝教程也可能會用到想寫一個更加詳細(xì)的,因?yàn)檫@并不僅僅是寫給?IT?行業(yè)的,其它行業(yè)可能也需要配置java環(huán)境
    2023-06-06
  • 最新評論

    康马县| 霍邱县| 武冈市| 闸北区| 中宁县| 恩平市| 敖汉旗| 邻水| 齐河县| 略阳县| 睢宁县| 东乌珠穆沁旗| 万荣县| 郎溪县| 沅陵县| 长顺县| 澜沧| 营口市| 高密市| 宝丰县| 惠东县| 巴塘县| 正宁县| 富蕴县| 米脂县| 吉安县| 衡东县| 肃宁县| 黄浦区| 龙南县| 晋州市| 水富县| 花莲县| 西昌市| 凤城市| 河东区| 永善县| 岑溪市| 肥东县| 江西省| 乌拉特前旗|