0

こんにちは、単純なデータベースログインJavaサーブレットを作成しようとしています。それはまっすぐに行きます Login failed. Please try again.

これが私のデータベースのセットです私はテーブルを持っていますuser- 、、、、、iduniidnamepasswordemail

たとえば、私はテーブルに次のデータを持っています

uniid - 18357593
name - Jared Hoight
password - password
email - jhall@students.monash.edu.au

これがログインサーブレット全体です

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.RequestDispatcher;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    private void sendLoginForm(HttpServletResponse response, 
              boolean withErrorMessage) 
              throws ServletException, IOException {

              response.setContentType("text/html"); 
              PrintWriter out = response.getWriter(); 
              out.println("<HTML>"); 
              out.println("<HEAD>"); 
              out.println("<TITLE>Login</TITLE>"); 
              out.println("</HEAD>"); 
              out.println("<BODY>"); 
              out.println("<CENTER>"); 

              if (withErrorMessage) 
                out.println("Login failed. Please try again.<BR>"); 

              out.println("<BR>"); 
              out.println("<BR><H2>Login Page</H2>"); 
              out.println("<BR>"); 
              out.println("<BR>Please enter your user name and password."); 
              out.println("<BR>"); 
              out.println("<BR><FORM METHOD=POST>"); 
              out.println("<TABLE>"); 
              out.println("<TR>"); 
              out.println("<TD>User Name:</TD>"); 
              out.println("<TD><INPUT TYPE=TEXT NAME=uniid></TD>"); 
              out.println("</TR>"); 
              out.println("<TR>"); 
              out.println("<TD>Password:</TD>"); 
              out.println("<TD><INPUT TYPE=PASSWORD NAME=password></TD>"); 
              out.println("</TR>"); 
              out.println("<TR>"); 
              out.println("<TD ALIGN=RIGHT COLSPAN=2>"); 
              out.println("<INPUT TYPE=SUBMIT VALUE=Login></TD>"); 
              out.println("</TR>"); 
              out.println("</TABLE>"); 
              out.println("</FORM>"); 
              out.println("</CENTER>"); 
              out.println("</BODY>"); 
              out.println("</HTML>"); 
            } 


    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        sendLoginForm(response, false); 

    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    public void doPost(HttpServletRequest request, 
              HttpServletResponse response) 
              throws ServletException, IOException {  
                String uniid = request.getParameter("uniid"); 
                String password = request.getParameter("password"); 
                if (login(uniid, password)) {
                  RequestDispatcher rd = 
                    request.getRequestDispatcher("AnotherServlet"); 
                  rd.forward(request, response); 
                } 
                else {
                  sendLoginForm(response, true); 
                } 

    }

     boolean login(String uniid, String password) {
            try {
              Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
              Connection con = 
                DriverManager.getConnection("jdbc:mysql://localhost:3306/wae","root",""); 
              System.out.println("got connection"); 

              Statement s = con.createStatement(); 
              String sql = "SELECT uniid FROM user" + 
                " WHERE uniid='" + uniid + "'" + 
                " AND password='" + password + "'"; 
              ResultSet rs = s.executeQuery(sql); 
              if (rs.next()) {
                rs.close(); 
                s.close(); 
                con.close(); 
                return true; 
              } 
                rs.close(); 
                s.close(); 
                con.close(); 
            } 
            catch (ClassNotFoundException e) {
              System.out.println(e.toString()); 
            } 
            catch (SQLException e) {
              System.out.println(e.toString()); 
            } 
            catch (Exception e) {
              System.out.println(e.toString()); 
            } 
            return false; 
          } 
        }
4

1 に答える 1

1

データベースドライバー

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 

MySQLで使用するための正しいものではありません。そのはず

Class.forName("com.mysql.jdbc.Driver");

メソッドで例外が発生するloginと、例外文字列が出力され、falseが返されます。

于 2012-10-01T01:25:12.490 に答える