当前位置:网站首页>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遍。
边栏推荐
- 1、基于增量式生成遮挡与对抗抑制的行人再识别
- How can NFT, whose stars enter the market against the market, get out of the independent market?
- 1. Input a 100 point score from the keyboard and output its grade according to the following principles: score ≥ 90, Grade A; 80 ≤ score < 90, grade B; 70 ≤ score < 80, grade C; 60 ≤ score < 70, grade
- Source code compilation!!
- 连接数%的准确率。现在拟需求。企业在数足以
- Hcip-- review the homework for the next day
- Markov random field: definition, properties, maximum a posteriori probability problem, energy minimization problem
- XML schema
- 太空可再生能源的代币化
- 智能指针、左值引用右值引用、lambda表达式
猜你喜欢

Introduction to 51 single chip microcomputer (dedicated to the most understandable article for beginners)

Update C language notes

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

This article takes you to understand C string functions and memory functions in simple terms

Scikit learn -- steps of machine learning application development

利用a*启发式搜索解决迷宫寻路问题

Redis enhancements

Basic knowledge of MySQL database

Web3 product manager's Guide: how to face the encryption world

How can NFT, whose stars enter the market against the market, get out of the independent market?
随机推荐
作、Ho量有关。嵌入,只有一70的该接
Accuracy of% connections. Now it is planned to meet the demand. The number of enterprises is enough
Token of space renewable energy
Mysq Database Constraints
How to set up an internal wiki for your enterprise?
反射
power. The operation is in the low peak period of business. Import call will help you prepare each word
Ren Xudong, chief open source liaison officer of Huawei: deeply cultivate basic software open source and jointly build the root technology of the digital world
Drools development decision table
Chapter 0 Introduction to encog
编译型语言和解释型语言的区别
Memorandum 2022
Foreign key operation of MySQL_ Cascade operation
微信朋友圈的高性能架构设计
Jetson device failed to download repository information use tips to record
[postgraduate entrance examination vocabulary training camp] day 10 - capital, expand, force, adapt, depand
Learning pyramid context encoder network for high quality image painting paper notes
Use of fiddler packet capturing tool
Markov random field: definition, properties, maximum a posteriori probability problem, energy minimization problem
Scikit learn -- steps of machine learning application development