ResultSet#next() 返回 false 即使我的查询是正确的

ResultSet#next() is returning false even though my query is correct

我在 jsp 文件中编写了以下代码。基本上它所做的是检查是否有任何具有特定银行名称、卡号和密码的数据。检查这个我使用了 select 查询..查询是正确的,但结果集返回错误..我什至单独检查了查询..它工作正常..这段代码有什么问题吗?请帮助..我正在使用 netbeans 作为我的 IDE 和 Derby 数据库..

    String URL="jdbc:derby://localhost:1527/Bank Database";
    try{
        Connection cn=DriverManager.getConnection(URL,"Bank","Bank");
        String qry="SELECT * FROM DATABASE WHERE BANK='"+bname+"' AND CARD_NO='"+cname+"' AND PASWORD='"+pswrd+"'";
        Statement sq=cn.createStatement();
        ResultSet rs=sq.executeQuery(qry);
        if(rs.next()){
            if(rs.getString("PASWORD").equals(pswrd))
            {
                cn.close();
                out.println("<script>function redirect(){ window.location=\"scsfl.html\";} setTimeout('redirect()',3000);</script>");  //redirects to another page   
            }
        }
        else{
            cn.close();
            out.println("<script>function redirect(){ window.location=\"failed.html\";} setTimeout('redirect()',3000);</script>");
        }
    }catch(Exception e){ System.out.print(e);}

一般来说,关闭连接应该会自动关闭与该连接关联的所有语句和结果集。

在这段代码中,我关闭了 rs.next() 方法中的连接接口引用。这就是 JSP 引擎无法将其转换为 java 文件的原因。它显示错误信息。请找到代码和相应的错误消息。

Jsp代码:

Class.forName("oracle.jdbc.driver.OracleDriver");
   Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "test", "test");
   
   String username = "Anil3";
   String password = "123";
  
  
   PreparedStatement pst = con.prepareStatement("select * from User_Login where userID='"+ username +"'");
   ResultSet rs = pst.executeQuery();
 while(rs.next())
 {
   if (password.equals(rs.getString("password"))) 
    {
   con.close();
  out.print("inside if loop");
    }
   
   else{
    
    con.close();
    out.print("Inside else loop");
   }
 }

错误信息:

org.apache.jasper.JasperException: An exception occurred processing JSP page /test.jsp at line 28

25:   
26:    PreparedStatement pst = con.prepareStatement("select * from User_Login where userID='"+ username +"'");
27:    ResultSet rs = pst.executeQuery();
28:  while(rs.next())
29:  {
30:    if (password.equals(rs.getString("password"))) 
31:     {


Stacktrace:
 org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)
 org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:455)
 org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
 org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:728)


root cause 

javax.servlet.ServletException: java.sql.SQLException: Closed Connection: next
 org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:912)
 org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:841)
 org.apache.jsp.test_jsp._jspService(test_jsp.java:124)
 org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
 org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
 org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
 org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:728)


root cause 

java.sql.SQLException: Closed Connection: next
 oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
 oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:146)
 oracle.jdbc.driver.OracleResultSetImpl.next(OracleResultSetImpl.java:181)
 org.apache.jsp.test_jsp._jspService(test_jsp.java:97)
 org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
 org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
 org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
 org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

您不应该关闭 ResultSet 内的连接。请删除代码中的 con.close() 语句并尝试。它会起作用。

如评论中所述,您的实际代码显示出许多问题:

  1. 您使用的是 Statement 而不是 PreparedStatement,后者更安​​全并且可以防止某些黑客移动 SQL 注入,您可以在 [=19= 中找到更多信息].
  2. 您正在检查 Java 代码中的密码,而它已经在您的 SQL 查询中完成,因此查询会检查密码是否正确,否则会 return 结果结果将为空。
  3. if..else 语句中关闭连接是非常 糟糕的 做法,您最好在 [=15] 中使用 try..catch 语句来处理它=] 块,您将在其中分别关闭 resultsetprepared statementconnexion.

我试图改进你的代码,这是你应该做的:

    Connection con = null;
    String URL="jdbc:derby://localhost:1527/Bank Database";
    PreparedStatement ps = null;
    ResultSet rs = null;
    String qry="SELECT * FROM DATABASE WHERE BANK=? AND CARD_NO=? AND PASWORD=?";
    try {
        con = DriverManager.getConnection(URL,"Bank","Bank");
        ps = con.prepareStatement(qry);

        //set the parameter
        ps.setString(1, bname);
        ps.setString(2, cname);
        ps.setString(3, pswrd);
        rs = ps.executeQuery();

        if (rs.next()) {
            out.println("<script>function redirect(){ window.location=\"scsfl.html\";} setTimeout('redirect()',3000);</script>");  //redirects to another page 
        } else {
            out.println("<script>function redirect(){ window.location=\"failed.html\";} setTimeout('redirect()',3000);</script>");
        }
    } finally {
        if (rs != null)
            rs.close();
        ps.close();
        con.close();
    }

确保你在正确的数据库中,我犯了几次这个错误。 查询正确,错误的数据库。