之前还好使在运行就变成了添加失败走了else那个判断了
来源:5-1 JDBC的SQL注入漏洞的演示
qq_ibertine_0
2020-10-08 16:17:02
package com.imooc.jdbc1;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.junit.Test;
public class Jdbc4 {
@Test
public void demo() {
Connection con= null;
Statement sta=null;
ResultSet res=null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
try {
con= DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctest"+"?useSSL=false&serverTimezone=Hongkong&useUnicode=true&characterEncoding=utf-8","root","wfy199852");
sta= con.createStatement();
String sql="delete from seller where id=5";
int i= sta.executeUpdate(sql);
if(i>0) {
String str="select * from seller";
res= sta.executeQuery(str);
while(res.next()) {
int id= res.getInt("id");
String name =res.getString("name");
Float price =res.getFloat("price");
String desp =res.getString("desp");
System.out.println(id+" "+name+" "+price+" "+desp);
}
}else
{
System.out.println("删除失败");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(con!=null) {
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
con=null;
}
if(sta!=null) {
try {
sta.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sta=null;
}
if(res!=null) {
try {
res.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
res=null;
}
}
}
}
1回答
同学你好,当第一遍删除后,数据库中id为5的数据是不存在的,因此i为0,然后进行判断,不符合if条件就会执行else语句中的“删除失败”。如下:
如果我的回答解决了你的疑惑,请采纳,祝学习愉快~
相似问题