📄 dbconnection.java
字号:
package com.cn.cms.util;
/**
* database connect
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: 智创软件</p>
* @author 黄文建
* @version 1.0
*/
import java.sql.*;
import java.sql.PreparedStatement;
import java.util.Properties;
public class DBConnection {
private final int PARAMVALUE = 3; //参数传递属于那几类
private final String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
private final String url = "jdbc:odbc:cms";
// private final String driver = "org.gjt.mm.mysql.Driver";
// private final String url = "jdbc:mysql:///ECardSuite";
// private final String driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
// private final String url = "jdbc:microsoft:sqlserver://netstudy:1433;DatabaseName=cms;SelectMethod=cursor";
private final Properties pr = null;
private final String userName = "sa";
private final String userPasw = "";
private boolean unitOfWork = false;
private Connection cnCon = null;
private PreparedStatement pstmt = null;
private ResultSet rs = null;
/**
* constructs
*/
public DBConnection() {
new DBDriver(driver);
}
public Connection getDBConnection() {
try {
switch (PARAMVALUE) {
case 1:
cnCon = getConnection(this.url);
break;
case 2:
cnCon = getConnection(this.url, this.userName);
break;
case 3:
cnCon = getConnection(this.url, this.userName, this.userPasw);
break;
default:
cnCon = null;
break;
}
}
catch (Exception e) {
System.out.println(e.getMessage());
}
return cnCon;
}
/**
* 连接数据库
* @param connetStr
* @param user
* @param pasw
* @return
*/
public Connection getConnection(String connetStr, String user, String pasw) {
try {
cnCon = DriverManager.getConnection(connetStr, user, pasw);
}
catch (SQLException ex) {
ex.printStackTrace();
}
return cnCon;
}
/**
* 连接数据库
* @param connetStr
* @param user
* @return
*/
public Connection getConnection(String connetStr, String user) {
try {
cnCon = DriverManager.getConnection(connetStr, user, "");
}
catch (SQLException ex) {
ex.printStackTrace();
}
return cnCon;
}
/**
* 连接数据库
* @param connetStr
* @return
*/
public Connection getConnection(String connetStr) {
try {
cnCon = DriverManager.getConnection(connetStr, "sa", "");
}
catch (SQLException ex) {
ex.printStackTrace();
}
return cnCon;
}
/**
* 事务处理
* @param supportTran
*/
public void beginTran(boolean supportTran) {
unitOfWork = supportTran;
getDBConnection();
try {
if (supportTran) {
cnCon.setAutoCommit(false);
}
else {
cnCon.setAutoCommit(true);
}
}
catch (SQLException e) {
System.out.println(e.getMessage());
}
}
/**
* 事务提交
*/
public void commitTran() {
if (unitOfWork) {
try {
cnCon.commit();
closeConnection();
}
catch (SQLException e) {
rollBackTran();
}
}
closeConnection();
}
/**
* 事务回滚
* @return boolean
* @throws TranSysException
* @throws TranRollBackException
*/
public boolean rollBackTran() {
boolean returnValue = false;
try {
cnCon.rollback();
log("TransactionContext: afterRollback");
closeConnection();
returnValue = true;
}
catch (SQLException e) {
closeConnection();
}
return returnValue;
}
/**
* 关闭连接
*/
public void closeConnection() {
try {
if (cnCon != null && !cnCon.isClosed()) {
cnCon.close();
log("Connection is closed");
}
}
catch (SQLException e) {
}
}
/**
* 关闭语句
* @param stmt
*/
public void closeStatement(Statement stmt) {
try {
if (stmt != null) {
stmt.close();
}
}
catch (SQLException e) {
}
}
/**
* 关闭数据集
* @param result
*/
public void closeResultSet(ResultSet result) {
try {
if (result != null) {
result.close();
}
}
catch (Exception e) {
}
}
/**
* 日志
* @param s
*/
private void log(String s) {
System.out.println(s);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -