public void druidTest () throws Exception {
Properties properties = new Properties ();
properties.load(new FileInputStream ("src\\druid.properties" ));
DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
Connection connection = dataSource.getConnection();
System.out.println("连接成功" );
connection.close();
}
配置文件
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
initialSize =10
minIdle =5
maxActive =20
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 {
private static DataSource ds;
static {
try {
Properties properties = new Properties ();
properties.load(new FileInputStream ("src\\druid.properties" ));
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 {
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 = connection.prepareStatement(sql);
preparedStatement.setObject(1 , 10 );
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 = connection.prepareStatement(sql);
preparedStatement.setObject(1 , 11 );
preparedStatement.setObject(2 , 11 );
int rows = preparedStatement.executeUpdate();
System.out.println(rows > 0 ? "操作成功" : "操作失败" );
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCUtilsByDruid.close(null , preparedStatement, connection);
}
}
}
(土方法) ResultSet =封装=> Arraylist
@Test
public void selectToArrayList () {
Connection connection = null ;
PreparedStatement preparedStatement = null ;
String sql = "select * from news " ;
ResultSet resultSet = null ;
ArrayList<News> list = new ArrayList <>();
try {
connection = JDBCUtilsByDruid.getConnection();
preparedStatement = connection.prepareStatement(sql);
resultSet = preparedStatement.executeQuery(sql);
while (resultSet.next()) {
int id = resultSet.getInt("id" );
String name = resultSet.getString("name" );
String content = resultSet.getString("content" );
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;
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?