在演示sql注入问题演示的时候,发现有个问题,但是自己弄不明白,请教各位大佬
public static void login() {
String name = "柳岩' -- ";
String id = "1";//模拟密码
Connection conn = null;
Statement st = null;
ResultSet rst = null;
try {
conn = JDBCUtils01.getConnection();
st = conn.createStatement();
rst = st.executeQuery("select * from user where name = '" + name + "'and id ='" + id+"'");
//处理结果集
while (rst.next()) {
int u_id = rst.getInt(1);
String u_name = rst.getString("name");
}
if (rst.next()) {
System.out.println("登录成功,欢迎您 " + rst.getString("name"));
} else {
System.out.println("用户名或者密码错误");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils01.release(conn,st,rst);
}
}
while (rst.next())是之前写错的,忘记删,演示的时候发现当while存在的时候就会报用户名错误,注释掉就可以了。
求指点~
因为rst.next()读取一条记录,因为数据库只返回了一条,读完了再读就没有了,所以while (rst.next()) 不能加
另外select * from user不合适,把所有的字段都取出来了,如果表中包含敏感信息,岂不是都返回了
你确认id是字符串类型么?否则sql里面去掉引号
因为你调用了2次rst.next()
实际上只应该调用1次
Boolean flag = rst.next();
if (flag) {
int u_id = rst.getInt(1);
String u_name = rst.getString("name");
}
if (flag) {
System.out.println("登录成功,欢迎您 " + rst.getString("name"));
} else {
System.out.println("用户名或者密码错误");
}