当前位置:网站首页>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创建oracle练习表
- 多线程与高并发(2)——synchronized用法详解
- io.lettuce.core.RedisCommandExecutionException: ERR wrong number of arguments for ‘auth‘ command
- 合约锁仓漏洞
- idea插件---背景播放歌曲
- 手动删除eureka上已经注册的服务
- SQL statement simple optimization
- MySQL lock mechanism
- What is JSON? First acquaintance with JSON
- Introduction to data security -- detailed explanation of database audit system
猜你喜欢
2 - software design principles
Duplicate key update in MySQL
多线程与高并发(1)——线程的基本知识(实现,常用方法,状态)
Batch import of orange single micro service
Issue 36 summary of atcoder beginer contest 248
Find the number of "blocks" in the matrix (BFS)
一文读懂当前常用的加密技术体系(对称、非对称、信息摘要、数字签名、数字证书、公钥体系)
Pytorch deep learning practice_ 11 convolutional neural network
MySQL lock mechanism
Jiugong magic square - the 8th Lanqiao provincial competition - group C (DFS and comparison of all magic square types)
随机推荐
Deconstruction function of ES6
Pavlov and hobbies
Range of numbers (dichotomous classic template topic)
AcWing 1096. Detailed notes of Dungeon Master (3D BFS) code
catkin_ What did package do
QSS, qdateedit, qcalendarwidget custom settings
Golang通过exec模块实现Ping连通性检测案例
提升独立站转化率攻略 | 挽回弃购用户
POI exports to excel, and the same row of data is automatically merged into cells
AcWing 836. Merge set (merge set)
STL function library
Breadth first search topics (BFS)
Xiuxian real world and game world
MySQL创建oracle练习表
C, class library
2 - software design principles
实体中list属性为空或者null,设置为空数组
College entrance examination volunteer filling reference
Cmake basic tutorial (39) pkgconfig
qt. qpa. plugin: Could not find the Qt platform plugin “xcb“ in ““