Write a Java program using JDBC to manage saving accounts of a bank. Make necessary assumptions. Make provisions of exception handling in your program.
Sure! Here’s a detailed Java program using JDBC to manage saving accounts for a bank. This program will include operations such as creating an account, depositing money, withdrawing money, and viewing account details. I will also handle exceptions to ensure the program operates smoothly.
### Java Program Using JDBC for Bank Account Management
Assumptions:
1. We are using a MySQL database.
2. The database has a table named `accounts` with columns: `account_id`, `account_holder`, `balance`.
Database Table Creation SQL:
```sql
CREATE TABLE accounts (
account_id INT AUTO_INCREMENT PRIMARY KEY,
account_holder VARCHAR(100),
balance DECIMAL(15, 2)
);
```
Java Program:
```java
import java.sql.*;
public class BankAccountManager {
private static final String URL = "jdbc:mysql://localhost:3306/bank";
private static final String USER = "root";
private static final String PASSWORD = "password";
// Establish database connection
private Connection connect() throws SQLException {
return DriverManager.getConnection(URL, USER, PASSWORD);
}
// Create a new account
public void createAccount(String holder, double initialBalance) {
String sql = "INSERT INTO accounts (account_holder, balance) VALUES (?, ?)";
try (Connection conn = connect(); PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, holder);
pstmt.setDouble(2, initialBalance);
pstmt.executeUpdate();
System.out.println("Account created successfully.");
} catch (SQLException e) {
_______ ________ ______ ______ __________ ________.
___ ____ ____ _____ _______ ____ ________ ______ _____.
__________ ______ ______ ____ __________ ____ _______ _________.
_____ _______ ________ _______ _____ ______ _______ ____ ______ ______.
_____ ___ ________ ______ ___ __________ __________.
__________ ____ _______ _______ _________ ____ _________.
_________ __________ ___ _____ ________ ____ __________ ___.
________ _________ _________ __________ ________.
_________ ______ ___ _________ _____ ________ ______.
______ _________ _______ ___ ___ _______ __________ ___.
__________ ____ ____ _____ ______ ___ ___ ____.
__________ _____ ___ ___ _________.
_____ _______ __________ ___ ________.
_____ _________ ________ ______ __________ ______.
_______ ______ _____ _____ _____ ______ ______.
_________ __________ _____ _____ ___ __________ _______.
________ ___ ___ ___ ____ ____ _________ ______ _______ __________.
_____ __________ ___ ___ _______.
____ __________ ____ ____ __________.
______ ________ __________ __________ _________ __________ ___.
_______ ______ ________ __________ _____ __________ _________ _____ ____ _______.
______ __________ __________ ___ ______ ____ ___ _____ _____ ______ __________ ___.
___ ____ ___ _____ _______ _______ __________ _______ _________.
______ _______ ____ _____ ___ ________ _______ __________ ___ ____.
____ _____ ______ _____ _________ ____ ____ __________ ___ _______ ________ __________.
__________ ___ __________ ____ _____ ______.
______ __________ ____ ______ ___ __________.
______ ___ ____ ________ _________ __________ _________ __________ __________ ____ _____ ____.
_________ __________ ___ _______ _______ _________.
__________ ___ ____ __________ ___ ___ ___ _________ ______ __________ _______.
________ ___ __________ ______ _________ ________ _____ ___ __________.
__________ ___ _________ __________ ___ __________.
_________ _____ _________ ________ __________.
_____ ________ _____ __________ _____ _______ _______.
_________ ________ ______ ____ ________ ______ ________ __________ ______ ______.
______ __________ ________ ______ _____ __________ ___ ______ ___ ______ ______.
__________ _______ _____ ______ ________ _________ _____ _______ __________ __________ ______.
_____ _____ ______ _____ _______ __________ ____ ________ ___ _______.
_____ ________ __________ _______ ___ _________ _________ __________ __________.
_______ ________ ______ _____ ___ _________ _________ _____.
_____ ____ ___ _____ _________ _____ ___.
___ __________ __________ ___ ________ _____ _______ ________ _____ _______ __________ __________.
__________ ___ _____ _______ ____ ___ ______ _______.
____ _______ ________ _____ ____ __________ ______ ___ ________ ______ ___.
_____ ________ ________ ______ ___.
__________ __________ _________ ___ __________ ____ _____ _________ ______ _____.
__________ _______ _________ ____ _________ ____ __________ _____ _____.
____ __________ _____ _________ ____ _______ ________ __________ __________.
_________ ____ ________ ________ _________ _______ ______ _______.
_________ _____ ________ ______ ________ _____ __________ __________ _______.
____ _____ _________ ______ ____ ________ _________ __________ ___ _____ ________.
_______ ______ __________ ____ _______ _____.
___ _______ ___ ___ ____ ________ ______.
_____ ___ _______ _____ _________ _____.
_____ ______ __________ ________ ________ ____ ____ _______.
___ __________ ____ __________ ______ _________ ____ ___ ___ __________ _____ ______.
_________ _______ __________ ______ ______ __________ ________ _________.
_______ ______ __________ ___ _____ ___ ___.
_________ _____ __________ ______ _______ _____.
___ ____ ________ ________ _______ _________.
______ _________ ______ ______ _____ _________ ______ _____ _______ _____.
________ ________ ____ ___ ________ ___ __________ _______ ________ ______.
_________ ______ ____ _______ _________ _________ _______ _______ ____ ____.
_________ ________ ____ ___ ___ _________ _____ _________ _______.
_______ ______ ________ _________ ________ ________ ______ __________.
_________ ______ ____ __________ ___ ____ ________.
____ _______ _____ ______ ______ _______ ____.
________ ______ _____ _________ _____ ______ ___ _______ _______ __________ ______.
______ ________ ___ _________ __________ ____ _________ _____ _____.
______ _____ ___ ______ _________.
_______ ___ ____ ________ ______ _______.
_____ _______ ________ _______ __________ ________ _______ ____.
________ ____ _____ ________ _________ _________ _____ _________ ____ ___.
________ _________ __________ __________ _______ ____ __________ ___ _____ ________ __________.
___ _________ ______ ________ ___ _________ __________ _________.
__________ ________ __________ _______ _____ _____ _______.
_____ ______ ____ _________ _________ ____ _______ _________ __________ _________.
_________ ________ ___ _________ ____ __________ __________ ___ ________ _____ ______ _________.
_______ _____ ______ _____ _______ __________ ______ ________.
_________ ____ ___ ___ __________ ______.
______ ______ ______ _____ __________ ________ ____ ________ _______.
________ ___ _________ ______ _______ ____.
________ ___ _____ ___ ___ _____ __________.
________ ______ ________ ________ __________.
_____ _____ _________ __________ ________.
_____ _______ ______ ________ ______ ____ ______.
_______ _______ _______ _____ ______.
______ __________ _______ ______ ____.
____ _______ ___ ______ _______ __________ __________ _____ ____ ______ ______ ____.
__________ ______ _______ __________ _________ _______ _______ _________ ____ ___ _________.
_____ ______ _____ _______ ______ _________ ______ __________.
________ ____ _________ ___ ___ ______ _____ ______ ________ __________.
__________ _____ __________ ___ _______ ___ _____ ______ ____.
___ ______ ____ ___ ___ _________ ______.
_______ _______ _____ ___ _______ _______.
__________ __________ ______ __________ ________.
________ ______ __________ ______ _______ _____ ________ ______.
_______ _________ __________ ___ _______ ______ __________ _______ ______ ________.
_____ ________ ___ ____ _______ ______.
______ ________ __________ ___ __________ _________ _____ ____ ____ _______.
____ _____ _________ ______ ___ _____.
_____ _______ _________ ________ _________ ________ _______ _____ ____ _____.
__________ _____ _______ _____ _____.
_______.
Get Full Answer on WhatsApp