一、Druid基本使用
一、使用步骤
pom.xml 添加依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.19</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
//这里有个小插曲,之前这段是从网上复制到pom.xml,然而会报错,原因是复制的内容里有特殊字符之类,但可能是不可见字符,后来,复制原来可用的dependency段,在这上面修改,果然问题解决
定义配置文件druid.properties:
是properties形式的
可以叫任意名称,可以放在任意目录下
加载配置文件properties
获取数据库连接对象:通过工厂来获取DruidDataSourceFactory
获取连接:getConnection
配置文件内容:
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/db3?useSSL=false
username=root
password=123456789
# 初始化连接数量
initialSize=5
# 最大连接数
maxActive=10
# 最大等待时间
maxWait=3000
minIdle=3
public class DruidDemo01 {
public static void main(String[] args) throws Exception {
Properties properties = new Properties();
InputStream is = DruidDemo01.class.getClassLoader().getResourceAsStream("druid.properties");
properties.load(is);
DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
Connection conn = dataSource.getConnection();
System.out.println(conn);
}
}
上面的代码中 getResourceAsStream取值如果为空,可参考这个解决方法 getResourceAsStream取值为空的问题解决方法
二、定义Druid工具类
定义一个类JDBCUtils
提供静态代码块加载配置文件,初始化连接对象
提供方法
获取连接方法:通过数据库连接池获取连接
释放资源
获取连接池的方法
示例:
package com.test;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class JDBCUtils {
//1.定义成员变量
private static DataSource dataSource;
static {
try {
//1.加载配置文件
Properties properties = new Properties();
properties.load(JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties"));
//2.获取DataSource
dataSource = DruidDataSourceFactory.createDataSource(properties);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取连接
*/
public static Connection getConnection() throws Exception {
return dataSource.getConnection();
}
/**
* 释放资源
*/
public static void close(Statement stmt, Connection conn){
close(null, stmt, conn);
}
/**
* 释放资源
*/
public static void close(ResultSet rs, Statement stmt, Connection conn){
if (rs != null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (stmt != null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
* 获取连接池
*/
public static DataSource getDataSource(){
return dataSource;
}
}
使用工具类:
package com.test;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Properties;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args ) {
testdb();
//testload();
}
private static void testload()
{
System.out.println( "Hello World!" );
Properties properties = new Properties();
InputStream is = App.class.getClassLoader().getResourceAsStream("druid.properties");
//InputStream is = App.class.getClassLoader().getResourceAsStream("/com/test/druid.properties");
try {
properties.load(is);
} catch (IOException e) {
e.printStackTrace();
}
DataSource dataSource = null;
try {
dataSource = DruidDataSourceFactory.createDataSource(properties);
} catch (Exception e) {
e.printStackTrace();
}
Connection conn = null;
try {
conn = dataSource.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
System.out.println(conn);
}
private static void testdb(){
Connection conn = null;
PreparedStatement pstmt = null;
try {
/**
* 完成添加操作
*/
//1.获取连接
conn = com.test.JDBCUtils.getConnection();
//2.定义sql
String sql = "insert into user values(?, ?, ?)";
//3.获取pstmt对象
pstmt = conn.prepareStatement(sql);
//4.给?赋值
pstmt.setString(1, "5");
pstmt.setString(2, "张三");
pstmt.setDouble(3, 50);
//5.执行sql
int count = pstmt.executeUpdate();
System.out.println(count);
} catch (Exception e) {
e.printStackTrace();
}finally {
com.test.JDBCUtils.close(pstmt, conn);
}
}
}
本测试是用idea创建的一个简单demo 如图创建该类型项目即可: