当前位置:网站首页>jdbc的增删改查
jdbc的增删改查
2022-07-24 05:15:00 【王九九】
1.查询数据库表中记录。
@Test //理解为main函数。可以独立运行。
public void testQuery() throws Exception {
//抛出异常只是为了操作方便。真正在开发时应该try--catch
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn = DriverManager.getConnection
("jdbc:mysql://localhost:3306/mydb?serverTimezone=Asia/Shanghai",
"root", "root");
Statement statement = conn.createStatement();
String sql = "select id,name,age,address from t_student";
//执行查询sql语句 并把数据库表中记录返回到ResultSet对象中进行保存。
ResultSet rs = statement.executeQuery(sql);
//取出ResultSet中表的记录。rs.next() 判断指针是否可以移动。如果可以移动则返回true,否则返回false
while (rs.next()) {
int id = rs.getInt("id"); //指针移动并获取指定列的值。
String name = rs.getString("name");
String address=rs.getString("address");
int age=rs.getInt("age");
System.out.println("id:"+id+";name:"+name+";age:"+age+";address:"+address);
}
}
2.根据条件查询
//根据条件查询数据库
@Test
public void testQueryByCondition() throws Exception{
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn = DriverManager.getConnection
("jdbc:mysql://localhost:3306/mydb?serverTimezone=Asia/Shanghai", "root", "root");
Statement statement = conn.createStatement();
String sql="select * from t_student where id=3";
ResultSet rs = statement.executeQuery(sql);
while (rs.next()){
int id = rs.getInt("id"); //指针移动并获取指定列的值。
String name = rs.getString("name");
String address=rs.getString("address");
int age=rs.getInt("age");
System.out.println("id:"+id+";name:"+name+";age:"+age+";address:"+address);
}
}增删改改变一下SQL语句
3.sql注入安全问题
演示sql注入的安全问题:
//演示sql注入的安全问题
public static void main(String [] args) throws Exception{
Scanner scanner=new Scanner(System.in); //Scanner类有没有讲过。
System.out.print("请输入账号:");
String username = scanner.nextLine();
System.out.print("请输入密码:");
String password = scanner.nextLine(); //你输入的账号和密码 nextLine() 可以输入空格 回车任认为结束 next()输入空格后认为输入结束。
boolean b = sqlSafe(username, password);
}
根据name查询数据 abc 演示的根据账号和密码查询数据库表记录 如果能查询表示登录成功 否则登录失败
private static boolean sqlSafe(String name,String password) throws Exception{
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn = DriverManager.getConnection
("jdbc:mysql://localhost:3306/mydb?serverTimezone=Asia/Shanghai", "root", "root");
Statement statement = conn.createStatement();
//这里的admin 是不是一个死数据 123456 也是一个死数据
String sql="select * from t_user where username='"+name+"' and password='"+password+"'";
System.out.println(sql);
ResultSet rs = statement.executeQuery(sql);
while (rs.next()){
System.out.println("登录成功");
return true;
}
System.out.println("登录失败");
return false;
}可以发现: 你的账号可以随便输入 你的密码也可以随便输入 但是 在输入密码时 or '4'='4 只要这个条件成立,那么你就能登录成功。 这个就是sql注入的安全问题。只要根据条件做sql。那么就会出现sql注入安全问题。
如何解决sql安全注入问题:
1. 前端做校验: --只防君子 防不了小人。
2. 后端也做校验:--难道以后每次写功能都进行校验吗? 代码变得复杂了。
3. 执行sql的类Statement出现了问题,后期PrepareStatement该类来解决sql注入安全问题。
Statement和PrepareStatement区别?
1. Statement会出现sql注入安全问题。Preparestatement不会出现sql注入安全问题。
2. Preparestatement是Statement的子类。就是因为早期使用Statement发现该类出现问题,后期维护人员创建Statement的子类来解决这个问题。注意:维护人员不会再原类上做维护,
4.使用prepareStatement来解决sql注入的问题。
//演示sql注入的安全问题
public static void main(String [] args) throws Exception{
Scanner scanner=new Scanner(System.in); //Scanner类有没有讲过。
System.out.print("请输入账号:");
String username = scanner.nextLine();
System.out.print("请输入密码:");
String password = scanner.nextLine(); //你输入的账号和密码 nextLine() 可以输入空格 回车任认为结束 next()输入空格后认为输入结束。
boolean b = sqlSafe02(username, password);
}
private static boolean sqlSafe02(String name,String password) throws Exception{
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn = DriverManager.getConnection
("jdbc:mysql://localhost:3306/mydb?serverTimezone=Asia/Shanghai", "root", "root");
//使用PrepareStatement 这里的? 是占位符。
String sql="select * from t_user where username=? and password=?";
PreparedStatement ps = conn.prepareStatement(sql);//预编译sql
//为占位符赋值。根据占位符的类型使用不同的方法来赋值
ps.setString(1,name); //1表示第一个占位符 name:表示第一个占位符的值
ps.setString(2,password);
//执行sql语句
ResultSet rs = ps.executeQuery();
while (rs.next()){
System.out.println("登录成功");
return true;
}
System.out.println("登录失败");
return false;
}PrepareStatement和Statement这两个类在代码上的区别?
以后都使用PreparedStatement这个类。
表示没有为占位符赋值。
完成增删改查--使用PreparedStatement
@Test public void testDelete() throws Exception {
Class.forName("com.mysql.cj.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/mydb?serverTimezone=Asia/Shanghai";
String user="root";
String pwd="root";
Connection connection = DriverManager.getConnection(url,user,pwd);
String sql="delete from t_student where id=?";
PreparedStatement ps = connection.prepareStatement(sql); //为占位符赋值.
int id=5; //未来应该是传递过来的
ps.setObject(1,id); //数据库中如果是其他类型 也可以使用''
//使用setObject
//执行sql语句 executeUpdate方法
ps.executeUpdate();
}作业:
使用PrepararedStatement完成对某张表的增删改查操作。每个练习5遍。
边栏推荐
- Chapter5 foundation of deep learning
- postgresql:在Docker中运行PostgreSQL + pgAdmin 4
- 472-82 (22, 165, 39, sword finger offer II 078, 48. Rotate image)
- 利用a*启发式搜索解决迷宫寻路问题
- PostgreSQL: run PostgreSQL + pgadmin 4 in docker
- How to set up an internal wiki for your enterprise?
- Transpose of array sparse matrix
- Source code compilation!!
- 京东方高级副总裁姜幸群:AIoT技术赋能企业物联网转型
- Rlib learning - [4] - algorithmconfig detailed introduction [pytoch version]
猜你喜欢

C primer plus learning notes - 6. Arrays and pointers

酒店IPTV数字电视系统解决方案

微信朋友圈的高性能架构设计

Chapter 0 Introduction to encog

View progress!!! RPM installation!!!
![Embedded system transplantation [2] - Construction of cross development environment](/img/96/8d209c04e41675fc0efaa872c35615.png)
Embedded system transplantation [2] - Construction of cross development environment

The fourth job: about the usage of cat, grep, cut, sort, uniq, VIM, TR and other commands

Bear market bottoming Guide

PPPoE网关模拟环境搭建

Fiddler抓包工具的使用
随机推荐
Mysq Database Constraints
Chapter 9 using image data
Technical team: improve team effectiveness, starting from never doing three things
1、基于增量式生成遮挡与对抗抑制的行人再识别
Ia notes 2
Pointer learning diary (IV) use structure and pointer (linked list)
XML schema
MySQL transaction and its problems and isolation level
NLP learning roadmap (mind map) is very comprehensive and clear!
Introduction to MapReduce
Emqx simple to use
Pointer learning diary (III)
The world's first large aerospace model came out. Wenxin's second supplement "Fuchun Mountain Residence map" is Baidu Pratt Whitney AI's perseverance
1. There is a fractional sequence: 2/1, 3/2, 5/3, 8/5, 13/8,... Program to sum the first 20 items of this sequence.
Pointer learning diary (I)
。单类型数字传感一个应用程.0。 Up- 开址在出厂
微信朋友圈的高性能架构设计
Token of space renewable energy
Kingbase V8R6集群安装部署案例---脚本在线一键扩容
Jetson device failed to download repository information use tips to record