jdbc与druid连接池的使用详解
作者:m0_52000372 时间:2023-09-18 17:50:11
使用jdbc实现对数据库的操作
Ⅰ 获取数据库连接
package org.example.utils;
import java.sql.*;
public class JavaDateConnection {
/**
* 获取数据库连接
* @return Connection
*/
public Connection getConn() {
//project为数据库名
String url = "jdbc:mysql://localhost:3306/project";
//用户名
String username = "root";
//密码
String password = "Hyk59308";
Connection conn = null;
try {
//注册驱动
Class.forName("com.mysql.jdbc.Driver");
//classLoader,加载对应驱动
conn = DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
Ⅱ编写SQL语句对数据库进行操作
String sql1="insert into myTable values(?,?,?,?)";//定义sql语句
String sql2="select * from myTable"; //定义sql语句
int result=0; //修改操作的返回值是一个整数,即受影响的行数
``/**
* PreparedStatement继承自Statement接口,PreparedStatement的对象已预编译过,
* 执行速度快于Statement对象,创建其对象时,需要SQL命令字符串作为对象
*/
PreparedStatement ps=connection.prepareStatement(sql1);
ps.setString(1,"tanker");
ps.setString(2, "m");
ps.setString(3,"1991-11-20");
ps.setString(4, "Franch");
result=ps.executeUpdate();
if(result>0)
System.out.println("插入成功");
else
System.out.println("插入失败");
//Statement用于将sql语句发送到数据库
Statement statement=connection.createStatement();
//执行数据库操作返回的结果集,其定义的是数据库游标
ResultSet results=statement.executeQuery(sql2);
System.out.println("name"+" "+"sex"+" "+"birth"+" "+"birthaddr");
System.out.println("------------------------");
while(results.next())
{
System.out.println(results.getString("name")+" "+
results.getString("sex")+" "+
results.getString("birth")+" "+
results.getString("birthaddr"));
}
System.out.println("搞定!");
Ⅲ关闭相关资源
* 关闭Connection PreparedStatement
* @param connection
* @param preparedStatement
*/
public static void closeConnection(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet){
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (preparedStatement != null) {
try {
preparedStatement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
使用Druid连接池u对数据库进行操作
Ⅰ创建Druid连接池对象并获取
package util;
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.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class DBUtil {
private static DataSource ds;
static {
//1.加载配置文件
Properties pro = new Properties();
try {
pro.load(DBUtil.class.getClassLoader().getResourceAsStream("/db.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();
}
Ⅱ创建SQL语句实现对数据库的操作
/**
* @param sql SQL语句
* @param objsSQL语句占位符实参,如果没有参数则传入null
* @return 返回增删改的结果,类型为int
*/
public static int executeDML(String sql,Object...objs){
// 声明jdbc变量
Connection conn = null;
PreparedStatement ps = null;
int i = -1;
try {
// 获取连接对象
conn = DBUtil.getConnection();
// 开启事务管理
conn.setAutoCommit(false);
// 创建SQL命令对象
ps = conn.prepareStatement(sql);
// 给占位符赋值
if(objs!=null){
for(int j=0;j<objs.length;j++){
ps.setObject(j+1,objs[j]);
}
}
// 执行SQL
i = ps.executeUpdate();
conn.commit();
} catch (Exception e) {
try {
conn.rollback();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
e.printStackTrace();
} finally {
DBUtil.closeAll(null, ps, conn);
}
return i;
}
Ⅲ关闭相关资源
//关闭资源
public static void closeAll(ResultSet rs,Statement stmt,Connection conn){
try {
if(rs!=null){
rs.close();
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
来源:https://blog.csdn.net/m0_52000372/article/details/115255588
标签:jdbc,druid,连接池
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Servlet3.0实现文件上传的方法
2023-08-15 00:52:44
![](https://img.aspxhome.com/file/2023/3/58143_0s.png)
Eclipse打开时“发现了以元素'd:skin'”开头的无效内容。此处不应含有子元素的解决方法
2022-11-24 17:54:50
![](https://img.aspxhome.com/file/2023/4/137364_0s.png)
Android View与Compose互相调用实例探究
2021-06-11 09:07:29
![](https://img.aspxhome.com/file/2023/3/96763_0s.png)
MyBatis中的连接池及事物控制配置过程
2022-05-17 05:00:40
![](https://img.aspxhome.com/file/2023/7/122177_0s.jpg)
C#使用Dispose模式实现手动对资源的释放
2022-09-21 16:12:14
Java经典面试题最全汇总208道(四)
2023-11-08 23:59:26
![](https://img.aspxhome.com/file/2023/5/59065_0s.png)
SpringCloud-Alibaba-Nacos启动失败解决方案
2023-12-19 05:34:45
![](https://img.aspxhome.com/file/2023/4/119774_0s.png)
Android 安全加密:非对称加密详解
2021-08-03 18:39:43
![](https://img.aspxhome.com/file/2023/9/138759_0s.png)
详解Java回环屏障CyclicBarrier
2022-08-30 15:02:09
![](https://img.aspxhome.com/file/2023/9/76219_0s.png)
解决android studio卡顿,提升studio运行速度的方法
2021-11-19 01:25:05
![](https://img.aspxhome.com/file/2023/4/138474_0s.jpg)
无法从 int? 转换为 int 运行时出现错误
2022-01-02 08:26:11
![](https://img.aspxhome.com/file/2023/1/96361_0s.png)
Java实现布隆过滤器的方法步骤
2023-02-15 20:31:47
![](https://img.aspxhome.com/file/2023/4/61264_0s.jpg)
SpringBoot 如何添加容器启动的初始化逻辑的操作方法
2021-11-20 12:10:39
![](https://img.aspxhome.com/file/2023/2/79272_0s.png)
Guava - 并行编程Futures详解
2022-04-28 23:16:34
解决idea 项目编译后没有class文件的问题
2023-11-04 07:55:06
![](https://img.aspxhome.com/file/2023/9/97809_0s.jpg)
Android应用启动速度优化
2023-03-18 04:07:44
Java超详细讲解三大特性之一的多态
2022-03-17 02:11:45
![](https://img.aspxhome.com/file/2023/0/119170_0s.png)
EditText监听方法,实时的判断输入多少字符
2021-08-16 11:32:36
![](https://img.aspxhome.com/file/2023/1/94981_0s.jpg)
SpringBoot中的main方法注入service
2021-10-31 15:33:33
Java实现AOP面向切面编程的实例教程
2023-02-20 19:32:38