从Datatable中检索用户和密码以进行登录

时间:2015-09-03 04:58:56

标签: java html mysql netbeans-7

我正在使用Netbeans。我已经为datatable创建了用户名和密码。我在登录和密码疑难解答时遇到问题,无法访问我的另一个jsp文件。它向我显示HTTP状态404 - 未找到。有什么问题?

以下是我的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>

2 个答案:

答案 0 :(得分: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 :(得分: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-我们通过以下方式呼叫require用户及其密码: 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();}`

}