How To Connect Login Page To Database In Eclipse

Web Development Software

Hello there! Today, I’m going to share a step-by-step guide on how to connect a login page to a database in Eclipse. As an experienced developer, I’ve come across this scenario countless times, and I’m excited to guide you through the process. So, let’s dive in!

Setting up the Database

The first step is to set up the database that will store the login credentials. In Eclipse, we can use a popular database management system like MySQL or PostgreSQL. For the sake of this tutorial, let’s assume we’ll be using MySQL.

To get started, you’ll need to install the MySQL server on your machine. Once installed, open the MySQL command-line client and create a new database using the following command:

CREATE DATABASE login_db;

After creating the database, you’ll need to create a table to store the user login information. Execute the following SQL statement to create a table called `users`:

CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL,
password VARCHAR(50) NOT NULL
);

Now that we have our database and table set up, let’s move on to coding the login page.

Coding the Login Page

In Eclipse, create a new dynamic web project and add a new HTML file called `login.html` to the project. This file will contain the user interface for the login page. Add the following code to the `login.html` file:


<html>
<head>
<title>Login Page</title>
</head>
<body>
<h2>Login Page</h2>
<form action="loginServlet" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="username"><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password"><br>
<input type="submit" value="Login">
</form>
</body>
</html>

In this code snippet, we’ve created a simple login form with fields for username and password. The form action is set to `loginServlet`, which will be our Java servlet responsible for authenticating the user against the database.

Next, let’s create the `loginServlet` class. In Eclipse, create a new Java class called `LoginServlet` and add the following code:


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

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

public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

private final String DB_URL = "jdbc:mysql://localhost:3306/login_db";
private final String DB_USERNAME = "root";
private final String DB_PASSWORD = "password";

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");

try {
Connection conn = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);
String sql = "SELECT * FROM users WHERE username = ? AND password = ?";
PreparedStatement statement = conn.prepareStatement(sql);
statement.setString(1, username);
statement.setString(2, password);

ResultSet result = statement.executeQuery();

if (result.next()) {
response.sendRedirect("welcome.html");
} else {
response.sendRedirect("login.html");
}

conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

Connecting the Login Page to the Database

In the `LoginServlet` class, we establish a connection to the MySQL database using the JDBC driver. Make sure to provide the correct URL, username, and password for your database in the respective fields.

Next, we retrieve the username and password entered by the user from the request parameters. We then execute a SQL query to check if there is a matching record in the `users` table. If a match is found, we redirect the user to a welcome page (`welcome.html`), indicating successful login. Otherwise, the user is redirected back to the login page (`login.html`).

Conclusion

And there you have it! With just a few simple steps, you’ve learned how to connect a login page to a database in Eclipse. By following this guide, you can create a secure and robust login system for your web applications. Remember to handle database connections and queries securely and effectively to protect user data. Happy coding!