当前位置:网站首页>JDBC工具类封装
JDBC工具类封装
2022-04-23 05:42:00 【hanyc..】
jdbc工具类的封装:
package com.hyc.study02.utils;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
public class JDBCUtils {
private static String driver = null;
private static String url = null;
private static String username = null;
private static String psw = null;
static {
try {
InputStream inputStream = JDBCUtils.class.getClassLoader().getResourceAsStream("db.properties");
Properties properties = new Properties();
properties.load(inputStream);
driver = properties.getProperty("driver");
//2、获取用户信息、url
url = properties.getProperty("url");
username = properties.getProperty("username");
psw = properties.getProperty("psw");
//驱动只用加载一次
//1、加载驱动
Class.forName(driver);
} catch (Exception e) {
e.printStackTrace();
}
}
//获取连接
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, username, psw);
}
//释放连接
public static void release(Connection connection, Statement statement, ResultSet resultSet) {
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
插入数据测试:
package com.hyc.study02;
import com.hyc.study02.utils.JDBCUtils;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class TestInsert {
public static void main(String[] args) {
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
//3、获取数据库连接
connection = JDBCUtils.getConnection();
//4、创建执行sql的对象
statement = connection.createStatement();
//5、执行sql语句
//6、返回执行结果集
String sql = "INSERT INTO `users`(`id`,`NAME`,`PASSWORD`,`email`,`birthday`) " +
"VALUES (4,'hyc','123456','[email protected]','2022-04-18')";
int num = statement.executeUpdate(sql);
if (num > 0) {
System.out.println("插入数据成功!");
}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
//7、释放连接
JDBCUtils.release(connection, statement, resultSet);
}
}
}
删除数据测试:
package com.hyc.study02;
import com.hyc.study02.utils.JDBCUtils;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class TestDelete {
public static void main(String[] args) {
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
//3、获取数据库连接
connection = JDBCUtils.getConnection();
//4、创建执行sql的对象
statement = connection.createStatement();
//5、执行sql语句
//6、返回执行结果集
String sql = "DELETE FROM `users` WHERE id=4";
int num = statement.executeUpdate(sql);
if (num > 0) {
System.out.println("删除数据成功!");
}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
//7、释放连接
JDBCUtils.release(connection, statement, resultSet);
}
}
}
修改数据测试:
package com.hyc.study02;
import com.hyc.study02.utils.JDBCUtils;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class TestUpdate {
public static void main(String[] args) {
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
//3、获取数据库连接
connection = JDBCUtils.getConnection();
//4、创建执行sql的对象
statement = connection.createStatement();
//5、执行sql语句
//6、返回执行结果集
String sql = "UPDATE `users` SET `NAME`='zxzb',`email`='[email protected]' WHERE `id`=1";
int num = statement.executeUpdate(sql);
if (num > 0) {
System.out.println("更新数据成功");
}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
//7、释放连接
JDBCUtils.release(connection, statement, resultSet);
}
}
}
查询数据测试:
package com.hyc.study02;
import com.hyc.study02.utils.JDBCUtils;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class TestSelect {
public static void main(String[] args) {
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
//3、获取数据库连接
connection = JDBCUtils.getConnection();
//4、创建执行sql的对象
statement = connection.createStatement();
//5、执行sql语句
//6、返回执行结果集
String sql = "SELECT * FROM `users` WHERE id=1";
resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
System.out.println(resultSet.getInt("id"));
System.out.println(resultSet.getString("NAME"));
System.out.println(resultSet.getString("PASSWORD"));
System.out.println(resultSet.getString("email"));
System.out.println(resultSet.getDate("birthday"));
}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
//7、释放连接
JDBCUtils.release(connection, statement, resultSet);
}
}
}
版权声明
本文为[hanyc..]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_42732184/article/details/124261085
边栏推荐
- 数据安全入门产品——数据库审计系统详解
- 一文读懂当前常用的加密技术体系(对称、非对称、信息摘要、数字签名、数字证书、公钥体系)
- mysql实现主从复制/主从同步
- The list attribute in the entity is empty or null, and is set to an empty array
- 第36期《AtCoder Beginner Contest 248 打比赛总结》
- acwing854. Floyd finds the shortest path
- Parameter analysis of open3d material setting
- 2 - principes de conception de logiciels
- Introduction to data security -- detailed explanation of database audit system
- MySQL创建oracle练习表
猜你喜欢
转置卷积(Transposed Convolution)
MySQL lock mechanism
Flutter 新一代圖形渲染器 Impeller
mysql sql优化之Explain
jdbc入门\获取数据库连接\使用PreparedStatement
lambda表达式
MySQL的锁机制
Hongji | how does HR carry out self change and organizational change in the digital era?
Frequently asked interview questions - 2 (computer network)
SQL statement simple optimization
随机推荐
实体中list属性为空或者null,设置为空数组
refused connection
Frequently asked interview questions - 2 (computer network)
Split and merge multiple one-dimensional arrays into two-dimensional arrays
Usage and difference of shellexecute, shellexecuteex and winexec in QT
提升独立站转化率攻略 | 挽回弃购用户
Step on the pit: Nacos uses startup CMD - M standalone startup error
OSI层常用协议
Mysql 查询使用\G,列转行
Relative reference and absolute reference of Excel
AcWing 1096. Detailed notes of Dungeon Master (3D BFS) code
Excel obtains the difference data of two columns of data
C, class library
College entrance examination volunteer filling reference
Common protocols of OSI layer
Golang implements Ping connectivity detection case through exec module
Cmake basic tutorial (39) pkgconfig
Solid contract DoS attack
Reading notes of modern methods of C language programming
MySQL创建oracle练习表