Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8604941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:42:05+00:00 2026-06-12T02:42:05+00:00

Hi all trying to create a simple database login java servlet. It’s going straight

  • 0

Hi all trying to create a simple database login java servlet. It’s going straight to
Login failed. Please try again.

here is the set out of my database I have the table user–id, uniid, name, password, email

for example i have the following data in the table

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

here is the login servlet in it’s entirety

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; 
          } 
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T02:42:06+00:00Added an answer on June 12, 2026 at 2:42 am

    The database driver

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

    is not the correct one for use with MySQL. It should be

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

    When an exception occurs in your login method, the exception string is printed and it returns false.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This may be a really simple question, but I'm trying to create the database
I'm trying to create a simple visual basic 6 program/database that uses ms access
I'm just trying to create a new MySQL database with a few simple tables.
I am trying to create a simple login page in ASP.NET C# but am
I'm trying to create a utility class for traversing all the files in a
I'm trying to create a css menu bar that has all grey text with
I'm trying to create a good way to handle all possible collisions between two
I am trying to create a asyncallback to return a list with all the
I'm trying to create a created_at query range that will capture all records created
I was trying to create a linear order from git log output, but all

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.