0% found this document useful (0 votes)
3 views4 pages

Database Connection in SQL

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)
3 views4 pages

Database Connection in SQL

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
You are on page 1/ 4

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 LoginSystem.java:

import java.sql.*;

import java.util.Scanner;

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(System.in);

while (true) {

System.out.println("\n==== Welcome to the Login Page ====");

System.out.println("1. Register");
System.out.println("2. Login");

System.out.println("3. Exit");

System.out.print("Choose option: ");

int choice = scanner.nextInt();

scanner.nextLine(); // consume newline

switch (choice) {

case 1:

register(scanner);

break;

case 2:

login(scanner);

break;

case 3:

System.out.println("Goodbye!");

return;

default:

System.out.println("Invalid choice!");

private static void register(Scanner scanner) {

System.out.println("---- Registration ----");

System.out.print("Enter username: ");

String username = scanner.nextLine();

System.out.print("Enter password: ");

String password = scanner.nextLine();

try (Connection conn = DriverManager.getConnection(JDBC_URL, DB_USER, DB_PASS)) {


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

PreparedStatement pstmt = conn.prepareStatement(query);

pstmt.setString(1, username);

pstmt.setString(2, password); // In real-world use hashing!

int rows = pstmt.executeUpdate();

if (rows > 0) {

System.out.println("Registered successfully!");

} else {

System.out.println("Registration failed.");

} catch (SQLIntegrityConstraintViolationException e) {

System.out.println("Username already exists. Please choose another one.");

} catch (SQLException e) {

e.printStackTrace();

private static void login(Scanner scanner) {

System.out.println("---- Login ----");

System.out.print("Enter username: ");

String username = scanner.nextLine();

System.out.print("Enter password: ");

String password = scanner.nextLine();

try (Connection conn = DriverManager.getConnection(JDBC_URL, DB_USER, DB_PASS)) {

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

PreparedStatement pstmt = conn.prepareStatement(query);

pstmt.setString(1, username);
pstmt.setString(2, password);

ResultSet rs = pstmt.executeQuery();

if (rs.next()) {

System.out.println(" Welcome " + username + ", you have logged in successfully!");

} else {

System.out.println(" Invalid credentials. Try again.");

} catch (SQLException e) {

e.printStackTrace();

Step 3:

javac -cp ".;lib/mysql-connector-java-8.0.33.jar" LoginSystem.java

java -cp ".;lib/mysql-connector-java-8.0.33.jar" LoginSystem

You might also like