2017-04-02 258 views
-1

我是編程領域的新手。我試圖做一個簡單的登錄不過,我得到這個錯誤:索引中缺少IN或OUT參數:: 3

missing IN or OUT parameter at index:: 3

我試圖找到答案,但我仍然無法在我的編碼認識到這個問題。

這裏是我的LoginServlet

package apl; 

import java.io.IOException; 
import java.io.PrintWriter; 

import javax.servlet.RequestDispatcher; 
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import javax.servlet.http.HttpSession; 

import apl.LoginDao; 


public class LoginServlet extends HttpServlet{ 

    private static final long serialVersionUID = 1L; 

    public void doPost(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 

     response.setContentType("text/html"); 
     PrintWriter out = response.getWriter(); 

     String i=request.getParameter("sid"); 
     String n=request.getParameter("sname"); 
     String p=request.getParameter("sphone"); 
     String u=request.getParameter("susername"); 
     String w=request.getParameter("spassword"); 


     HttpSession session = request.getSession(true); 
     if(session!=null) 
     session.setAttribute("sid", i); 

     if(LoginDao.validate(i,n,p,u,w)){ 
      RequestDispatcher rd=request.getRequestDispatcher("welcome.jsp"); 
      rd.forward(request,response); 
     } 
     else{ 
      out.print("<p style=\"color:red\">Sorry username or password incorrect</p>"); 
      RequestDispatcher rd=request.getRequestDispatcher("login.jsp"); 
      rd.include(request,response); 
     } 

     out.close(); 
    } 
} 

這裏是我的LoginDao

package apl; 

import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.sql.SQLException; 

public class LoginDao { 

     public static boolean validate(String sid, String sname, String sphone, String susername, String spassword) { 

      boolean status = false; 
      Connection conn = null; 
      PreparedStatement pst = null; 
      ResultSet rs = null; 

      String driver = "oracle.jdbc.driver.OracleDriver"; 

      try { 
       Class.forName(driver).newInstance(); 
       conn = DriverManager 
         .getConnection("jdbc:oracle:thin:@localhost:1521:xe","apl","system"); 

       pst = conn.prepareStatement("select * from SELLER where sid=?,sname=?,sphone=?,susername=? and spassword=?"); 

       pst.setString(1, sid);    
       pst.setString(2, sname);     
       pst.setString(3, sphone); 
       pst.setString(4, susername); 
       pst.setString(5, spassword); 


       rs = pst.executeQuery(); 

       status = rs.next();    


      } catch (Exception e) { 
       System.out.println(e); 
      } finally { 
       if (conn != null) { 
        try { 
         conn.close(); 
        } catch (SQLException e) { 
         e.printStackTrace(); 
        } 
       } 
       if (pst != null) { 
        try { 
         pst.close(); 
        } catch (SQLException e) { 
         e.printStackTrace(); 
        } 
       } 
       if (rs != null) { 
        try { 
         rs.close(); 
        } catch (SQLException e) { 
         e.printStackTrace(); 
        } 
       } 
      } 
      return status;  
     } 
    } 

這裏是我的JSP文件

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
    pageEncoding="ISO-8859-1"%> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>Login Application</title> 
</head> 
<body> 
    <form action="LoginServlet" method="post"> 
     <fieldset style="width: 300px"> 
      <legend> Login to App </legend> 
      <table> 
       <tr> 
        <td>Id</td> 
        <td><input type="text" name="sid" required="required" /></td> 
       </tr> 
       <tr> 
        <td>Password</td> 
        <td><input type="text" name="sname" required="required" /></td> 
       </tr> 
       <tr> 
        <td>Phone</td> 
        <td><input type="text" name="sphone" required="required" /></td> 
       </tr> 
       <tr> 
        <td>User name</td> 
        <td><input type="text" name="susername" required="required" /></td> 
       </tr> 
       <tr> 
        <td>Password</td> 
        <td><input type="password" name="spassword" required="required" /></td> 
       </tr> 
       <tr> 
        <td><input type="submit" value="Login" /></td> 
       </tr> 
      </table> 
     </fieldset> 
    </form> 
</body> 
</html> 

的web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> 
    <servlet> 
    <servlet-name>LoginServlet</servlet-name> 
    <servlet-class>apl.LoginServlet</servlet-class> 
    </servlet> 
    <servlet-mapping> 
    <servlet-name>LoginServlet</servlet-name> 
    <url-pattern>/LoginServlet</url-pattern> 
    </servlet-mapping> 
    <welcome-file-list> 
    <welcome-file>login.jsp</welcome-file> 
    </welcome-file-list> 
+1

歡迎堆棧溢出!我會想象這95%的代碼與你的問題無關。請創建一個[**最小**,完整且可驗證的示例](http://stackoverflow.com/help/mcve),以說明您的問題。 –

+0

請閱讀[在什麼情況下,我可以添加「緊急」或其他類似的短語到我的問題,以獲得更快的答案?](https://meta.stackoverflow.com/q/326569) - 總結是,這並不是解決志願者問題的理想方式,而且可能對獲得答案產生反作用。請不要將這添加到您的問題。 – halfer

+0

請不要將Stack Snippets用於不是CSS,HTML或JavaScript的材料。 Java不能在使用Snippet的瀏覽器中運行。 – halfer

回答

1

在SQL where條款,and操作缺失,所以你需要添加它,如下圖所示:

pst = conn.prepareStatement("select * from SELLER where sid=? 
and sname=? and sphone=? and susername=? and spassword=?") 
+0

我試圖修改它,但它不工作。你有另外的建議嗎? – anan

+0

現在有什麼錯誤? – developer

+0

仍然一樣。 「index:3缺少IN或OUT參數」 – anan

相關問題