亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時(shí)間:2009年02月22日 02:28:54   作者:  
剛剛試著用JDBC,仿著原來(lái)C#的寫法寫了這段代碼,自己覺得還是挺粗糙的,還煩請(qǐng)路過的朋友推薦一個(gè)寫得較好較完整的相關(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ù)庫(kù)管理類
* @author CodingMouse
* 2009.2.20
*/
public final class DBManager {
/**
* 數(shù)據(jù)庫(kù)連接對(duì)象
*/
private Connection dbConnection = null;
/**
* 數(shù)據(jù)庫(kù)命令執(zhí)行對(duì)象
*/
private PreparedStatement preStatement = null;
/**
* 結(jié)果集對(duì)象
*/
private ResultSet rsSet = null;
/**
* 數(shù)據(jù)庫(kù)驅(qū)動(dòng)版本號(hào)
*/
private static String driverVersion = null;
/**
* 數(shù)據(jù)庫(kù)服務(wù)器登錄用戶名和密碼字符串常量(默認(rèn)值均
為'sa')
*/
private static String databaseUser = "sa";
private static String databasePassword = "sa";
/**
* 數(shù)據(jù)庫(kù)驅(qū)動(dòng)完整類名字符串常量
*/
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ù)庫(kù)連接字符串常量
*/
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ù)庫(kù)連接管理類實(shí)例的方法(單例[件]模式的應(yīng)用)
* @param version 數(shù)據(jù)庫(kù)驅(qū)動(dòng)版本號(hào),取值:(version =
2000 | version = 2005 | version = odbc)
* @param user 數(shù)據(jù)庫(kù)服務(wù)器登錄用戶名
* @param password 數(shù)據(jù)庫(kù)服務(wù)器登錄密碼
* @return 數(shù)據(jù)庫(kù)連接管理對(duì)象
* @throws Exception 參數(shù)錯(cuò)誤異常
*/
public static DBManager getInstance(
String version,
String user,
String password)
throws Exception {
if (!(version == "2000" || version == "2005"
|| version == "odbc")) {
throw new Exception("數(shù)據(jù)庫(kù)驅(qū)動(dòng)版本號(hào)
不正確,取值只能是“2000/2005/odbc”!");
}
// 保存數(shù)據(jù)庫(kù)驅(qū)動(dòng)版本號(hào)
driverVersion = version;
if (user == null || user.equals("")) {
throw new Exception("數(shù)據(jù)庫(kù)服務(wù)器登錄
用戶名不能為空!");
}
// 保存數(shù)據(jù)庫(kù)服務(wù)器登錄用戶名和密碼
databaseUser = user;
databasePassword = password;
// 應(yīng)用單例[件]模式確保類本身只有一個(gè)實(shí)例
if (connectionManager == null) {
connectionManager = new DBManager();
}
// 返回類本身的實(shí)例
return connectionManager;
}
/**
* 獲取數(shù)據(jù)庫(kù)連接的方法
* @return 數(shù)據(jù)庫(kù)連接對(duì)象
*/
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ù)庫(kù)驅(qū)動(dòng)類:" + ex.getMessage());
// 在控制臺(tái)輸出異常堆棧信息
// ex.printStackTrace();
} catch (Exception ex) {
System.err.println("獲取數(shù)據(jù)庫(kù)連接錯(cuò)
誤:" + ex.getMessage());
// 在控制臺(tái)輸出異常堆棧信息
// ex.printStackTrace();
}
// 返回?cái)?shù)據(jù)庫(kù)連接對(duì)象
return this.dbConnection;
}
/**
* 獲取數(shù)據(jù)庫(kù)命令執(zhí)行對(duì)象的方法
* @param sql 要執(zhí)行的SQL命令拼裝語(yǔ)句字符串
* @return 數(shù)據(jù)庫(kù)命令執(zhí)行對(duì)象
*/
private PreparedStatement getPreparedStatement
(String sql) {
try {
// 根據(jù)獲取的數(shù)據(jù)庫(kù)連接對(duì)象創(chuàng)建數(shù)據(jù)庫(kù)
命令執(zhí)行對(duì)象
this.preStatement = getConnection
().prepareStatement(sql);
} catch (Exception ex) {
System.err.println("獲取數(shù)據(jù)庫(kù)命令執(zhí)
行對(duì)象錯(cuò)誤:" + ex.getMessage());
// 在控制臺(tái)輸出異常堆棧信息
// ex.printStackTrace();
}
// 返回?cái)?shù)據(jù)庫(kù)命令執(zhí)行對(duì)象
return this.preStatement;
}
/**
* 執(zhí)行更新語(yǔ)句(Insert|Update|Delete)
* @param sql 要執(zhí)行的SQL命令拼裝語(yǔ)句字符串
* @return 受影響的行數(shù)
*/
public int executeUpdate(String sql){
try {
// 置空結(jié)果集對(duì)象的原有內(nèi)容
this.rsSet = null;
// 執(zhí)行語(yǔ)句并返回受影響行數(shù)
return this.getPreparedStatement
(sql).executeUpdate();
} catch (SQLException e) {
System.err.println("更新數(shù)據(jù)錯(cuò)誤:" +
e.getMessage());
return 0;
}finally{
// 關(guān)閉數(shù)據(jù)庫(kù)連接資源
closeDBResource();
}
}
/**
* 執(zhí)行查詢語(yǔ)句(Select)
* @param sql 要執(zhí)行的SQL命令拼裝語(yǔ)句字符串
* @return 查詢后的結(jié)果集對(duì)象
*/
public ResultSet executeQuery(String sql){
try {
// 置空結(jié)果集對(duì)象的原有內(nèi)容
this.rsSet = null;
// 執(zhí)行sql語(yǔ)句獲得結(jié)果集
this.rsSet =
this.getPreparedStatement(sql).executeQuery();
} catch (SQLException e) {
System.err.println("查詢數(shù)據(jù)錯(cuò)誤:" +
e.getMessage());
}
// 返回結(jié)果集對(duì)象
return this.rsSet;
}
/**
* 獲取執(zhí)行指定sql語(yǔ)句后的返回結(jié)果集的記錄條數(shù)
* @param sql 要執(zhí)行的SQL命令拼裝語(yǔ)句字符串
* @return 查詢結(jié)果得到的記錄條數(shù)
*/
public int getResultSetCount(String sql) {
// 保存得到指定的sql語(yǔ)句執(zhí)行后返回記錄行數(shù)的計(jì)數(shù)器變量
int count = 0;
try {
// 置空結(jié)果集對(duì)象的原有內(nèi)容
this.rsSet = null;
// 執(zhí)行sql語(yǔ)句獲得結(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ù)庫(kù)連接資源(包括結(jié)果集對(duì)象、命令執(zhí)行對(duì)象、連
接對(duì)象)
*/
public void closeDBResource() {
try {
closeResultSet();
closePreparedStatement();
closeConnection();
} catch (SQLException sqlEx) {
System.err.println(sqlEx.getMessage
());
// 在控制臺(tái)輸出異常堆棧信息
// sqlEx.printStackTrace();
}
}
/**
* 關(guān)閉結(jié)果集對(duì)象的方法
* @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é)果集對(duì)
象錯(cuò)誤:" + sqlEx.getMessage());
// 在控制臺(tái)輸出異常堆棧信息
// sqlEx.printStackTrace();
}
}
/**
* 關(guān)閉數(shù)據(jù)庫(kù)命令執(zhí)行對(duì)象的方法
* @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ù)庫(kù)命
令執(zhí)行對(duì)象錯(cuò)誤:" + sqlEx.getMessage());
// 在控制臺(tái)輸出異常堆棧信息
// sqlEx.printStackTrace();
}
}
/**
* 關(guān)閉數(shù)據(jù)庫(kù)連接的方法
* @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ù)庫(kù)連
接錯(cuò)誤:" + sqlEx.getMessage());
// 在控制臺(tái)輸出異常堆棧信息
// sqlEx.printStackTrace();
}
}
}

相關(guān)文章

  • java數(shù)據(jù)結(jié)構(gòu)基礎(chǔ):循環(huán)鏈表和棧

    java數(shù)據(jù)結(jié)構(gòu)基礎(chǔ):循環(huán)鏈表和棧

    這篇文章主要介紹了Java數(shù)據(jù)結(jié)構(gòu)之循環(huán)鏈表、棧的實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了Java數(shù)據(jù)結(jié)構(gòu)中循環(huán)鏈表、棧、的功能、定義及使用方法,需要的朋友可以參考下
    2021-08-08
  • java8 stream的多字段排序?qū)崿F(xiàn)(踩坑)

    java8 stream的多字段排序?qū)崿F(xiàn)(踩坑)

    這篇文章主要介紹了java8 stream的多字段排序?qū)崿F(xiàn)(踩坑),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • java 學(xué)習(xí)筆記(入門篇)_java的安裝與配置

    java 學(xué)習(xí)筆記(入門篇)_java的安裝與配置

    學(xué)習(xí)Java已經(jīng)很長(zhǎng)時(shí)間了,由于基礎(chǔ)不好遇到問題就無(wú)從下手,所以,打算寫Java的隨手筆記來(lái)鞏固基礎(chǔ),加強(qiáng)學(xué)習(xí),接下來(lái)講解java的安裝,配置等,感興趣的朋友可以參考下
    2013-01-01
  • springboot如何實(shí)現(xiàn)國(guó)際化配置

    springboot如何實(shí)現(xiàn)國(guó)際化配置

    這篇文章主要介紹了springboot如何實(shí)現(xiàn)國(guó)際化配置問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • SpringBoot項(xiàng)目中處理返回json的null值(springboot項(xiàng)目為例)

    SpringBoot項(xiàng)目中處理返回json的null值(springboot項(xiàng)目為例)

    本文以spring boot項(xiàng)目為例給大家介紹SpringBoot項(xiàng)目中處理返回json的null值問題,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友參考下
    2019-10-10
  • Java中Servlet的生命周期

    Java中Servlet的生命周期

    這篇文章主要介紹了Java中Servlet的生命周期,Servlet?初始化后調(diào)用?init?()?方法、Servlet?調(diào)用?service()?方法來(lái)處理客戶端的請(qǐng)求、Servlet?銷毀前調(diào)用?destroy()?方法,下面來(lái)看看具體的解析吧,需要的小伙伴可以參考一下
    2022-01-01
  • SpringBoot2 task scheduler 定時(shí)任務(wù)調(diào)度器四種方式

    SpringBoot2 task scheduler 定時(shí)任務(wù)調(diào)度器四種方式

    這篇文章主要介紹了SpringBoot2 task scheduler 定時(shí)任務(wù)調(diào)度器四種方式,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • Java使用Condition控制線程通信的方法實(shí)例詳解

    Java使用Condition控制線程通信的方法實(shí)例詳解

    這篇文章主要介紹了Java使用Condition控制線程通信的方法,結(jié)合實(shí)例形式分析了使用Condition類同步檢測(cè)控制線程通信的相關(guān)操作技巧,需要的朋友可以參考下
    2019-09-09
  • springcloud項(xiàng)目改名的操作方法

    springcloud項(xiàng)目改名的操作方法

    這篇文章主要介紹了springcloud項(xiàng)目改名的操作方法,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-11-11
  • 示例解析java重載Overloading與覆蓋Overriding

    示例解析java重載Overloading與覆蓋Overriding

    這篇文章主要介紹了java重載Overloading與覆蓋Overriding的示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-05-05

最新評(píng)論