Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
30 views4 pages

MySQL User Registration and Login System

The document provides a step-by-step guide to create a MySQL database and a Java-based login system. It includes SQL commands to set up a database and a table for user credentials, as well as Java code for user registration and login functionality. Instructions for compiling and running the Java program are also included.

Uploaded by

mirshakishok
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views4 pages

MySQL User Registration and Login System

The document provides a step-by-step guide to create a MySQL database and a Java-based login system. It includes SQL commands to set up a database and a table for user credentials, as well as Java code for user registration and login functionality. Instructions for compiling and running the Java program are also included.

Uploaded by

mirshakishok
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Step 1: Create MySQL Database and Table

Run this SQL in MySQL:

CREATE DATABASE login_db;

USE login_db;

CREATE TABLE users (

id INT AUTO_INCREMENT PRIMARY KEY,

username VARCHAR(50) NOT NULL UNIQUE,

password VARCHAR(255) NOT NULL

);

Step 2: Java Code for Registration and Login

Save this as [Link]:

import [Link].*;

import [Link];

public class LoginSystem {

static final String JDBC_URL = "jdbc:mysql://localhost:3306/login_db";

static final String DB_USER = "root"; // change if needed

static final String DB_PASS = "your_password"; // change to your MySQL password

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

while (true) {

[Link]("\n==== Welcome to the Login Page ====");

[Link]("1. Register");
[Link]("2. Login");

[Link]("3. Exit");

[Link]("Choose option: ");

int choice = [Link]();

[Link](); // consume newline

switch (choice) {

case 1:

register(scanner);

break;

case 2:

login(scanner);

break;

case 3:

[Link]("Goodbye!");

return;

default:

[Link]("Invalid choice!");

private static void register(Scanner scanner) {

[Link]("---- Registration ----");

[Link]("Enter username: ");

String username = [Link]();

[Link]("Enter password: ");

String password = [Link]();

try (Connection conn = [Link](JDBC_URL, DB_USER, DB_PASS)) {


String query = "INSERT INTO users (username, password) VALUES (?, ?)";

PreparedStatement pstmt = [Link](query);

[Link](1, username);

[Link](2, password); // In real-world use hashing!

int rows = [Link]();

if (rows > 0) {

[Link]("Registered successfully!");

} else {

[Link]("Registration failed.");

} catch (SQLIntegrityConstraintViolationException e) {

[Link]("Username already exists. Please choose another one.");

} catch (SQLException e) {

[Link]();

private static void login(Scanner scanner) {

[Link]("---- Login ----");

[Link]("Enter username: ");

String username = [Link]();

[Link]("Enter password: ");

String password = [Link]();

try (Connection conn = [Link](JDBC_URL, DB_USER, DB_PASS)) {

String query = "SELECT * FROM users WHERE username = ? AND password = ?";

PreparedStatement pstmt = [Link](query);

[Link](1, username);
[Link](2, password);

ResultSet rs = [Link]();

if ([Link]()) {

[Link](" Welcome " + username + ", you have logged in successfully!");

} else {

[Link](" Invalid credentials. Try again.");

} catch (SQLException e) {

[Link]();

Step 3:

javac -cp ".;lib/[Link]" [Link]

java -cp ".;lib/[Link]" LoginSystem

You might also like