我们在使用数据库连接时候,很多时候会考虑使用数据库连接池,因为数据库连接池的机制,使得在大用户数量上进行交互的时候使得其效率比较有优势!
static {
dbPool = new DBPool();
}
public DBPool() {
try {
dataSource = new ComboPooledDataSource();
dataSource.setUser("root");
dataSource.setPassword("123456");
dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/game?user=root&password=123456&useUnicode=true");
dataSource.setDriverClass("com.mysql.jdbc.Driver");
// 设置初始连接池的大小!
dataSource.setInitialPoolSize(2);
// 设置连接池的最小值!
dataSource.setMinPoolSize(1);
// 设置连接池的最大值!
dataSource.setMaxPoolSize(10);
// 设置连接池中的最大Statements数量!
dataSource.setMaxStatements(50);
// 设置连接池的最大空闲时间!
dataSource.setMaxIdleTime(60);
} catch (PropertyVetoException e) {
throw new RuntimeException(e);
}
}
public final static DBPool getInstance() {
return dbPool;
}
public final Connection getConnection() {
try {
return dataSource.getConnection();
} catch (SQLException e) {
throw new RuntimeException("无法从数据源获取连接 ", e);
}
}
public static void main(String[] args) throws SQLException {
Connection con = null;
try {
con = DBPool.getInstance().getConnection();
ResultSet rs = con.createStatement().executeQuery("SELECT * FROM LESOGO_USER");
while (rs.next()) {
System.out.println(rs.getObject(1));
System.out.println(rs.getObject(2));
System.out.println(rs.getObject(3));
}
} catch (Exception e) {
} finally {
if (con != null)
con.close();
}
}
}
public static void main(String[] argv) {
try {
/**
* 让一个命令行参数指定名称,将我们的数据源的绑定。
*/
String jndiName = argv[0];
/**
* 获取数据源使用预设池params ...
* 这是唯一的C3P0的特定代码在这里
*/
DataSource unpooled = DataSources
.unpooledDataSource("jdbc:mysql://127.0.0.1:3306/game?user=root&password=123456&useUnicode=true", "root", "123456");
DataSource pooled = DataSources.pooledDataSource(unpooled);
System.out.println("是否能够获得Connection : "+pooled.getConnection());
/**
* 创建的InitialContext,并绑定的数据源,以它在通常的方式。
* 我们使用的是没有的的InitialContext的构造参数版本,因此通过jndi.properties的文件,
* 系统属性,或通过其他手段,JNDI环境,必须先设置。
*/
InitialContext ctx = new InitialContext();
System.out.println("jndiName : " + jndiName);
ctx.rebind(jndiName, pooled);
System.out.println("DataSource bound to nameservice under the name /"" + jndiName + '/"');
} catch (Exception e) {
e.printStackTrace();
}
}
static void attemptClose(ResultSet o) {
try {
if (o != null)
o.close();
} catch (Exception e) {
e.printStackTrace();
}
}
static void attemptClose(Statement o) {
try {
if (o != null)
o.close();
} catch (Exception e) {
e.printStackTrace();
}
}
static void attemptClose(Connection o) {
try {
if (o != null)
o.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private JndiBindDataSource() {
}
}
public static void main(String[] argv) {
try {
/**
* 让一个命令行参数指定名称,我们将查找的数据源。
*/
String jndiName = argv[0];
/**
* 创建InitialContext的,和查找惯常做法的DataSource。 我们使用的是没有的的InitialContext的构造参数版本,因此通过jndi.properties的文件,
* 系统属性,或通过其他手段,JNDI环境,必须先设置。
*/
InitialContext ctx = new InitialContext();
/**
* 获取数据源...这是唯一的C3P0的特定代码在这里
*/
DataSource ds = (DataSource) ctx.lookup(jndiName);
/**
* 掌握了这样一个连接东西,通常的方式
*/
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
con = ds.getConnection();
stmt = con.createStatement();
rs = stmt.executeQuery("SELECT * FROM LESOGO_USER");
while (rs.next())
System.out.println(rs.getString(1));
} finally {
/**
* 我试图约在资源管理,`神经质显式关闭每个资源,但如果你只收盘家长资源的习惯(如连接), 让他们接近自己的孩子,都C3P0的数据源一定会妥善处理。
*/
attemptClose(rs);
attemptClose(stmt);
attemptClose(con);
}
} catch (Exception e) {
e.printStackTrace();
}
}
static void attemptClose(ResultSet o) {
try {
if (o != null)
o.close();
} catch (Exception e) {
e.printStackTrace();
}
}
static void attemptClose(Statement o) {
try {
if (o != null)
o.close();
} catch (Exception e) {
e.printStackTrace();
}
}
static void attemptClose(Connection o) {
try {
if (o != null)
o.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private UseJndiDataSource() {
}
}
public static void main(String[] argv) {
try {
//
// 收购之前,您的数据源!
// 获取数据源...这是唯一的C3P0的特定代码在这里
DataSource unpooled = DataSources.unpooledDataSource(
"jdbc:mysql://127.0.0.1:3306/lesogo_game2?user=root&password=123456&useUnicode=true", "root",
"123456");
DataSource pooled = DataSources.pooledDataSource(unpooled);
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
con = pooled.getConnection();
stmt = con.createStatement();
rs = stmt.executeQuery("SELECT * FROM LESOGO_USER");
while (rs.next())
System.out.println(rs.getString(1));
} finally {
attemptClose(rs);
attemptClose(stmt);
attemptClose(con);
}
} catch (Exception e) {
e.printStackTrace();
}
}
static void attemptClose(ResultSet o) {
try {
if (o != null)
o.close();
} catch (Exception e) {
e.printStackTrace();
}
}
static void attemptClose(Statement o) {
try {
if (o != null)
o.close();
} catch (Exception e) {
e.printStackTrace();
}
}
static void attemptClose(Connection o) {
try {
if (o != null)
o.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private UsePoolBackedDataSource() {
}
}