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

Java JDBC

The document outlines the setup of a MySQL database for a banking application, including the creation of 'users' and 'accounts' tables. It also provides a JDBC connection class for database interaction and a main banking program that allows users to log in, create accounts, deposit, withdraw, and check their balance. The program uses prepared statements for secure database operations and handles user input through a console interface.

Uploaded by

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

Java JDBC

The document outlines the setup of a MySQL database for a banking application, including the creation of 'users' and 'accounts' tables. It also provides a JDBC connection class for database interaction and a main banking program that allows users to log in, create accounts, deposit, withdraw, and check their balance. The program uses prepared statements for secure database operations and handles user input through a console interface.

Uploaded by

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

MySQL Database Setup

CREATE DATABASE bankdb;

USE bankdb;

-- USERS TABLE

CREATE TABLE users (

username VARCHAR(50) PRIMARY KEY,

password VARCHAR(50)

);

-- ACCOUNTS TABLE (linked with user)

CREATE TABLE accounts (

acc_no INT PRIMARY KEY,

name VARCHAR(50),

balance DOUBLE,

username VARCHAR(50),

FOREIGN KEY (username) REFERENCES users(username)

);

-- Insert test user

INSERT INTO users VALUES ('admin', '1234');


JDBC Connection Class

package [Link];

import [Link];

import [Link];

public class DBConnection {

public static Connection getConnection() {

try {

[Link]("[Link]");

return [Link](

"jdbc:mysql://localhost:3306/bankdb",

"root",

"password" // change to your MySQL password

);

} catch (Exception e) {

[Link]();

return null;

}
3. Main Banking Program

package [Link];

import [Link].*;

import [Link];

public class BankApp {

static Scanner sc = new Scanner([Link]);

static String loggedInUser = null;

public static void main(String[] args) {

if (!login()) {

[Link]("Exiting...");

return;

while (true) {

[Link]("\n--- Banking System ---");

[Link]("1. Create Account");

[Link]("2. Deposit");

[Link]("3. Withdraw");

[Link]("4. Check Balance");

[Link]("5. Exit");

[Link]("Enter choice: ");

int choice = [Link]();

switch (choice) {

case 1: createAccount(); break;

case 2: deposit(); break;

case 3: withdraw(); break;


case 4: checkBalance(); break;

case 5: [Link](0);

default: [Link]("Invalid choice!");

// LOGIN

static boolean login() {

try {

Connection con = [Link]();

[Link]("Username: ");

String username = [Link]();

[Link]("Password: ");

String password = [Link]();

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

PreparedStatement ps = [Link](query);

[Link](1, username);

[Link](2, password);

ResultSet rs = [Link]();

if ([Link]()) {

loggedInUser = username;

[Link]("Login Successful!");

return true;

} else {

[Link]("Invalid Credentials!");
return false;

} catch (Exception e) {

[Link]();

return false;

// CREATE ACCOUNT (linked to logged user)

static void createAccount() {

try {

Connection con = [Link]();

[Link]("Enter Account No: ");

int accNo = [Link]();

[Link]("Enter Name: ");

String name = [Link]();

[Link]("Enter Initial Balance: ");

double balance = [Link]();

String query = "INSERT INTO accounts VALUES (?, ?, ?, ?)";

PreparedStatement ps = [Link](query);

[Link](1, accNo);

[Link](2, name);

[Link](3, balance);

[Link](4, loggedInUser);

[Link]();

[Link]("Account Created!");
} catch (Exception e) {

[Link]("Error creating account!");

[Link]();

// DEPOSIT (only your account)

static void deposit() {

try {

Connection con = [Link]();

[Link]("Enter Account No: ");

int accNo = [Link]();

[Link]("Enter Amount: ");

double amount = [Link]();

String query = "UPDATE accounts SET balance = balance + ? WHERE acc_no=? AND username=?";

PreparedStatement ps = [Link](query);

[Link](1, amount);

[Link](2, accNo);

[Link](3, loggedInUser);

int rows = [Link]();

if (rows > 0)

[Link]("Deposit Successful!");

else

[Link]("Account not found or not yours!");

} catch (Exception e) {
[Link]();

// WITHDRAW

static void withdraw() {

try {

Connection con = [Link]();

[Link]("Enter Account No: ");

int accNo = [Link]();

[Link]("Enter Amount: ");

double amount = [Link]();

String checkQuery = "SELECT balance FROM accounts WHERE acc_no=? AND username=?";

PreparedStatement ps1 = [Link](checkQuery);

[Link](1, accNo);

[Link](2, loggedInUser);

ResultSet rs = [Link]();

if ([Link]()) {

double balance = [Link]("balance");

if (balance >= amount) {

String updateQuery = "UPDATE accounts SET balance = balance - ? WHERE acc_no=? AND
username=?";

PreparedStatement ps2 = [Link](updateQuery);

[Link](1, amount);

[Link](2, accNo);

[Link](3, loggedInUser);
[Link]();

[Link]("Withdrawal Successful!");

} else {

[Link]("Insufficient Balance!");

} else {

[Link]("Account not found!");

} catch (Exception e) {

[Link]();

// CHECK BALANCE

static void checkBalance() {

try {

Connection con = [Link]();

[Link]("Enter Account No: ");

int accNo = [Link]();

String query = "SELECT * FROM accounts WHERE acc_no=? AND username=?";

PreparedStatement ps = [Link](query);

[Link](1, accNo);

[Link](2, loggedInUser);

ResultSet rs = [Link]();

if ([Link]()) {

[Link]("Name: " + [Link]("name"));


[Link]("Balance: " + [Link]("balance"));

} else {

[Link]("Account not found!");

} catch (Exception e) {

[Link]();

You might also like