Druid:数据库连接池实现技术,由阿里巴巴提供的
- 步骤:
1. 导入jar包 druid-1.0.9.jar
2. 定义配置文件:
是properties形式的
可以叫任意名称,可以放在任意目录下
3. 加载配置文件。Properties
4. 获取数据库连接池对象:通过工厂来来获取 DruidDataSourceFactory
5. 获取连接:getConnection - 定义工具类
1. 定义一个类 Jdbccount
2. 提供静态代码块加载配置文件,初始化连接池对象
3. 提供方法
1. 获取连接方法:通过数据库连接池获取连接
2. 释放资源
3. 获取连接池的方法
properties配置文件:(对应数据库的连接,需要放在src目录下)
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/student
username=root
password=
inttialSize=5
maxActive=10
maxWait=300
Jdbc类代码:
public class Jdbccount {
private static DataSource dataSource=null;
static{
try {
Properties pt=new Properties();
pt.load(Jdbccount.class.getClassLoader().getResourceAsStream("jdbc.properties"));
} catch (IOException e) {
e.printStackTrace();
}
}
public DataSource getDataSource() {
return dataSource;
}
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
}
调用Jdbccount 做简单测试
public class Test1 {
public static void main(String[] args) {
JdbcTemplate jt=new JdbcTemplate(JdbcCount.getDs());
String sql="delete from teacher where id=1";
int i=jt.update(sql);
System.out.println(i);
}
}
大于0为成功,0为失败。