您的位置:首页 > 数据库

一个数据库链接代码

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控制读取对应的配置文件
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: