当前位置:网站首页>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();
}
}
边栏推荐
- 0710RHCSA
- ES6数组去重 new Set()
- 【GCN-RS】Learning Explicit User Interest Boundary for Recommendation (WWW‘22)
- 0720RHCSA
- 6.27 uniapp项目历程
- VIM tip: always show line numbers
- 全网最简单解决方式1045-Access denied for user [email protected](using password:YES)
- 详解浮点数的精度问题
- Convolutional neural network model -- alexnet network structure and code implementation
- 并发编程之AQS
猜你喜欢

录制和剪辑视频,如何解决占用空间过大的问题?

The migration of arm architecture to alsa lib and alsa utils is smooth

R language GLM generalized linear model: logistic regression, Poisson regression fitting mouse clinical trial data (dose and response) examples and self-test questions

Connotation and application of industrial Internet

基于百问网IMX6ULL_PRO开发板驱动AP3216实验

Shell common script: check whether a domain name and IP address are connected

【GCN-CTR】DC-GNN: Decoupled GNN for Improving and Accelerating Large-Scale E-commerce Retrieval WWW22

Friends let me see this code

Esp32-c3 is based on blinker lighting control 10 way switch or relay group under Arduino framework

0713RHCSA
随机推荐
基于百问网IMX6ULL_PRO开发板驱动AP3216实验
Hcip day 9 notes
【GCN】《Adaptive Propagation Graph Convolutional Network》(TNNLS 2020)
Detailed explanation of the training and prediction process of deep learning [taking lenet model and cifar10 data set as examples]
Based on Baiwen imx6ull_ Pro development board transplants LCD multi touch driver (gt911)
若依如何实现用户免密登录配置方法?
Leetcode 113. 路径总和 II
外围系统调用SAP的WebAPI接口
Pytorch creates its own dataset and loads the dataset
int数组获取重复数据
ES6 array de duplication new set()
机器学习强基计划0-4:通俗理解奥卡姆剃刀与没有免费午餐定理
0713RHCSA
二叉树基本知识
Excel录制宏
Docker learning - redis cluster -3 master and 3 slave - capacity expansion - capacity reduction building
卷积神经网络模型之——LeNet网络结构与代码实现
Uniapp handles background transfer pictures
Error: cannot find or load main class XXXX
G027-OP-INS-RHEL-04 RedHat OpenStack 创建自定义的QCOW2格式镜像