📄 dbmanager.java
字号:
/*
* 创建日期 2005-5-5
*/
package com.suninformation.database;
import com.suninformation.user.*;
import com.suninformation.tools.*;
import java.sql.*;
/**
* @author 刘镇
* 数据库管理
*/
public class DBManager {
/**
* 获取数据库连接 设置好CLASSFORNAME、SERVANDDB、USER、PWD参数后,进行数据库连接,并返回Connection
* 对象,如果连接过程中发生异常,将返回一个null值。
*
* @return Connection对象
* @throws UnacceptableException
*/
public static Connection getConnection() throws UnacceptableException {
Connection conn = null;
String DBCLASSFORNAME = null;
String DBSERVER = null;
String DBUSER = null;
String DBPWD = null;
try {
DBCLASSFORNAME = SunGlobal.getDbClassForName(); //"sun.jdbc.odbc.JdbcOdbcDriver";
DBSERVER = SunGlobal.getDbServer(); // "jdbc:odbc:OKNET_DB";
DBUSER = SunGlobal.getDbUser(); //"";
DBPWD = SunGlobal.getDbPwd(); //"";
} catch (UnloadPropertiesException e) {
throw new UnacceptableException(e.getMessage(), e);
}
try {
Class.forName(DBCLASSFORNAME).newInstance();
conn = DriverManager.getConnection(DBSERVER, DBUSER, DBPWD);
} catch (Exception e) {
throw new UnacceptableException("连接数据库失败。", e);
}
return conn;
}
/**
* 关闭所有数据库相关对象如果rs、pstmt、conn对象不为null,则对其进行关闭操作, 若发生异常,控制台输出错误信息。
*
* @param conn
* Connection对象
* @param pstmt
* PreparedStatement对象
* @param rs
* ResultSet对象
*/
public static void closeObject(Connection conn, PreparedStatement pstmt,
ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void closeObject(Connection conn, Statement stmt, ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -