rs.next()总是返回false

bzzcjhmw  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(145)

数据库中有条目,但每当我在登录窗体中输入任何有效或无效的详细信息时,它总是显示登录失败。rs.next()总是返回false。这个问题有什么解决办法吗?
代码如下follows:-

try {

    Class.forName("com.mysql.jdbc.Driver");
    Connection con =DriverManager.getConnection("jdbc:mysql://localhost:3306/logindata","root","");

    @SuppressWarnings("deprecation")

    String sql ="Select * from login Where UserName=' " + textField.getText()+ " 'and Password= ' " + passwordField.getText().toString()+"'";
    PreparedStatement stmt =con.prepareStatement(sql);
    ResultSet rs = stmt.executeQuery();

    String id=null;
    String userId=null;

    while(rs.next())
    {
    id=rs.getString("UserName");
   userId=rs.getString("Password");
   }

   if(id!=null)
   System.out.println("Login Success");
   else
   System.out.println("Login Failed");

    con.close();    
}
catch(Exception ex) {
    System.out.println(ex);
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题