当前位置:网站首页>手写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();
}
}
边栏推荐
- Common operations for Yum and VIM
- Shell common script: get the IP address of the network card
- Shell常用脚本:获取网卡IP地址
- QingChuang technology joined dragon lizard community to build a new ecosystem of intelligent operation and maintenance platform
- 【OpenCV 例程 300篇】239. Harris 角点检测之精确定位(cornerSubPix)
- 【AI4Code】《Unified Pre-training for Program Understanding and Generation》 NAACL 2021
- 一味地做大元宇宙的规模,已经背离了元宇宙本该有的发展逻辑
- Seven lines of code made station B crash for three hours, but "a scheming 0"
- Mlx90640 infrared thermal imager temperature sensor module development notes (V)
- Mysql 远程连接权限错误1045问题
猜你喜欢

Mlx90640 infrared thermal imager temperature sensor module development notes (V)

Excel添加按键运行宏

B tree and b+ tree

Jupyter Notebook介绍

massCode 一款优秀的开源代码片段管理器
![Detailed explanation of the training and prediction process of deep learning [taking lenet model and cifar10 data set as examples]](/img/70/2b5130be16d7699ef7db58d9065253.png)
Detailed explanation of the training and prediction process of deep learning [taking lenet model and cifar10 data set as examples]

【GCN-RS】Towards Representation Alignment and Uniformity in Collaborative Filtering (KDD‘22)

7行代码让B站崩溃3小时,竟因“一个诡计多端的0”

卷积神经网络模型之——AlexNet网络结构与代码实现
详解浮点数的精度问题
随机推荐
Masscode is an excellent open source code fragment manager
[Video] Markov chain Monte Carlo method MCMC principle and R language implementation | data sharing
【GCN-RS】Towards Representation Alignment and Uniformity in Collaborative Filtering (KDD‘22)
[Video] visual interpretation of Markov chain principle and Mrs example of R language region conversion | data sharing
Convolutional neural network model -- lenet network structure and code implementation
录制和剪辑视频,如何解决占用空间过大的问题?
二叉树基本知识
全网最简单解决方式1045-Access denied for user [email protected](using password:YES)
MLX90640 红外热成像仪测温传感器模块开发笔记(五)
Docekr learning - MySQL 8 master-slave replication setup deployment
Simple understanding of flow
0713RHCSA
Vim技巧:永远显示行号
Common operations for Yum and VIM
[operation and maintenance, implementation of high-quality products] interview skills for technical positions with a monthly salary of 10k+
conda常用命令:安装,更新,创建,激活,关闭,查看,卸载,删除,清理,重命名,换源,问题
pytorch创建自己的Dataset加载数据集
Esp32-c3 is based on blinker lighting control 10 way switch or relay group under Arduino framework
Azure Devops(十四) 使用Azure的私有Nuget仓库
【CSDN 年终总结】结束与开始,一直在路上—— “1+1=王”的2021总结