public void druidTest() throws Exception {
//创建一个Properties 对象 读取配置文件
Properties properties = new Properties();
properties.load(new FileInputStream("src\\druid.properties"));
// 创建一个指定参数的数据库连接池, Druid 连接池
DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
//建立连接
Connection connection = dataSource.getConnection();
System.out.println("连接成功");
connection.close();
}
配置文件
#key=value
driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/jdbcstudy?rewriteBatchedStatements=true&useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC
username=root
password=root
#initial connection Size
initialSize=10
#min idle connecton size
minIdle=5
#max active connection size
maxActive=20
#max wait time (5000 mil seconds) 最大等待时间(等待队列)
maxWait=5000
Druid工具类
package com.JDBC_Utils;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class JDBCUtilsByDruid {
/**
* 基于 druid 数据库连接池的工具类
*/
private static DataSource ds;
static {
try {
//创建一个Properties 对象 读取配置文件
Properties properties = new Properties();
properties.load(new FileInputStream("src\\druid.properties"));
// 创建一个指定参数的数据库连接池, Druid 连接池
ds = DruidDataSourceFactory.createDataSource(properties);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static Connection getConnection() {
try {
return ds.getConnection();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public static void close(ResultSet resultSet, Statement statement, Connection connection) {
try {
//关闭连接: 在数据库连接池技术中,close 不是真的断掉连接
//而是把使用的 Connection 对象放回连接池
if (resultSet != null) {
resultSet.close();
}
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
对Druid工具类测试
package com.JDBC_Utils;
import org.junit.jupiter.api.Test;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class JDBCUtilsByDruidTest {
@Test
public void select() {
Connection connection = null;
PreparedStatement preparedStatement = null;
String sql = "select * from news where id=?";
ResultSet resultSet = null;
try {
//建立连接
connection = JDBCUtilsByDruid.getConnection();
//创建一个 PreparedStatement对象,用于将参数化的SQL语句发送到数据库。
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setObject(1, 10);
//执行sql语句,返回一个结果集
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
System.out.println(resultSet.getObject("id"));
System.out.println(resultSet.getObject("content"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCUtilsByDruid.close(resultSet, preparedStatement, connection);
System.out.println("执行完毕~");
}
}
@Test
public void Dml() {
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
//建立连接
connection = JDBCUtilsByDruid.getConnection();
//插入语句
String sql = "insert into news values (?,?)";
//创建一个 PreparedStatement对象,用于将参数化的SQL语句发送到数据库
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setObject(1, 11);
preparedStatement.setObject(2, 11);
//执行sql语句
int rows = preparedStatement.executeUpdate();
System.out.println(rows > 0 ? "操作成功" : "操作失败");
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCUtilsByDruid.close(null, preparedStatement, connection);
}
}
}
(土方法) ResultSet =封装=> Arraylist
//这里当我们关闭连接时,就无法再次获取 结果集的 内容,要想对关闭连接后的 结果集 进行操作
//我们可以在获取结果集的时候 创建 一个表 的类(表中的每一列对应类的属性),表中的每一条记录都是一个对象,
// 将每一次获得的记录封装到ArrayList集合中,就可以对获取的记录进行操作管理
@Test
public void selectToArrayList() {
Connection connection = null;
PreparedStatement preparedStatement = null;
//String sql = "select * from news where id=?";
String sql = "select * from news ";
ResultSet resultSet = null;
ArrayList<News> list = new ArrayList<>();
try {
//建立连接
connection = JDBCUtilsByDruid.getConnection();
//创建一个 PreparedStatement对象,用于将参数化的SQL语句发送到数据库。
preparedStatement = connection.prepareStatement(sql);
//preparedStatement.setObject(1, 10);
//执行sql语句,返回一个结果集
// resultSet = preparedStatement.executeQuery();
resultSet = preparedStatement.executeQuery(sql);
while (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
String content = resultSet.getString("content");
//把得到的 resultset 的记录,封装到 News 对象,放入到 list 集合
list.add(new News(id, name, content)); //将每次获取的记录封装到 集合中
}
System.out.println(list);
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCUtilsByDruid.close(resultSet, preparedStatement, connection);
System.out.println("执行完毕~");
}
}
将数据库的 表 当作一个 类进行操作管理 (JavaBean) (此类要 公开 )
package com.JDBC_Utils;
public class News {
//属性顺序要与数据库列名保持一致
private Integer id;
private String name;
private String content;
//一定要有无参构造器,反射需要
public News() {
}
public News(Integer id, String name, String content) {
this.id = id;
this.name = name;
this.content = content;
}
@Override
public String toString() {
return "\nNews\n{" +
"id=" + id +
", name='" + name + '\'' +
", content=" + content +
'}';
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}