📄 dbmanager.java~9~
字号:
package service;
import java.sql.*;
import java.util.*;
import common.Common;
/**
*
* <p>Title: DBManager</p>
* <p>Description: 数据库接口</p>
* <p>Copyright: Copyright (c) 2006</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class DBManager {
public static Connection getConnection() throws DBException {
try {
return DBProxy.getConnection(Common.getInstance().getJdbc_driver_name(),
Common.getInstance().getJdbc_url());
}
catch (Exception e) {
e.printStackTrace();
throw new DBException("无法连接数据库!");
}
}
/**
* 建立数据库连接,执行SQL(使用PreparedStatement),关闭数据库连接
* @param sqlForPS Sql for PreparedStatement
* @param params PreparedStatement将用到的参数列表
* @throws DBException
*/
public static void execSql(String sqlForPS, List params) throws DBException {
Connection conn = null;
try {
conn = getConnection();
DBProxy.execSql(conn, sqlForPS, params);
}
catch (Exception e) {
e.printStackTrace();
throw new DBException("执行SQL时出错!");
}
finally {
try {
DBProxy.release(conn, null, null);
}
catch (Exception e) {
e.printStackTrace();
throw new DBException("无法关闭数据库连接");
}
}
}
/**
* 建立数据库连接,执行一系列SQL,关闭数据库连接
* @param sqlAndParams Sql和它的参数。它是一个List,List的每个元素是一个Map, Map包括的键名为"sql"和"params"
* @throws DBException
*/
public static void execSqls(List sqlAndParams) throws DBException{
Connection conn = null;
try {
conn = getConnection();
DBProxy.execSqls(conn,sqlAndParams);
}
catch (Exception e) {
e.printStackTrace();
throw new DBException("执行SQL时出错!");
}
finally {
try {
DBProxy.release(conn, null, null);
}
catch (Exception e) {
e.printStackTrace();
throw new DBException("无法关闭数据库连接");
}
}
}
/**
* 建立数据库连接,按SQL直接生成用Map数组包装的ResultSet(使用PreparedStatement),关闭数据连接
* @param sqlForPS Sql for PreparedStatement
* @param params PreparedStatement将用到的参数列表
* @return @see RSUtil.toMaps()
* @throws DBException
*/
public static List getMapsBySql(String sqlForPS, List params) throws DBException {
Connection conn = null;
try {
conn = getConnection();
return DBProxy.getMapsBySql(conn, sqlForPS, params);
}
catch (Exception e) {
e.printStackTrace();
throw new DBException("执行SQL时出错!");
}
finally {
try {
DBProxy.release(conn, null, null);
}
catch (Exception e) {
e.printStackTrace();
throw new DBException("无法关闭数据库连接");
}
}
}
/**
* 建立数据库连接,按SQL直接生成用二维数组包装的ResultSet(使用PreparedStatement),关闭数据连接
* @param sqlForPS Sql for PreparedStatement
* @param params PreparedStatement将用到的参数列表
* @return @see RSUtil.toMaps()
* @throws DBException
*/
public static List getValueListBySql(String sqlForPS, List params) throws DBException {
Connection conn = null;
try {
conn = getConnection();
return DBProxy.getValueListBySql(conn, sqlForPS, params);
}
catch (Exception e) {
throw new DBException("执行SQL时出错!",e);
}
finally {
try {
DBProxy.release(conn, null, null);
}
catch (Exception e) {
e.printStackTrace();
throw new DBException("无法关闭数据库连接");
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -