1.方式
一种是使用直接c3p0数据库连接池,一种是使用配置文件将连接信息写在配置文件中。
将连接信息写在配置文件中有很多好处,比如如果想替换一个线上项目的数据库,只要改变配置文件中的链接信息就可以了。再就是一些需要修改的地方,比如想要修改链接池的属性,也只需要修配置文件就可以了,不需要去修改java代码,那样还需要重新编译生成class文件,重新运行一遍。
2.直接使用jar包
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
package com.begin21.w1106.ClassTest.excute;
import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;
import com.mchange.v2.c3p0.ComboPooledDataSource;
//数据库连接池
public class ConnectionPool {
private static ComboPooledDataSource comboPooledDataSource;
//初始化连接池
public static final void initDBSource(){
comboPooledDataSource = new ComboPooledDataSource();
try {
comboPooledDataSource.setDriverClass("com.mysql.jdbc.Driver");
} catch (PropertyVetoException e) {
e.printStackTrace();
}
comboPooledDataSource.setJdbcUrl("jdbc:mysql://localhost:3306/gaobo");
comboPooledDataSource.setUser("root");
comboPooledDataSource.setPassword("");
comboPooledDataSource.setMinPoolSize(3); //最小线程数
comboPooledDataSource.setMaxPoolSize(20);//最大线程数
comboPooledDataSource.setInitialPoolSize(5);//初始线程数
comboPooledDataSource.setAcquireIncrement(2);//每次新增线程数
comboPooledDataSource.setMaxIdleTime(5);//线程空闲时间
}
/**
* 从连接池中获取连接
* @return
*/
public static synchronized Connection getConnection() {
Connection conn = null;
try {
conn = comboPooledDataSource.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static void closeOpenResource(AutoCloseable resource) {
if (null != resource) {
try {
resource.close();
} catch (Exception e) {
e.printStackTrace();
}}}}
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
package com.begin21.w1106.ClassTest.excute;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Main2 {
public static void main(String[] args) {
ConnectionPool.initDBSource();
query();}
private static void query() {
Connection conn = ConnectionPool.getConnection();
PreparedStatement prst = null;
ResultSet rs = null;
String sql = "select * from repo_sto";
try {
prst = conn.prepareStatement(sql);
rs = prst.executeQuery();
while(rs.next()) {
System.out.println(rs.getString(2));}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
ConnectionPool.closeOpenResource(rs);
ConnectionPool.closeOpenResource(prst);
// close不是关闭数据库连接池的连接对象,而是把它放回连接池
ConnectionPool.closeOpenResource(conn);}}}
3.使用配置文件
配置文件 db.properties
package com.begin21.w1106.ClassTest.excute;
import java.beans.PropertyVetoException;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class ConnectionPoolProp {
private static ComboPooledDataSource comboPooledDataSource;
public static final void initDBSource() {
Properties properties = new Properties();
try {
File file = new File(ConnectionPoolProp.class.getResource("/").getFile().toString() + "dbs.properties"); //引用配置文件
System.out.println(ConnectionPoolProp.class.getResource("/").getFile());
InputStream inputStream = new FileInputStream(file);
properties.load(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
comboPooledDataSource = new ComboPooledDataSource();
try {
comboPooledDataSource.setDriverClass(properties.getProperty("driver"));
} catch (PropertyVetoException e) {
e.printStackTrace();
}
comboPooledDataSource.setJdbcUrl(properties.getProperty("url"));
comboPooledDataSource.setUser(properties.getProperty("user"));
comboPooledDataSource.setPassword(properties.getProperty("password"));
comboPooledDataSource.setMinPoolSize(Integer.parseInt(properties.getProperty("c3p0.minPoolSize")));
comboPooledDataSource.setMaxPoolSize(Integer.parseInt(properties.getProperty("c3p0.maxPoolSize")));
comboPooledDataSource.setInitialPoolSize(Integer.parseInt(properties.getProperty("c3p0.initPoolSize")));
comboPooledDataSource.setAcquireIncrement(Integer.parseInt(properties.getProperty("c3p0.acquireIncrement")));
comboPooledDataSource.setMaxStatements(Integer.parseInt(properties.getProperty("c3p0.maxStatements").trim()));
comboPooledDataSource.setMaxIdleTime(Integer.parseInt(properties.getProperty("c3p0.maxIdleTime")));
}
public static synchronized Connection getConnection() {
Connection conn = null;
try {
conn = comboPooledDataSource.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static void closeOpenResource(AutoCloseable resource) {
if (null != resource) {
try {
resource.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();}}}}