当前位置:网站首页>手写jdbc的使用步骤?
手写jdbc的使用步骤?
2022-07-25 13:11:00 【紫乾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();//有sql注入风险
preparedStatementMethod();//能够防止sql注入
}
private static void preparedStatementMethod() throws ClassNotFoundException, SQLException {
//1、注册驱动
Class.forName("com.mysql.jdbc.Driver");
//2、获取连接
String url = "jdbc:mysql://localhost:3306/jdbc";
String username = "root";
String password = "123";
Connection connection = DriverManager.getConnection(url, username, password);
//3、获取预处理对象
//String sql = "insert into users(username) values(?)";
String sql = "select * from users where username = ? and password = ?";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
//4、sql语句占位符设置实际参数
//preparedStatement.setObject(1,"wang");
//sql注入测试
preparedStatement.setObject(1,"'u' or '1=1'");
preparedStatement.setObject(2,"'p' or '1=1'");
//5、执行sql语句
//preparedStatement.executeUpdate();
ResultSet resultSet = preparedStatement.executeQuery();
//6、处理结果集(查询才需要处理结果集)
while(resultSet.next()){
System.out.println(resultSet.getString(1)+":"+resultSet.getString(2));
}
//7、关闭资源
resultSet.close();
preparedStatement.close();
connection.close();
}
private static void statementMethod() throws ClassNotFoundException, SQLException {
//1、注册驱动
Class.forName("com.mysql.jdbc.Driver");
//2、获取连接
String url = "jdbc:mysql://localhost:3306/jdbc";
String username = "root";
String password = "123";
Connection connection = DriverManager.getConnection(url, username, password);
//3、获取Statement对象
Statement statement = connection.createStatement();
//4、执行sql
//int i = statement.executeUpdate("insert into users(username) VALUES ('xiaozhou')");
//演示sql注入
Scanner scanner = new Scanner(System.in);
System.out.println("请输入用户名");//输入'u' or '1=1'
String un = scanner.nextLine();
Scanner scanner1 = new Scanner(System.in);
System.out.println("请输入密码");//输入'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、处理结果集(查询才需要处理结果集)
while(resultSet.next()){
System.out.println(resultSet.getString(1)+":"+resultSet.getString(2));
}
//6、关闭资源
resultSet.close();
statement.close();
connection.close();
}
}
边栏推荐
- 外围系统调用SAP的WebAPI接口
- cv2.resize函数报错:error: (-215:Assertion failed) func != 0 in function ‘cv::hal::resize‘
- Date and time function of MySQL function summary
- Cyberspace Security penetration attack and defense 9 (PKI)
- 牛客论坛项目部署总结
- 详解浮点数的精度问题
- 错误: 找不到或无法加载主类 xxxx
- Convolutional neural network model -- googlenet network structure and code implementation
- 【GCN-RS】Learning Explicit User Interest Boundary for Recommendation (WWW‘22)
- 工业互联网的内涵及其应用
猜你喜欢

arm架构移植alsa-lib和alsa-utils一路畅通

Shell common script: judge whether the file of the remote host exists
TCP的拥塞控制
![[figure attack and Defense] backdoor attacks to graph neural networks (sacmat '21)](/img/d2/6be99fd194c66e4f60af38c6e52c93.png)
[figure attack and Defense] backdoor attacks to graph neural networks (sacmat '21)

【AI4Code】《IntelliCode Compose: Code Generation using Transformer》 ESEC/FSE 2020

Make a general cascade dictionary selection control based on jeecg -dictcascadeuniversal

Atcoder beginer contest 261e / / bitwise thinking + DP

Docker学习 - Redis集群-3主3从-扩容-缩容搭建

从输入网址到网页显示
![[Video] visual interpretation of Markov chain principle and Mrs example of R language region conversion | data sharing](/img/6e/9e0abf8db5ec93080033bd89605ac2.jpg)
[Video] visual interpretation of Markov chain principle and Mrs example of R language region conversion | data sharing
随机推荐
【CTR】《Towards Universal Sequence Representation Learning for Recommender Systems》 (KDD‘22)
Microsoft proposed CodeT: a new SOTA for code generation, with 20 points of performance improvement
备战2022 CSP-J1 2022 CSP-S1 初赛 视频集
The programmer's father made his own AI breast feeding detector to predict that the baby is hungry and not let the crying affect his wife's sleep
AtCoder Beginner Contest 261 F // 树状数组
C#基础学习(二十三)_窗体与事件
6W+字记录实验全过程 | 探索Alluxio经济化数据存储策略
Generate SQL script file by initializing the latest warehousing time of vehicle attributes
卷积神经网络模型之——AlexNet网络结构与代码实现
Azure Devops(十四) 使用Azure的私有Nuget仓库
【GCN-RS】Towards Representation Alignment and Uniformity in Collaborative Filtering (KDD‘22)
Concurrent programming - memory model JMM
arm架构移植alsa-lib和alsa-utils一路畅通
JS sorts according to the attributes of the elements in the array
Simple understanding of flow
基于百问网IMX6ULL_PRO开发板移植LCD多点触摸驱动(GT911)
基于百问网IMX6ULL_PRO开发板驱动AP3216实验
Brpc source code analysis (III) -- the mechanism of requesting other servers and writing data to sockets
牛客论坛项目部署总结
Substance Designer 2021软件安装包下载及安装教程