当前位置:网站首页>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
边栏推荐
- In depth understanding of the relationship between dncblevel and noise denoising in the paper
- 线性规划问题中可行解,基本解和基本可行解有什么区别?
- Latex快速入门
- 自动控制(韩敏版)
- PyQt5学习(一):布局管理+信号和槽关联+菜单栏与工具栏+打包资源包
- Programming record - picture rotation function SciPy ndimage. Simple use and effect observation of rotate()
- Create enterprise mailbox account command
- 数据处理之Numpy常用函数表格整理
- 深度学习基础——简单了解meta learning(来自李宏毅课程笔记)
- EditorConfig
猜你喜欢

Anaconda安装PyQt5 和 pyqt5-tools后没有出现designer.exe的问题解决

Fundamentals of in-depth learning -- a simple understanding of meta learning (from Li Hongyi's course notes)

数字图像处理基础(冈萨雷斯)二:灰度变换与空间滤波

创建二叉树
![Paper on Image Restoration - [red net, nips16] image restoration using very deep revolutionary encoder decoder networks wi](/img/1b/4eea05e2634780f45b44273d2764e3.png)
Paper on Image Restoration - [red net, nips16] image restoration using very deep revolutionary encoder decoder networks wi

Pytorch learning record (V): back propagation + gradient based optimizer (SGD, adagrad, rmsporp, Adam)

Font shape `OMX/cmex/m/n‘ in size <10.53937> not available (Font) size <10.95> substituted.

线性代数第二章-矩阵及其运算

You cannot access this shared folder because your organization's security policy prevents unauthenticated guests from accessing it

建表到页面完整实例演示—联表查询
随机推荐
Fundamentals of digital image processing (Gonzalez) I
filebrowser实现私有网盘
Latex quick start
图解numpy数组矩阵
Comparative study paper - [Moco, cvpr2020] momentum contract for unsupervised visual representation learning
Anaconda安装PyQt5 和 pyqt5-tools后没有出现designer.exe的问题解决
Pytorch学习记录(十):数据预处理+Batch Normalization批处理(BN)
Fundamentals of SQL: first knowledge of database and SQL - installation and basic introduction - Alibaba cloud Tianchi
Anaconda installed pyqt5 and pyqt5 tools without designer Exe problem solving
JSP语法及JSTL标签
Chapter 4 of line generation - linear correlation of vector systems
Remedy after postfix becomes a spam transit station
Pytorch learning record (III): structure of neural network + using sequential and module to define the model
事实最终变量与最终变量
Pytorch learning record (XI): data enhancement, torchvision Explanation of various functions of transforms
Contrôle automatique (version Han min)
SQL基础:初识数据库与SQL-安装与基本介绍等—阿里云天池
PyTorch入门小笔记——利用简单例子观察前向传播各个层输出的size
Solve the error: importerror: iprogress not found Please update jupyter and ipywidgets
Pytorch——数据加载和处理