Maven下Druid连接池配置及使用

1.在pom.xml中导入架包及jdbc架包

<dependency>
          <groupId>com.alibaba</groupId>
          <artifactId>druid</artifactId>
          <version>1.1.6</version>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.17</version>
    </dependency>

2.定义配置文件(这里的文件时properties格式的,可以放到任意位置)
druid.properties

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/db1?serverTimezone=Asia/Shanghai
username=root
password=root
#初始化连接数量
initialSize=5
#最大连接数
maxActive=10
#最大等待时间
maxWait=3000

3.测试,需要先加载配置文件

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.util.Properties;

public class DruidDemo {
     
    public static void main(String[] args) throws Exception {
     
        //1.加载配置文件
        Properties pro = new Properties();
        InputStream is = DruidDemo.class.getClassLoader().getResourceAsStream("druid.properties");
        pro.load(is);
        //2.获取连接池对象
        DataSource ds = DruidDataSourceFactory.createDataSource(pro);
        //3.获取连接
        Connection conn = ds.getConnection();
        System.out.println(conn);
    }
}

4.结果:出现如下图即成功
在这里插入图片描述
5.简化步骤,将一些操作封装到一个工具类中
JDBCUtils.java

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;

/*
Druid连接池的工具类
 */
public class JDBCUtils {
     
    //1.定义成员变量 DataSource
    private static DataSource ds;
    static {
     
        //1.加载配置文件
        Properties pro = new Properties();
        try {
     
            pro.load(JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties"));
            //获取DataSource
            ds = DruidDataSourceFactory.createDataSource(pro);
        } catch (IOException e) {
     
            e.printStackTrace();
        } catch (Exception 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 (Exception e) {
     
                System.out.println(e.getMessage());
            }
        }
        if (conn!=null){
     
            try {
     
                conn.close();//归还连接
            } catch (Exception e) {
     
                System.out.println(e.getMessage());
            }
        }
    }
    public static void close(ResultSet rs, Statement stmt, Connection conn){
     
        if (rs!=null) {
     
            try {
     
                rs.close();
            } catch (Exception e) {
     
                System.out.println(e.getMessage());
            }
        }
        if (stmt!=null) {
     
            try {
     
                stmt.close();
            } catch (Exception e) {
     
                System.out.println(e.getMessage());
            }
        }
        if (conn!=null){
     
            try {
     
                conn.close();//归还连接
            } catch (Exception e) {
     
                System.out.println(e.getMessage());
            }
        }
    }
    //获取连接池
    public static DataSource getDataSource(){
     
        return ds;
    }
}

测试工具类:DruidTest.java

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

//测试工具类
public class DruidTest {
     
    public static void main(String[] args) throws SQLException {
     
        //给dept表添加一条记录
        //1.获取连接
        Connection conn = JDBCUtils.getConnection();
        //2.定义sql
        String sql = "insert into dept values(?,?,?)";
        //3.获取邮差对象
        PreparedStatement ps = conn.prepareStatement(sql);
        //4.给?赋值
        ps.setInt(1,50);
        ps.setString(2,"HAHAHAH");
        ps.setString(3,"CHINA");
        //5.执行sql
        ps.executeUpdate();
    }
}

结果:插入成功Maven下Druid连接池配置及使用_第1张图片

你可能感兴趣的:(JDBC)