import javax.swing.
*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.awt.Desktop;
import java.net.URI;
public class BankManagementSystem3 {
private JFrame frame;
private JTextField nameField, balanceField, amountField;
private JTextArea outputArea;
private Connection connection;
public BankManagementSystem3() {
// Set up the GUI
frame = new JFrame("Bank Management System");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1366, 768);
frame.setLayout(null);
frame.setLocationRelativeTo(null);
// UI Components
JLabel nameLabel = new JLabel("Account Holder Name:");
JLabel balanceLabel = new JLabel("Initial Balance:");
JLabel amountLabel = new JLabel("Deposit/Withdraw Amount:");
nameField = new JTextField(15);
balanceField = new JTextField(10);
amountField = new JTextField(10);
JButton addButton = new JButton("Add Account");
JButton viewButton = new JButton("View Accounts");
JButton depositButton = new JButton("Deposit");
JButton withdrawButton = new JButton("Withdraw");
JButton referenceBankButton = new JButton("Reference Bank");
outputArea = new JTextArea(10, 40);
outputArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(outputArea);
// Set component positions
int x = 400, y = 150, width = 250, height = 30;
nameLabel.setBounds(x, y, 200, height);
nameField.setBounds(x + 220, y, width, height);
balanceLabel.setBounds(x, y + 50, 200, height);
balanceField.setBounds(x + 220, y + 50, width, height);
amountLabel.setBounds(x, y + 100, 200, height);
amountField.setBounds(x + 220, y + 100, width, height);
addButton.setBounds(x, y + 150, 180, 40);
viewButton.setBounds(x + 200, y + 150, 180, 40);
depositButton.setBounds(x, y + 200, 180, 40);
withdrawButton.setBounds(x + 200, y + 200, 180, 40);
referenceBankButton.setBounds(x + 100, y + 250, 200, 40);
scrollPane.setBounds(x - 100, y + 300, 700, 300);
// Database connection
try {
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/bank",
"root", "");
Statement stmt = connection.createStatement();
stmt.execute("CREATE TABLE IF NOT EXISTS accounts (id INT AUTO_INCREMENT
PRIMARY KEY, name VARCHAR(100), balance DOUBLE)");
} catch (SQLException e) {
JOptionPane.showMessageDialog(frame, "Database Connection Failed! " +
e.getMessage());
// Action Listeners
addButton.addActionListener(e -> addAccount());
viewButton.addActionListener(e -> viewAccounts());
depositButton.addActionListener(e -> deposit());
withdrawButton.addActionListener(e -> withdraw());
referenceBankButton.addActionListener(e -> openReferenceBank());
// Add components to frame
frame.add(nameLabel);
frame.add(nameField);
frame.add(balanceLabel);
frame.add(balanceField);
frame.add(amountLabel);
frame.add(amountField);
frame.add(addButton);
frame.add(viewButton);
frame.add(depositButton);
frame.add(withdrawButton);
frame.add(referenceBankButton);
frame.add(scrollPane);
frame.setVisible(true);
private void openReferenceBank() {
try {
Desktop.getDesktop().browse(new URI("https://www.hdfcbank.com"));
} catch (Exception e) {
JOptionPane.showMessageDialog(frame, "Failed to open reference bank website: " +
e.getMessage());
// Method to add an account
private void addAccount() {
String name = nameField.getText();
try {
double balance = Double.parseDouble(balanceField.getText());
PreparedStatement pstmt = connection.prepareStatement("INSERT INTO accounts
(name, balance) VALUES (?, ?)");
pstmt.setString(1, name);
pstmt.setDouble(2, balance);
pstmt.executeUpdate();
JOptionPane.showMessageDialog(frame, "Account Added Successfully");
nameField.setText("");
balanceField.setText("");
} catch (NumberFormatException | SQLException e) {
JOptionPane.showMessageDialog(frame, "Error: " + e.getMessage());
// Method to view all accounts
private void viewAccounts() {
outputArea.setText("ID | Name | Balance\n");
try {
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM accounts");
while (rs.next()) {
outputArea.append(rs.getInt("id") + " | " + rs.getString("name") + " | " +
rs.getDouble("balance") + "\n");
} catch (SQLException e) {
JOptionPane.showMessageDialog(frame, "Database Error: " + e.getMessage());
// Method to deposit money
private void deposit() {
String name = nameField.getText();
try {
double amount = Double.parseDouble(amountField.getText());
PreparedStatement pstmt = connection.prepareStatement("UPDATE accounts SET
balance = balance + ? WHERE name = ?");
pstmt.setDouble(1, amount);
pstmt.setString(2, name);
int rows = pstmt.executeUpdate();
if (rows > 0) {
JOptionPane.showMessageDialog(frame, "Deposit Successful");
} else {
JOptionPane.showMessageDialog(frame, "Account Not Found");
} catch (NumberFormatException | SQLException e) {
JOptionPane.showMessageDialog(frame, "Error: " + e.getMessage());
// Method to withdraw money
private void withdraw() {
String name = nameField.getText();
try {
double amount = Double.parseDouble(amountField.getText());
PreparedStatement checkStmt = connection.prepareStatement("SELECT balance
FROM accounts WHERE name = ?");
checkStmt.setString(1, name);
ResultSet rs = checkStmt.executeQuery();
if (rs.next()) {
double currentBalance = rs.getDouble("balance");
if (currentBalance >= amount) {
PreparedStatement pstmt = connection.prepareStatement("UPDATE accounts
SET balance = balance - ? WHERE name = ?");
pstmt.setDouble(1, amount);
pstmt.setString(2, name);
pstmt.executeUpdate();
JOptionPane.showMessageDialog(frame, "Withdrawal Successful");
} else {
JOptionPane.showMessageDialog(frame, "Insufficient Balance");
} else {
JOptionPane.showMessageDialog(frame, "Account Not Found");
} catch (NumberFormatException | SQLException e) {
JOptionPane.showMessageDialog(frame, "Error: " + e.getMessage());
// Other methods (addAccount, viewAccounts, deposit, withdraw) remain unchanged
public static void main(String[] args) {
new BankManagementSystem3();