当需要频繁访问数据库时,多次连接和释放需要占用大量资源,而数据库连接池,在开始时请求所配置的连接数量,之后每次从连接池中拿取一个,用完之后连接池会将其回收。
①:DBCP数据源
首先是配置文件,dbcpconfig.properties,将其放在src目录下即可
driverClassName = com.mysql.jdbc.Driver
url = jdbc:mysql://localhost:3306/jdbc
username = root
password = password
initialSize = 8
maxActive = 10
maxIdle = 10
或者在程序内部
static{
BasicDataSource bds = new BasicDataSource();
//获取数据源对象,并对其设置属性值
bds.setDriverClassName(driverClassName);
bds.setUrl(url);
bds.setUsername(username);
bds.setPassword(password);
bds.setInitialSize(initialSize);
//bds.setMaxActive(maxActive);
bds.setMaxIdle(maxIdle);
ds = bds;
}
而后,在同包的文件下面,通过getConnection即可获得一个链接
static{
Properties prop = new Properties();
try{
InputStream in = new DBCP_example().getClass().getClassLoader()
.getResourceAsStream("dbcpconfig.properties");
prop.load(in);
ds = BasicDataSourceFactory.createDataSource(prop);
}catch(Exception e){
throw new ExceptionInInitializerError(e);
}
}
public static void main(String[] args) throws SQLException{
Connection conn = ds.getConnection();
DatabaseMetaData metaData = conn.getMetaData();
System.out.println(metaData.getURL());
System.out.println(metaData.getUserName());
System.out.println(metaData.getDriverName());
}
②:C3P0数据源,最流行的开源数据源
首先是配置文件,c3p0-config.xml,将其放在src目录下
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<default-config>
<property name="jdbcUrl">
jdbc:mysql://localhost:3306/jdbc?useSSL=false
</property>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="user">root</property>
<property name="password">password</property>
<property name="checkoutTimeout">30000</property>
<property name="idleConnectionTestPeriod">30</property>
<property name="initialPoolSize">10</property>
<property name="maxIdleTime">30</property>
<property name="maxPoolSize">100</property>
<property name="minPoolSize">10</property>
<property name="maxStatements">200</property>
</default-config>
<named-config name="lne">
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">
jdbc:mysql://localhost:3306/jdbc?useSSL=false
</property>
<property naem="user">root</property>
<property name="password">password</property>
<property name="initialPoolSize">6</property>
<property name="maxPoolSize">10</property>
</named-config>
<named-config name="model">
<property name="jdbcUrl">
jdbc:mysql://localhost:3306/jdbc?useSSL=false
</property>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="user">root</property>
<property name="password">password</property>
<property name="checkoutTimeout">30000</property>
<property name="idleConnectionTestPeriod">30</property>
<property name="initialPoolSize">10</property>
<property name="maxIdleTime">30</property>
<property name="maxPoolSize">100</property>
<property name="minPoolSize">10</property>
<property name="maxStatements">200</property>
</named-config>
</c3p0-config>
或者在程序内部
public static DataSource ds = null;
public static String driverClassName = "com.mysql.jdbc.Driver";
public static String url = "jdbc:mysql://localhost:3306/jdbc";
public static String username = "root";
public static String password = "password";
public static int initialSize = 6;
public static int maxActive = 10;
public static int maxIdle = 10;
/*
static{
ComboPooledDataSource cpds = new ComboPooledDataSource();
try{
cpds.setDriverClass(driverClassName);
cpds.setJdbcUrl(url);
cpds.setUser(username);
cpds.setPassword(password);
cpds.setInitialPoolSize(initialSize);
cpds.setMaxPoolSize(maxActive);
ds = cpds;
}catch(Exception e){
throw new ExceptionInInitializerError(e);
}
}
*/
主程序,通过getConnection可获得一个连接 static{
ComboPooledDataSource cpds = new ComboPooledDataSource("model");
//指明读取配置文件的哪个用户,如果没有填写,则读取default-config
ds =cpds;
}
//*/
public static void main(String[] args) throws SQLException{
System.out.println(ds.getConnection());
}
③:DBUtils工具
作为一个数据库操作组件,可以简化对数据库操作的编码,它可以将查找的结果以JavaBean的形式返回,使得获取信息更简便。
(1)BeanHandler:将结果集中的第一行数据都封装到一个对应的JavaBean实例中
(2)BeanListHandler:将结果集中的每一行数据都封装到一个JavaBean实例中,并存放在List里
(3)ScalarHandler:将结果集中的某一条记录的其中某一列数据存储成Object对象
public static void testBeanHandler()throws SQLException{
BaseDao basedao = new BaseDao();
String sql = "select * from first where sub=?";
Subject sub = (Subject) BaseDao.query(sql, new BeanHandler<Object>(Subject.class),"math");
//将其封装成一个JavaBean,其中JavaBean中各个名称需和数据库名称一致
if(sub==null){
System.out.println("什么都没...");
//记录一下,由于之前在sub中未统一格式,即数据库中有sub而后我却用name表示,因此获取不了
}
System.out.println(sub.getSub());
}
public static void testBeanListHandler() throws SQLException{
BaseDao basedao = new BaseDao();
String sql = "select * from first";
ArrayList<Subject> subjects = (ArrayList<Subject>) BaseDao.query(sql, new BeanListHandler<Object>(Subject.class));
//将查询的结果(多个),全部封装成JavaBean,并将纳入动态数组之中
for(int i = 0; i<subjects.size();i++){
System.out.println(subjects.get(i).getScorce());
}
}
public static void testScalarHandler() throws SQLException{
BaseDao basedao = new BaseDao();
String sql = "select * from first where sub=?";
Object arr =(Object) BaseDao.query(sql, new ScalarHandler<Object>("sub"), "Python");
//只获取某列的数据,并之获取一个
System.out.println(arr);
}