无法使用PreparedStatement检索数据

时间:2016-05-09 02:54:55

标签: java oracle jsp servlets

我编写了一些代码,用于从数据库中检索数据。如果我给出了硬编码值,那么它工作正常但我想做动态然后它不从db获取数据。

编码

UserLogin.java

    PrintWriter out=response.getWriter();
    String firstName = request.getParameter("firstName"); 
    String password = request.getParameter("password"); 
    Connection con= DBConnection.getConnection();   
    String sql = "select count(*) from hmssignupdetails where firstname=?  and password=?";
    PreparedStatement ps;
    try {
        ps = con.prepareStatement(sql);
         ps.setString(1, firstName);
         ps.setString(2, password);
         ResultSet rs=ps.executeQuery();
         if(rs.next()){
            int i= rs.getInt(1);
             if(i==1){
                 request.setAttribute("err",firstName );
                 System.out.println("Details");
                 /*System.out.println(rs.getInt(1));
                 System.out.println(rs.getString(2));*/

                 response.sendRedirect("CustomerDetail.jsp");
             }
             else{
                 System.out.println("Sorry UserName or Password Error!");  
                 RequestDispatcher rd=request.getRequestDispatcher("UserLogin.jsp");  
                 request.setAttribute("err", "Invalid login");
                 rd.include(request, response);  
             }

         }

如何设置用户名和密码的值,我需要在jsp页面下面获取值。请提出建议?

CustomerDetail.jsp

    <%
    String firstName = request.getParameter("firstName");
    String password = request.getParameter("password");
    Connection con = DBConnection.getConnection();
    //String sql = "select * from HMSSIGNUPDETAILS";
    /* Statement stmt = con.createStatement();
      ResultSet rset = stmt
    .executeQuery("select * from HMSSIGNUPDETAILS where firstname='Malay' and password='123'");  */
     System.out.println("fetching data");
      String sql = "select * from HMSSIGNUPDETAILS where firstname=? and password=?";
    PreparedStatement ps;
    ps = con.prepareStatement(sql);
    ps.setString(1, firstName);
    ps.setString(2, password);
    ResultSet rset = ps.executeQuery(); 

%>

上面的注释行代码工作正常,但如果动态想要在Prepared语句的帮助下做,那么它无法从db检索数据?可能是什么原因?

以dsiplay数据

  <center>
        <table border="1" bordercolor="red" style="width: 100%;">
        <tr style="font-size: 16px; color: red;">       
            <td>USER ID</td>
            <td>FIRSTNAME</td>
            <td>LASTNAME</td>
            <td>EMAIL</td>
            <td>PASSWORD</td>
            <td>REPASSWORD</td>
            <td>IDENTIFICATION</td>
            <td>PHONE NO</td>
            <td>ADDRESS</td>
        </tr>
        </table>
    </center>
  <%
     if(rset.next()) {
%>
<center>
    <table border="1" bordercolor="red" style="width: 100%">
        <tr style="font-size: 20px; color: white;">
            <td><%=rset.getString("userid")%></td>
            <td><%=rset.getString("firstname")%></td>
            <td><%=rset.getString("lastname")%></td>
            <td><%=rset.getString("email")%></td>
            <td><%=rset.getString("password")%></td>
            <td><%=rset.getString("repassword")%></td>
            <td><%=rset.getString("identification")%></td>
            <td><%=rset.getString("phoneno")%></td>
            <td><%=rset.getString("address")%></td>
        </tr>
    </table>
</center>
<%
    }
%>

0 个答案:

没有答案