1.数据库连接池
数据库连接池指一个容器(集合),存放数据库连接的容器。系统初始化后,容器被创建,容器会申请连接对象,当用户访问数据库时,从容器中获取连接对象,访问完之后,将连接对象归还给容器。
(1)优点:节约资源,用户访问高效
(2)实现:
①标准接口:DataSource javax.sql包下
1)方法
a.获取连接: getConnection()
b.归还连接: 若连接对象Connection是从连接池中获取,调用Connection.close()方法,则直接归还连接。
②一般不去实现它,由数据库厂商来实现
1)C3P0: 数据库连接池技术(较老)
2)Druid: 数据库连接池技术(阿里,很强)
2.C3P0:
(1)步骤:
①导入jar包(两个) c3p0-0.9.5.2.jar 和 mchange-commons-java-0.2.12.jar
②定义配置文件:
1)名称:c3p0.properties 或者 c3p0-config.xml
2)路径:直接将文件放在src目录下即可
③创建核心对象 数据库连接池对象 CombopooldeDataSource
④获取连接: getConnection
3.Druid:
步骤
(1)导入jar包 druid-1.0.9.jar
(2)定义配置文件:是properties形式的;可叫任意名字,可放在任意目录下)java编程有一句俗语:“约定大于配置,配置大于代码”通过读取配置文件的形式可以实现代码的深层次解耦。
(3)加载配置文件 。Properties 配置文件所包含内容
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3305/db1
username=root
password=root
initialSize=5
maxActive=10
maxWait=3000
(4)获取数据库连接池对象:通过工厂来获取 DruidDataSourceFactory
(5)获取连接:getConnection
public class druid01 {
public static void main(String[] args) throws Exception {
//加载配置文件
Properties pro = new Properties();
InputStream is = druid01.class.getClassLoader().getResourceAsStream("druid.properties");
pro.load(is);
//获取对象
DataSource ds = DruidDataSourceFactory.createDataSource(pro);
//获取连接
Connection conn = ds.getConnection();
}
}
为简化操作,通常会定义一个类来简化代码,
定义工具类:
1.定义一个类 JDBCUtils
2.提供静态代码块加载配置文件,初始化连接池对象
3.提供方法
(1)获取连接方法:通过数据库连接池获取连接
(2)释放资源
(3)获取连接池的方法
public class JDBCutils {
//定义成员变量
private static DataSource ds;
static {
try {
Properties pro = new Properties();
pro.load(JDBCutils.class.getClassLoader().getResourceAsStream("druid.properties"));
try {
ds = DruidDataSourceFactory.createDataSource(pro);
} catch (Exception e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}
//获取连接
public static Connection getConnection() throws SQLException {
return ds.getConnection();
}
//释放资源
public static void close( Statement stmt, Connection conn) {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
public static void close(ResultSet rs, Statement stmt, Connection conn) {
if (rs != null) {
try {
rs.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
public static DataSource getDataSource(){
return ds;
}
}
4.Spring JDBC:JDBC Template
Spring框架对JDBC的简单封装,提供了一个JDBCTemplate对象简化的JDBC的开发
步骤
①导入jar包
②创建JDBCTemplate对象,依赖于数据源DataSource
public static void main(String[] args) {
JdbcTemplate template = new JdbcTemplate(JDBCutils.getDataSource());
String sql = "update words set bala = 10 where id = ?";
int count = template.update(sql, 1);
}
③调用JdbcTemplate 的方法来完成CRUD的操作
1)Update():执行DML语句。增、删、改语句
2)QueryForMap(): 查询结果将结果集封装为map集合
a.该方法查询的结果集长度只能是1
b.将列名作为key,将值作为value,
c.将这条记录封装为一个map集合
3)QueryForlist(): 查询结果将结果集封装为list集合
a.将每一条记录封装为一个map集合,再将多个map集合封装为一个list集合
4)Query(): 查询结果,将结果封装为JavaBean对象
a.Query参数:RowMapper
b.一般使用BeanPropertyRowMapper实现类,可以完成数据到JavaBean的自动封装
c.New BeanPropertyRowMapper<类型>(类型.class)
5)QueryForObject: 查询结果,将结果封装为对象
a.一般用于聚合函数的查询