一个数据库链接代码
2014-12-01 09:37
253 查看
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;
import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSource;
/**
* 读取数据库操作接口的实现
*
* @author lost_icer
*
*/
public class ConnectionToMysql {
/** 定义读取配置文件对象 */
public ResourceBundle resb;
/** 数据源 */
private DataSource dataSource = null;
/** 用于存放数据库对象 */
private static Map<Integer, ConnectionToMysql> conMap = new HashMap<Integer, ConnectionToMysql>();
/**
* 定义私有构造函数,防止外部使用new关键字实例化本对象
*/
private ConnectionToMysql() throws Exception {
super();
}
public DataSource Connection(int area) throws Exception {
try {
String a = "ConfigR0" + area;
ResourceBundle resb = ResourceBundle.getBundle(a);
BasicDataSource dsR = new BasicDataSource();
dsR.setDriverClassName(resb.getString("driverClassName"));
dsR.setUsername(resb.getString("username"));
dsR.setPassword(resb.getString("password"));
dsR.setUrl(resb.getString("url"));
System.out.println(dsR.getUrl());
// 初始的连接数
dsR.setInitialSize(Integer.parseInt(resb.getString("initialSize")));
// 最大链接数
dsR.setMaxActive(Integer.parseInt(resb.getString("maxActive")));
// 最大空闲数
dsR.setMaxIdle(Integer.parseInt(resb.getString("maxIdle")));
// 等待时间
dsR.setMaxWait(Integer.parseInt(resb.getString("maxWait")));
// 调取连接时检查有效性
dsR.setTestOnBorrow(true);
dsR.setTestOnReturn(true);
dsR.setTestWhileIdle(true);
//是否回收超时链接
dsR.setRemoveAbandoned(true);
//设置超时链接回收时间(单位:秒)
dsR.setRemoveAbandonedTimeout(300);
// 验证连接有效性的方式,这步不能省
dsR.setValidationQuery("select 1 from dual");
// 失效检查线程运行时间间隔,如果小于等于0,不会启动检查线程,默认-1
dsR.setTimeBetweenEvictionRunsMillis(1800000);
// 大于0,进行连接空闲时间判断,或为0,对空闲的连接不进行验证;默认30分钟
dsR.setMinEvictableIdleTimeMillis(3600000);
dataSource = dsR;
dataSource.getConnection();
} catch (Exception e) {
throw new Exception("初始化数据库连接失败:" + e.getMessage());
}
return dataSource;
}
/**
* 使用Map控制连接对象
*
* @return ConnectionToMysql 返回ConnectionToMysql实例
* */
public synchronized static ConnectionToMysql getInstance(int area)
throws Exception {
ConnectionToMysql conn = null;
System.out.println(conn);
if (conMap.containsKey(area)) {
conn = conMap.get(area);
} else {
conn = new ConnectionToMysql();
conn.dataSource = conn.Connection(area);
conMap.put(area, conn);
}
return conn;
}
/**
* 获得数据库连接接口
*
* @return Connection
* @throws Exception
*/
public Connection getConnectionDB() throws Exception {
// 创建连接数据库对象
Connection conn = null;
try {
conn = dataSource.getConnection();
} catch (Exception e) {
throw new Exception("数据库连接失败:" + e.getMessage());
}
return conn;
}
/**
* 关闭数据库连接接口
*
* @param conn
* 连接对象
*
* @param pStm
* 域处理sql语句对象
*
* @throws SQLException
* 抛出异常
*
* @throws Exception
* 抛出异常
*
*/
public static void colse(Connection conn, PreparedStatement pStm,
ResultSet res_list) throws SQLException, Exception {
try {
if (null != res_list) {
// 关闭结果集
// System.out.println(1);
res_list.close();
}
} catch (SQLException e) {
throw new SQLException("关闭结果集对象失败:" + e.getMessage());
} catch (Exception e) {
throw new SQLException("关闭结果集对象失败:" + e.getMessage());
}
try {
if (null != pStm) {
// System.out.println(2);
pStm.close();
}
} catch (SQLException e) {
throw new SQLException("关闭域处理对象失败:" + e.getMessage());
} catch (Exception e) {
throw new SQLException("关闭域处理对象失败:" + e.getMessage());
}
try {
if (null != conn) {
// System.out.println(3);
conn.close();
}
} catch (SQLException e) {
throw new SQLException("关闭数据连接对象失败:" + e.getMessage());
} catch (Exception e) {
throw new SQLException("关闭数据连接对象失败:" + e.getMessage());
}
}
public static void main(String[] args) {
PreparedStatement ps = null;
Connection conn = null;
try {
conn = ConnectionToMysql.getInstance(1002).getConnectionDB();
ps = conn.prepareStatement("");
System.out.println(conn.toString());
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
ConnectionToMysql.colse(conn, ps, null);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
朋友在做手游后台的时候让帮忙写一个数据库链接,最开始游戏没有分区,使用的是一个数据库,后来推广起来之后,随着人数增长,将数据库根据游戏区分开,这样在访问数据库的时候就必须按照区去访问个各自的数据库,然后就让我再次改了一下,下面是修改后的代码,上线使用后,目前没有发现问题,当然,我也不敢信心十足的保证后面会不会出别的问题,如有不合适的地方,虚心请教。
注: area 表示所访问区的数据库,使用area控制读取对应的配置文件
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;
import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSource;
/**
* 读取数据库操作接口的实现
*
* @author lost_icer
*
*/
public class ConnectionToMysql {
/** 定义读取配置文件对象 */
public ResourceBundle resb;
/** 数据源 */
private DataSource dataSource = null;
/** 用于存放数据库对象 */
private static Map<Integer, ConnectionToMysql> conMap = new HashMap<Integer, ConnectionToMysql>();
/**
* 定义私有构造函数,防止外部使用new关键字实例化本对象
*/
private ConnectionToMysql() throws Exception {
super();
}
public DataSource Connection(int area) throws Exception {
try {
String a = "ConfigR0" + area;
ResourceBundle resb = ResourceBundle.getBundle(a);
BasicDataSource dsR = new BasicDataSource();
dsR.setDriverClassName(resb.getString("driverClassName"));
dsR.setUsername(resb.getString("username"));
dsR.setPassword(resb.getString("password"));
dsR.setUrl(resb.getString("url"));
System.out.println(dsR.getUrl());
// 初始的连接数
dsR.setInitialSize(Integer.parseInt(resb.getString("initialSize")));
// 最大链接数
dsR.setMaxActive(Integer.parseInt(resb.getString("maxActive")));
// 最大空闲数
dsR.setMaxIdle(Integer.parseInt(resb.getString("maxIdle")));
// 等待时间
dsR.setMaxWait(Integer.parseInt(resb.getString("maxWait")));
// 调取连接时检查有效性
dsR.setTestOnBorrow(true);
dsR.setTestOnReturn(true);
dsR.setTestWhileIdle(true);
//是否回收超时链接
dsR.setRemoveAbandoned(true);
//设置超时链接回收时间(单位:秒)
dsR.setRemoveAbandonedTimeout(300);
// 验证连接有效性的方式,这步不能省
dsR.setValidationQuery("select 1 from dual");
// 失效检查线程运行时间间隔,如果小于等于0,不会启动检查线程,默认-1
dsR.setTimeBetweenEvictionRunsMillis(1800000);
// 大于0,进行连接空闲时间判断,或为0,对空闲的连接不进行验证;默认30分钟
dsR.setMinEvictableIdleTimeMillis(3600000);
dataSource = dsR;
dataSource.getConnection();
} catch (Exception e) {
throw new Exception("初始化数据库连接失败:" + e.getMessage());
}
return dataSource;
}
/**
* 使用Map控制连接对象
*
* @return ConnectionToMysql 返回ConnectionToMysql实例
* */
public synchronized static ConnectionToMysql getInstance(int area)
throws Exception {
ConnectionToMysql conn = null;
System.out.println(conn);
if (conMap.containsKey(area)) {
conn = conMap.get(area);
} else {
conn = new ConnectionToMysql();
conn.dataSource = conn.Connection(area);
conMap.put(area, conn);
}
return conn;
}
/**
* 获得数据库连接接口
*
* @return Connection
* @throws Exception
*/
public Connection getConnectionDB() throws Exception {
// 创建连接数据库对象
Connection conn = null;
try {
conn = dataSource.getConnection();
} catch (Exception e) {
throw new Exception("数据库连接失败:" + e.getMessage());
}
return conn;
}
/**
* 关闭数据库连接接口
*
* @param conn
* 连接对象
*
* @param pStm
* 域处理sql语句对象
*
* @throws SQLException
* 抛出异常
*
* @throws Exception
* 抛出异常
*
*/
public static void colse(Connection conn, PreparedStatement pStm,
ResultSet res_list) throws SQLException, Exception {
try {
if (null != res_list) {
// 关闭结果集
// System.out.println(1);
res_list.close();
}
} catch (SQLException e) {
throw new SQLException("关闭结果集对象失败:" + e.getMessage());
} catch (Exception e) {
throw new SQLException("关闭结果集对象失败:" + e.getMessage());
}
try {
if (null != pStm) {
// System.out.println(2);
pStm.close();
}
} catch (SQLException e) {
throw new SQLException("关闭域处理对象失败:" + e.getMessage());
} catch (Exception e) {
throw new SQLException("关闭域处理对象失败:" + e.getMessage());
}
try {
if (null != conn) {
// System.out.println(3);
conn.close();
}
} catch (SQLException e) {
throw new SQLException("关闭数据连接对象失败:" + e.getMessage());
} catch (Exception e) {
throw new SQLException("关闭数据连接对象失败:" + e.getMessage());
}
}
public static void main(String[] args) {
PreparedStatement ps = null;
Connection conn = null;
try {
conn = ConnectionToMysql.getInstance(1002).getConnectionDB();
ps = conn.prepareStatement("");
System.out.println(conn.toString());
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
ConnectionToMysql.colse(conn, ps, null);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
朋友在做手游后台的时候让帮忙写一个数据库链接,最开始游戏没有分区,使用的是一个数据库,后来推广起来之后,随着人数增长,将数据库根据游戏区分开,这样在访问数据库的时候就必须按照区去访问个各自的数据库,然后就让我再次改了一下,下面是修改后的代码,上线使用后,目前没有发现问题,当然,我也不敢信心十足的保证后面会不会出别的问题,如有不合适的地方,虚心请教。
注: area 表示所访问区的数据库,使用area控制读取对应的配置文件
相关文章推荐
- 最近设计了一个生成asp代码的程序,同时也可以作为数据库管理查询的软件,发两张图,等完全做好了,给大家共享!
- 一个用隐藏域修删除数据库记录的代码
- 数据完整性的数据库措施,收藏一个链接
- PHP数据库链接代码
- 一个JDBC和Accsess数据库的查询代码
- 介绍一个完全托管代码的对象数据库DB4O
- C#链接各种数据库代码总结
- 树状菜单 点击一个关闭另一个 链接了数据库
- 另外一个实现链接提示的代码
- 创建一个复制UBB软件信息的链接或按钮的js代码
- 一个不错的从数据库读取图片的跑马灯代码
- 点击一个页面的链接后,刚开始会正常显示。几分钟后就会报出空指针异常,异常代码如下:
- 一个ASP的数据库操作类,意图就是让新手也能写出规范的代码,节少时间。
- 微软项目管理[EPM]数据库应用举例2: 取得一个项目的某大纲代码的值
- 登录判断用户名和密码是否正确的代码(链接和读取数据库)
- 最近设计了一个生成asp代码的程序,同时也可以作为数据库管理查询的软件,有兴趣的朋友可以去下载!
- 【小技巧】如何得到一个网页的所有a标记 herf 链接代码
- 一个棘手的问题,在access数据库 做的一个小站上,数据库链接打开出错
- 封装一个PDO数据库操作类代码
- 一个页面显示2个数据库的查询内容 代码优化