当前位置:网站首页>Introduction of JDBC preparestatement+ database connection pool
Introduction of JDBC preparestatement+ database connection pool
2022-07-29 04:54:00 【Chen Yuchen】
jdbc-PrepareStatement
- PrepareStatement The role of
precompile SQL Statement and execute ; The prevention of SQL Injection problem
- SQL Description of injection :
sql Injection is to modify the pre-defined by operation input SQL sentence , The method used to execute code to attack the server
For example, when we log in : Need to enter account number and password , When we enter the password ‘or’1’='1 It's going to produce sql Inject , Look at the analysis below :
@Test
public void testLogin() throws Exception {
//2. Get the connection : If the connection is local mysql And the port is the default 3306 Can simplify writing
String url = "jdbc:mysql:///db1?useSSL=false";
String username = "root";
String password = "1234";
Connection conn = DriverManager.getConnection(url, username, password);
// Receive user input User name and password
String name = "sjdljfld";
String pwd = "' or '1' = '1";
String sql = "select * from tb_user where username = '"+name+"' and password = '"+pwd+"'";
// obtain stmt object
Statement stmt = conn.createStatement();
// perform sql
ResultSet rs = stmt.executeQuery(sql);
// Judge whether the login is successful
if(rs.next()){
System.out.println(" Login successful ~");
}else{
System.out.println(" Login failed ~");
}
//7. Release resources
rs.close();
stmt.close();
conn.close();
}
Look at this statement :“select * from tb_user where username = '”+name+“’ and password = '”+pwd+“'”;
Write the account password we entered into ( Random account , Passwords will be generated sql Inject ): password = ’ ‘or’1’='1 ’ ==>
password = ’ ‘or’1’=‘1’ ; ‘’ => false or ‘1’=‘1’ really So the result is true , So even if the input is not the real password , Finally, login can be realized .
Now we use ?( Place holder ) To prevent sql Inject :
// SQL Parameter value in statement , Use ? Placeholder replacement
String sql = "select * from user where username = ? and password = ?";
// adopt Connection Object acquisition , And pass in the corresponding sql sentence
PreparedStatement pstmt = conn.prepareStatement(sql);
We go through PrepareStatement Object's setXxx() Method to set parameters ? Value , as follows :
// Receive user input User name and password
String name = "zhangsan";
String pwd = "' or '1' = '1";
// Definition sql
String sql = "select * from tb_user where username = ? and password = ?";
// obtain pstmt object
PreparedStatement pstmt = conn.prepareStatement(sql);
// Set up ? Value
pstmt.setString(1,name);
pstmt.setString(2,pwd);
// perform sql
ResultSet rs = pstmt.executeQuery();
// Judge whether the login is successful
if(rs.next()){
System.out.println(" Login successful ~");
}else{
System.out.println(" Login failed ~");
}
I'm curious why it doesn't happen sql Inject ?PrepareStatement How is it realized ?
PrepareStatement Is the escape of special characters , Escaped sql as follows :
select * from tb_user where username = 'sjdljfld' and password = '\'or \'1\' = \'1'
PrepareStatement The advantages of :
precompile SQL, Higher performance
prevent SQL Inject : Escape sensitive characters
java The flow of code operation database is shown in the figure :
Turn on the precompile function :
Write... In code url The following parameters need to be added when . And we didn't turn on the precompile function before , Knowledge solves sql Inject holes
useServerPrepstmts=true
To configure MYSQL Execution log ( restart mysql Effective after service )
- stay mysql The configuration file (my.ini) Add the following configuration
log-output=FILE
general-log=1
general_log_file="D:\mysql.log"
slow-query-log=1
slow_query_log_file="D:\mysql_slow.log"
long_query_time=2
Summary
- In obtaining PreparedStatement Object time , take sql The statement is sent to mysql The server checks , compile ( These steps are time-consuming )
- You don't have to do these steps when executing , Faster
- If sql It's like a template , You only need to check once 、 compile
4, Database connection pool
Database connection pool is a container , To be responsible for the distribution of 、 Manage database connections (Connection)
It allows applications to reuse an existing database connection , Instead of building a new ;
Release database connection with idle time exceeding the maximum idle time to avoid missing database connection caused by no free database connection
benefits
Resource reuse
Improve system response speed
Avoid missing database connections
Database connection pool implementation
Standard interface :DataSource
official (SUN) Database connection pool standard interface provided , This interface is implemented by a third-party organization . This interface provides the function of obtaining connection :
Connection getConnection()Then you don't need to pass
DriverManagerObject acquisitionConnectionobject , But through the connection pool (DataSource) obtainConnectionobject .Common database connection pool
- DBCP
- C3P0
- Druid
Now we use more Druid, Its performance will be better than the other two .
Druid( Druid )
Druid Connection pool is an open source database connection pool project of Alibaba
Powerful , Excellent performance , yes Java One of the best database connection pools in language
边栏推荐
- Reveal安装配置调试
- UE plays video in scene or UMG
- Command line interactive tools (latest version) inquirer practical tutorial
- 软件测试面试题(四)
- On prepayment of house purchase
- How is the entered query SQL statement executed?
- Spark的算子操作列表
- 学术 | [LaTex]超详细Texlive2022+Tex Studio下载安装配置
- Conv2d of torch
- img 响应式图片的实现(含srcset属性、sizes属性的使用方法,设备像素比详解)
猜你喜欢

office2010每次打开都要配置进度怎么解决?

2022杭电多校联赛第四场 题解

Torch.nn.crossentropyloss() details

MySQL regularly calls preset functions to complete data update

GCC Basics

Take you to understand JS array

【无标题】

Auto.js脚本开发环境搭建

Solution to the fourth game of 2022 Hangzhou Electric Multi school league

After the spinning up installation is completed, use the tutorial to test whether it is successful. There are library "Glu" not found and 'from pyglet.gl import * error solutions
随机推荐
数据湖:分布式开源处理引擎Spark
Vivo market API event reporting and docking
Basic grammar of C language
office2010每次打开都要配置进度怎么解决?
Big silent event Google browser has no title
Reveal installation configuration debugging
谷歌浏览器 打开网页出现 out of memory
IOS interview preparation - IOS
ios面试准备 - 网络篇
A little knowledge about management
如何安装office2010安装包?office2010安装包安装到电脑上的方法
Flink+iceberg environment construction and production problem handling
学术 | [LaTex]超详细Texlive2022+Tex Studio下载安装配置
Recyclerview switches the focus up and down through the dpad key. When switching to the control outside the interface, the focus will jump left and right
How to avoid damage of oscilloscope current probe
荣耀2023内推,内推码ambubk
IOS interview preparation - Objective-C
Conv1d of torch
stack和queue和优先级队列(大堆和小堆)模拟实现和仿函数讲解
Box horizontal vertical center layout (summary)