当前位置:网站首页>JDBC tool class encapsulation
JDBC tool class encapsulation
2022-04-23 06:00:00 【hanyc..】
jdbc Encapsulation of tool class :
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、 Get user information 、url
url = properties.getProperty("url");
username = properties.getProperty("username");
psw = properties.getProperty("psw");
// The driver only loads once
//1、 The load driver
Class.forName(driver);
} catch (Exception e) {
e.printStackTrace();
}
}
// Get the connection
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, username, psw);
}
// Release the connection
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();
}
}
}
}
Insert data test :
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、 Get database connection
connection = JDBCUtils.getConnection();
//4、 Create execution sql The object of
statement = connection.createStatement();
//5、 perform sql sentence
//6、 Return execution result set
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(" Insert data succeeded !");
}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
//7、 Release the connection
JDBCUtils.release(connection, statement, resultSet);
}
}
}
Delete data test :
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、 Get database connection
connection = JDBCUtils.getConnection();
//4、 Create execution sql The object of
statement = connection.createStatement();
//5、 perform sql sentence
//6、 Return execution result set
String sql = "DELETE FROM `users` WHERE id=4";
int num = statement.executeUpdate(sql);
if (num > 0) {
System.out.println(" Delete data succeeded !");
}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
//7、 Release the connection
JDBCUtils.release(connection, statement, resultSet);
}
}
}
Modify data test :
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、 Get database connection
connection = JDBCUtils.getConnection();
//4、 Create execution sql The object of
statement = connection.createStatement();
//5、 perform sql sentence
//6、 Return execution result set
String sql = "UPDATE `users` SET `NAME`='zxzb',`email`='[email protected]' WHERE `id`=1";
int num = statement.executeUpdate(sql);
if (num > 0) {
System.out.println(" Update data successful ");
}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
//7、 Release the connection
JDBCUtils.release(connection, statement, resultSet);
}
}
}
Query data test :
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、 Get database connection
connection = JDBCUtils.getConnection();
//4、 Create execution sql The object of
statement = connection.createStatement();
//5、 perform sql sentence
//6、 Return execution result set
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、 Release the connection
JDBCUtils.release(connection, statement, resultSet);
}
}
}
版权声明
本文为[hanyc..]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230541160277.html
边栏推荐
- Pyqy5 learning (2): qmainwindow + QWidget + qlabel
- Meta annotation (annotation of annotation)
- protected( 被 protected 修饰的成员对于本包和其子类可见)
- 线性代数第二章-矩阵及其运算
- Pytorch notes - observe dataloader & build lenet with torch to process cifar-10 complete code
- Multithreading and high concurrency (2) -- detailed explanation of synchronized usage
- Pyqy5 learning (III): qlineedit + qtextedit
- Anaconda
- redhat实现目录下特定文本类型内关键字查找及vim模式下关键字查找
- Pytoch learning record (x): data preprocessing + batch normalization (BN)
猜你喜欢
In depth source code analysis servlet first program
Pytorch学习记录(十二):学习率衰减+正则化
filebrowser实现私有网盘
数字图像处理基础(冈萨雷斯)一
Configure domestic image accelerator for yarn
Graphic numpy array matrix
PyTorch笔记——观察DataLoader&用torch构建LeNet处理CIFAR-10完整代码
Pytoch -- data loading and processing
Multithreading and high concurrency (2) -- detailed explanation of synchronized usage
Comparative study paper - [Moco, cvpr2020] momentum contract for unsupervised visual representation learning
随机推荐
Create enterprise mailbox account command
JSP语法及JSTL标签
PyEMD安装及简单使用
Font shape `OMX/cmex/m/n‘ in size <10.53937> not available (Font) size <10.95> substituted.
Anaconda安装PyQt5 和 pyqt5-tools后没有出现designer.exe的问题解决
创建线程的三种方式
线性规划问题中可行解,基本解和基本可行解有什么区别?
Practical operation - Nacos installation and configuration
线性代数第二章-矩阵及其运算
PyTorch入门小笔记——利用简单例子观察前向传播各个层输出的size
Chapter 4 of line generation - linear correlation of vector systems
实操—Nacos安装与配置
Filebrowser realizes private network disk
CONDA virtual environment management (create, delete, clone, rename, export and import)
线性代数第一章-行列式
自定义异常类
PyQt5学习(一):布局管理+信号和槽关联+菜单栏与工具栏+打包资源包
Opensips (1) -- detailed process of installing opensips
Anaconda installed pyqt5 and pyqt5 tools without designer Exe problem solving
Postfix变成垃圾邮件中转站后的补救