2015-09-03 100 views
1

我正在使用Netbeans。我已經創建用戶和密碼datatable。我有問題排除我的登錄名和密碼來訪問我的另一個jsp文件。它向我顯示HTTP狀態404 - 未找到。問題是什麼?從Datatable中檢索用戶名和密碼進行登錄

以下是我的logincheck.jsp代碼:

<%@page contentType="text/html" pageEncoding="UTF-8"%> 

<%@ page language="Java" import="java.sql.*" %> 
<!DOCTYPE html> 

<body> 

<% 
String username = request.getParameter("username"); 
String password = request.getParameter("password"); 
String userType = request.getParameter("usertype"); 

String driver = "oracle.jdbc.driver.localDriver"; 
String dbURL = "jdbc:derby://localhost:1527/Parts "; 

String dbuser = "user"; 
String dbpassword = "password"; 

Connection theConnection = null; 
PreparedStatement theStatement = null; 

try { 

    Class.forName(driver); 

    theConnection=DriverManager.getConnection(dbURL,dbuser,dbpassword); 

    theStatement = theConnection.prepareStatement("select * from USERS"); 

    theStatement.setString(1,request.getParameter("username")); 
    theStatement.setString(2,request.getParameter("password")); 

    ResultSet theResult = theStatement.executeQuery(); 

    if(theResult.next()) 
     System.out.println("welcome.jsp"); 
    else 
     System.out.println("Failed"); 
} 
catch(Exception e) { 
    System.out.println("Exception occured! "+e.getMessage()+" "+e.getStackTrace()); 
} 
%> 
</body> 
+0

404錯誤發生在哪裏? – Steve

+0

它在HTTP 404上顯示狀態 - 未找到類型狀態報告消息未找到描述請求的資源不可用。我該怎麼辦? –

+0

我的意思是錯誤發生在哪一行? – Steve

回答

0

我認爲這可能是錯誤...

theStatement = theConnection.prepareStatement("select * from USERS"); 

應該是這樣..

theStatement = theConnection.prepareStatement("select * from USERS where Username=? and Password=?) 

theStatement.setString(1,username); 
theStatement.setString(2,password); 
.. 
.. 

讓我知道如果這個工程....

+1

@Drew,404意思是'在服務器上找不到頁面' – Sategroup

+0

@Drew,您可能是對的。它可能與Query沒有任何關係,因爲如果在Users表中有一條記錄,那麼'theResult.next()'將成功並調用welcome.jsp。 @Poh Chang Chien,你應該在歡迎時檢查URL,jsp被命中。這絕對是路徑問題。您的welcome.jsp應該位於根級別上。如果不是,那麼你需要指定從根級開始的完整路徑。例如:'http:// yoursite.com/path_to_welcome_jsp/welcome.jsp' – Sategroup

+0

謝謝。 :) theStatement = theConnection.prepareStatement(「select * from USERS where username = login and Password = password)。這是錯誤的語句 –

0

通過結果集從數據庫獲取一個require用戶和密碼。

String user=usertx.getText(); 
    char[] password=pswdtx.getPassword(); 
    String pswd=String.valueOf(password); \\here is important to put a char password in string.` 

1我們所說的需要由用戶和他的密碼: Resultset rs= stmt.excuteQuery("SELECT * FROM USERS WHERE NAME='"+user+"' AND PASSWORD='"+pswd+"'");
這個代碼將獲得行選定的用戶中,只有排他的信息,並將其設置(RS).`

rs.next()的

2-代碼:

while(rs.next()){ 
     rsuser=rs.getString("NAME");   //get user and set it in rsuser. 
     rspswd=rs.getString("PASSWORD");} //get password and set it in rspswd. 
     if ((user.equals(rsuser)) && (pswd.equals(rspswd))){ //the equevlant statment. 
     JOptionPane.showMessageDialog(null, "Username and Password exist");} 
     else { 
      JOptionPane.showMessageDialog(null, "Please Check Username and Password ");} 
     } 

這裏是整個代碼:

private void submitActionPerformed(java.awt.event.ActionEvent evt) {          
    try { 
     String user=usertx.getText(); 
     char[] password=pswdtx.getPassword(); 
     String pswd=String.valueOf(password); 

     String rsuser=null; 
     String rspswd=null; 
     String Q="SELECT * FROM USERS WHERE NAME='"+user+"' AND PASSWORD='"+pswd+"'"; 
     rs=stmt.executeQuery(Q); 

     while(rs.next()){ 
     rsuser=rs.getString("NAME"); 
     rspswd=rs.getString("PASSWORD");} 
     if ((user.equals(rsuser)) && (pswd.equals(rspswd))){ 
     JOptionPane.showMessageDialog(null, "Username and Password exist");} 
     else { 
      JOptionPane.showMessageDialog(null, "Please Check Username and Password ");} 
     } 
    catch (SQLException ex) {ex.printStackTrace();}` 

}