当前位置:网站首页>How to use handwritten JDBC?
How to use handwritten JDBC?
2022-07-25 13:31:00 【Ziqian 2014】
package cn.zxj.jdbc;
import java.sql.*;
import java.util.Scanner;
public class JdbcDemo {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
statementMethod();// Yes sql Injection risk
preparedStatementMethod();// Can prevent sql Inject
}
private static void preparedStatementMethod() throws ClassNotFoundException, SQLException {
//1、 Registration drive
Class.forName("com.mysql.jdbc.Driver");
//2、 Get the connection
String url = "jdbc:mysql://localhost:3306/jdbc";
String username = "root";
String password = "123";
Connection connection = DriverManager.getConnection(url, username, password);
//3、 Get preprocessing object
//String sql = "insert into users(username) values(?)";
String sql = "select * from users where username = ? and password = ?";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
//4、sql Statement placeholder to set actual parameters
//preparedStatement.setObject(1,"wang");
//sql Injection test
preparedStatement.setObject(1,"'u' or '1=1'");
preparedStatement.setObject(2,"'p' or '1=1'");
//5、 perform sql sentence
//preparedStatement.executeUpdate();
ResultSet resultSet = preparedStatement.executeQuery();
//6、 Processing result set ( Only queries need to process the result set )
while(resultSet.next()){
System.out.println(resultSet.getString(1)+":"+resultSet.getString(2));
}
//7、 close resource
resultSet.close();
preparedStatement.close();
connection.close();
}
private static void statementMethod() throws ClassNotFoundException, SQLException {
//1、 Registration drive
Class.forName("com.mysql.jdbc.Driver");
//2、 Get the connection
String url = "jdbc:mysql://localhost:3306/jdbc";
String username = "root";
String password = "123";
Connection connection = DriverManager.getConnection(url, username, password);
//3、 obtain Statement object
Statement statement = connection.createStatement();
//4、 perform sql
//int i = statement.executeUpdate("insert into users(username) VALUES ('xiaozhou')");
// demonstration sql Inject
Scanner scanner = new Scanner(System.in);
System.out.println(" Please enter a user name ");// Input 'u' or '1=1'
String un = scanner.nextLine();
Scanner scanner1 = new Scanner(System.in);
System.out.println(" Please input a password ");// Input 'p' or '1=1'
String pw = scanner1.nextLine();
String sql = "select * from users where username="+un+" and password="+pw;
System.out.println(sql);
ResultSet resultSet = statement.executeQuery(sql);
//5、 Processing result set ( Only queries need to process the result set )
while(resultSet.next()){
System.out.println(resultSet.getString(1)+":"+resultSet.getString(2));
}
//6、 close resource
resultSet.close();
statement.close();
connection.close();
}
}
边栏推荐
- 为提高效率使用ParallelStream竟出现各种问题
- 并发编程之阻塞队列
- Blindly expanding the scale of the meta universe has deviated from the development logic of the meta universe
- Azure Devops(十四) 使用Azure的私有Nuget仓库
- Arrays常用方法
- 0717RHCSA
- Peripheral system calls SAP's webapi interface
- 面试官问我:Mysql的存储引擎你了解多少?
- mujoco_ Py Chinese document
- Convolutional neural network model -- googlenet network structure and code implementation
猜你喜欢

Shell common script: get the IP address of the network card

面试官问我:Mysql的存储引擎你了解多少?

0710RHCSA

6.27 uniapp project history

Jupyter Notebook介绍
[email protected](using password:YES)"/>全网最简单解决方式1045-Access denied for user [email protected](using password:YES)

基于百问网IMX6ULL_PRO开发板移植LCD多点触摸驱动(GT911)

R语言GLM广义线性模型:逻辑回归、泊松回归拟合小鼠临床试验数据(剂量和反应)示例和自测题

The interviewer asked me: how much do you know about MySQL's storage engine?
[email protected] (using password:YES)"/>The simplest solution of the whole network 1045 access denied for user [email protected] (using password:YES)
随机推荐
How to solve the problem of taking up too much space when recording and editing videos?
MLIR原理与应用技术杂谈
Pycharm cannot input Chinese solution
说说对hashcode和equals方法的理解?
备战2022 CSP-J1 2022 CSP-S1 初赛 视频集
HTTP cache tongtianpian, there may be something you want
【GCN-CTR】DC-GNN: Decoupled GNN for Improving and Accelerating Large-Scale E-commerce Retrieval WWW22
Introduction and features of numpy (I)
G027-OP-INS-RHEL-04 RedHat OpenStack 创建自定义的QCOW2格式镜像
Machine learning strong foundation program 0-4: popular understanding of Occam razor and no free lunch theorem
Prepare for 2022 csp-j1 2022 csp-s1 preliminaries video set
【GCN-RS】Towards Representation Alignment and Uniformity in Collaborative Filtering (KDD‘22)
Basic knowledge of binary tree
Django 2 ----- 数据库与Admin
嵌入式代码如何进行重构?
Hcip eighth day experiment
VIM basic operation summary
Azure Devops(十四) 使用Azure的私有Nuget仓库
mujoco+spinningup进行强化学习训练快速入门
hcip第十天笔记