当前位置:网站首页>Preparedstatement prevents SQL injection
Preparedstatement prevents SQL injection
2022-04-23 06:00:00 【hanyc..】
Add data :
package com.hyc.study03;
import com.hyc.study02.utils.JDBCUtils;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class TestInsert {
public static void main(String[] args) {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
//3、 Get database connection
connection = JDBCUtils.getConnection();
// Use ? Placeholders replace parameters
String sql = "INSERT INTO `users`(`id`,`NAME`,`PASSWORD`,`email`,`birthday`) " +
"VALUES (?,?,?,?,?);";
//4、 Access to perform sql The object of
// precompile sql, First write sql, Then don't execute
preparedStatement = connection.prepareStatement(sql);
// Assign parameters manually
preparedStatement.setInt(1, 4);
preparedStatement.setString(2, "hyc");
preparedStatement.setString(3, "123456");
preparedStatement.setString(4, "[email protected]");
//java.sql.Date It's database Date , Only the date information is included , It is java.util.Date( Contains the information of month, day, hour, minute and second ) Subclasses of
preparedStatement.setDate(5, new java.sql.Date(new Date().getTime()));
//5、 perform sql sentence
//6、 Return execution result set
int num = preparedStatement.executeUpdate();
if (num > 0) {
System.out.println(" Insert data succeeded !");
}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
//7、 Release the connection
JDBCUtils.release(connection, preparedStatement, resultSet);
}
}
}
Delete data :
package com.hyc.study03;
import com.hyc.study02.utils.JDBCUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class TestDelete {
public static void main(String[] args) {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
//3、 Get database connection
connection = JDBCUtils.getConnection();
// Use ? Placeholders replace parameters
String sql = "DELETE FROM `users` WHERE id=?";
//4、 Access to perform sql The object of
// precompile
preparedStatement = connection.prepareStatement(sql);
// Assign parameters manually
preparedStatement.setInt(1, 4);
//5、 perform sql sentence
//6、 Return execution result set
int num = preparedStatement.executeUpdate();
if (num > 0) {
System.out.println(" Delete data succeeded !");
}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
//7、 Release the connection
JDBCUtils.release(connection, preparedStatement, resultSet);
}
}
}
Modifying data :
package com.hyc.study03;
import com.hyc.study02.utils.JDBCUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class TestUpdate {
public static void main(String[] args) {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
//3、 Get database connection
connection = JDBCUtils.getConnection();
// Use ? Placeholders replace parameters
String sql = "UPDATE `users` SET `NAME`=?,`email`=? WHERE `id`=?";
//4、 Access to perform sql The object of
// precompile
preparedStatement = connection.prepareStatement(sql);
// Assign parameters manually
preparedStatement.setString(1, "zhangsan");
preparedStatement.setString(2, "[email protected]");
preparedStatement.setInt(3, 1);
//5、 perform sql sentence
//6、 Return execution result set
int num = preparedStatement.executeUpdate();
if (num > 0) {
System.out.println(" Update data successful !");
}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
//7、 Release the connection
JDBCUtils.release(connection, preparedStatement, resultSet);
}
}
}
Query data :
package com.hyc.study03;
import com.hyc.study02.utils.JDBCUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class TestSelect {
public static void main(String[] args) {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
//3、 Get database connection
connection = JDBCUtils.getConnection();
// Use ? Placeholders replace parameters
String sql = "SELECT * FROM `users` WHERE id=?";
//4、 Access to perform sql The object of
// precompile
preparedStatement = connection.prepareStatement(sql);
// Assign parameters manually
preparedStatement.setInt(1, 1);
//5、 perform sql sentence
//6、 Return execution result set
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
System.out.println(resultSet.getString("NAME"));
}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
//7、 Release the connection
JDBCUtils.release(connection, preparedStatement, resultSet);
}
}
}
PreparedStatement prevent SQL Inject :
PreparedStatement prevent SQL The essence of injection is to treat the parameters passed in as characters ( I want to wrap the passed parameters into a new string ), If there are escape characters , for example ‘ Will be escaped directly . This can avoid string splicing into illegal sql sentence , Cause data leakage .
package com.hyc.study03;
import com.hyc.study02.utils.JDBCUtils;
import java.sql.*;
public class PourIntoSql {
public static void main(String[] args) {
login("'' or '1=1", "123456");
}
public static void login(String username, String psw) {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
connection = JDBCUtils.getConnection();
String sql = "SELECT * FROM `users` WHERE `NAME`=? AND `PASSWORD`=?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, username);
preparedStatement.setString(2, psw);
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
System.out.println(resultSet.getString("NAME"));
System.out.println("===========================================");
}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
JDBCUtils.release(connection, preparedStatement, resultSet);
}
}
}
result :
stay PreparedStatement Under the influence of , Then try to achieve... Through string splicing SQL The purpose of injection cannot be achieved .
版权声明
本文为[hanyc..]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230541159555.html
边栏推荐
- Pytorch学习记录(四):参数初始化
- Linear algebra Chapter 2 - matrices and their operations
- Remedy after postfix becomes a spam transit station
- protected( 被 protected 修饰的成员对于本包和其子类可见)
- PyQt5学习(一):布局管理+信号和槽关联+菜单栏与工具栏+打包资源包
- Use Matplotlib. In Jupiter notebook Pyplot server hangs up and crashes
- Linear algebra Chapter 1 - determinant
- Pytorch learning record (IV): parameter initialization
- Pytorch学习记录(十一):数据增强、torchvision.transforms各函数讲解
- 线性代数第三章-矩阵的初等变换与线性方程组
猜你喜欢
Development environment EAS login license modification
无监督去噪——[TMI2022]ISCL: Interdependent Self-Cooperative Learning for Unpaired Image Denoising
Linear algebra Chapter 1 - determinant
Implementation of displaying database pictures to browser tables based on thymeleaf
深度学习基础——简单了解meta learning(来自李宏毅课程笔记)
深入理解去噪论文——FFDNet和CBDNet中noise level与噪声方差之间的关系探索
Opensips (1) -- detailed process of installing opensips
实操—Nacos安装与配置
基于thymeleaf实现数据库图片展示到浏览器表格
Pytorch learning record (7): skills in processing data and training models
随机推荐
DBCP使用
开发环境 EAS登录 license 许可修改
PyQy5学习(二):QMainWindow+QWidget+QLabel
CONDA virtual environment management (create, delete, clone, rename, export and import)
关于二叉树的遍历
Unsupervised denoising - [tmi2022] ISCL: dependent self cooperative learning for unpaired image denoising
SQL注入
异常的处理:抓抛模型
Implementation of displaying database pictures to browser tables based on thymeleaf
Pytorch学习记录(七):处理数据和训练模型的技巧
Viewer: introduce MySQL date function
sklearn之 Gaussian Processes
Configure domestic image accelerator for yarn
container
Multithreading and high concurrency (2) -- detailed explanation of synchronized usage
数字图像处理基础(冈萨雷斯)一
umi官网yarn create @umijs/umi-app 报错:文件名、目录名或卷标语法不正确
数字图像处理基础(冈萨雷斯)二:灰度变换与空间滤波
How to use comparative learning to do unsupervised - [cvpr22] training & [eccv20] image translation
PreparedStatement防止SQL注入