当前位置:网站首页>Encapsulated JDBC tools
Encapsulated JDBC tools
2022-04-21 23:51:00 【RhineHe_ hjs】
encapsulation JDBC
For the convenience of adding and modifying the database , You need to encapsulate the method of connecting to the database , In this way, it will be more convenient for us to develop . Write less code , Do more . Next, I will show how to operate the database with the encapsulated tool
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;
/** * @author Rhine_He * @date 2020-06-24-15:23 * @fuction JDBC encapsulation **/
public class DBUtilUseProperties {
private static final Properties pro = new Properties();
static Connection connection =null;
static PreparedStatement preparedStatement =null;
static ResultSet resultSet=null;
static {
InputStream is = DBUtilUseProperties.class.getResourceAsStream("db.propertise");
try {
pro.load(is);
Class.forName(pro.getProperty("driver"));
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection(){
try {
connection = DriverManager.getConnection(pro.getProperty("url"),
pro.getProperty("user"),pro.getProperty("password"));
return connection;
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
}
test
import bean.User;
import jdbcPool.JDBCPool;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.util.List;
/** * @author Rhine_He * @date 2020-06-24-15:37 **/
public class JDBCDemoUseUtil2 {
public static void main(String[] args) throws Exception {
/* * Use garbage packaging JDBC operation : */
/* Connection connection = DBUtilUseProperties.getConnection(); *//* // This method is slow , There is still SQL The danger of Injection Statement statement = connection.createStatement(); int result = statement.executeUpdate("insert into user(userid,username,sex)values(5,' Sun Qi ',' male ')");*//* // The following methods can effectively avoid SQL The risk of Injection , To compile the first , Post run String sql="insert into user(userid,username,sex)values(2,' Li Si ',' male ')"; PreparedStatement statement = connection.prepareStatement(sql); int result =statement.executeUpdate(); if(result != -1){ System.out.println(" Registered successfully !"); }else { System.out.println(" Registration failed !"); } DBUtilUseProperties.closeAll(connection,statement,null);*/
/* * Use good packaging JDBC, You just need to use the encapsulated class to call the method , Pass on SQL And parameters , * If it is a query, you also need to pass the class of the returned object, such as student object :(Student.class) Premise , Write it yourself Student Entity class */
// Modifying data : This method can modify and delete database data
DBUtilUseProperties.update("delete from user where userid>?",5);// Delete
DBUtilUseProperties.update("update user set username=? where userid=? "," Zhang SA ",2);// modify
// Query data
User user = DBUtilUseProperties.queryOne("select *from user where userid=?",User.class,4);// Query a piece of data
System.out.println(user);
// Query multiple data
List<User> users = DBUtilUseProperties.queryList("select *from user", User.class, null);
for (Object object : users) {
System.out.println(object);
}
}
}
版权声明
本文为[RhineHe_ hjs]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204211801130177.html
边栏推荐
- Leetcode daily question 824 Goat Latin
- wireshark区别包的情况
- Two benchmark performance test methods of redis
- C dual insurance process monitor lol ensures that the monitored program runs' almost 'forever Keywords: process operation process view create process
- How does catboost handle non numeric (object) data
- 架构实战训练 模块三
- 已有千人收藏,一起“干完”这份300页1000道面试题
- Classified summary of series articles (second issue)
- 341 Linux connection database
- [source code] super detailed interpretation of linkedblockingqueue source code
猜你喜欢

7.6 thread properties

Necessary skills for large factory interview, Android audio and video framework

340-Leetcode 有效的字母异位词

Man machine verification reCAPTCHA V3 complete instructions

leetcode:386. Dictionary order

"National tide" building blocks, the next answer to tide play?

Multi table view creation problem: modify view data times 1062

已拿offer,面试必备知识点

机器学习,深度学习,神经网络,深度神经网络之间有何区别?
![[source code] super detailed interpretation of linkedblockingqueue source code](/img/e0/e55aa577e4ff30d5924aa549093231.png)
[source code] super detailed interpretation of linkedblockingqueue source code
随机推荐
SolidWorks hold down Ctrl and drag to copy entities
Buuctf question brushing record
Administrator login and token related
7.6 thread properties
Find the first prime number greater than x (day 42)
Classified summary of series articles (second issue)
FreeFileSync的使用教程
The longitude and latitude tables of major cities across the country are kept for future reference
7.4 thread exit
Cuda02 - memory access optimization and unified memory
leetcode:440. The k-th smallest digit in dictionary order
Electronic address book management system based on C
多表创建视图问题:修改视图数据时报1062
AVL tree deletion, detailed illustration
[transfer] differences and relations between collection set map vector list
《数字电子技术基础》3.3 CMOS门电路(下)
"Yushu technology" completed hundreds of millions of yuan of Series B financing and accelerated the large-scale commercial landing of quadruped robots
How to set the motion stroke and angle in SolidWorks
Leetcode每日一题824. 山羊拉丁文
L2-016 愿天下有情人都是失散多年的兄妹 (25 分)