### Instruction:
Write a Java program that simulates a simple ATM machine. Users can enter their PIN, check their balance, deposit money, and withdraw money. The program should interact with a MySQL database to store user information and transaction history.


### Response:

To create a Java program that simulates a simple ATM machine, you'll need to have the following:

1. Java Development Kit (JDK) installed on your computer.
2. MySQL Server and MySQL Connector/J (JDBC driver for MySQL) installed.

Here's an example of how you can create this program:

1. First, create a new database in MySQL and a table named `users` with columns `id`, `pin`, `balance`, and `transaction_history`.

```sql
CREATE DATABASE atm;
USE atm;

CREATE TABLE users (
  id INT AUTO_INCREMENT PRIMARY KEY,
  pin VARCHAR(4) NOT NULL,
  balance DECIMAL(10, 2) NOT NULL DEFAULT 0,
  transaction_history TEXT
);
```

2. Add some sample data to the `users` table.

```sql
INSERT INTO users (pin, balance)
VALUES ('1234', 1000), ('5678', 2000);
```

3. Create a Java class named `ATM.java` and add the following code:

```java
import java.sql.*;
import java.util.Scanner;

public class ATM {
    private static final String DB_URL = "jdbc:mysql://localhost:3306/atm";
    private static final String DB_USER = "root"; // Replace with your MySQL username
    private static final String DB_PASSWORD = ""; // Replace with your MySQL password

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("Welcome to the ATM!");
        System.out.print("Enter your PIN: ");
        String pin = scanner.nextLine();

        try (Connection connection = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD)) {
            PreparedStatement statement = connection.prepareStatement("SELECT * FROM users WHERE pin = ?");
            statement.setString(1, pin);
            ResultSet resultSet = statement.executeQuery();

            if (!resultSet.next()) {
                System.out.println("Invalid PIN. Exiting...");
                return;
            }

            int userId = resultSet.getInt("id");
            double balance = resultSet.getDouble("balance");

            while (true) {
                System.out.println("\nChoose an option:");
                System.out.println("1. Check Balance");
                System.out.println("2. Deposit Money");
                System.out.println("3. Withdraw Money");
                System.out.println("4. Exit");
                System.out.print("Enter your choice: ");
                int choice = scanner.nextInt();

                switch (choice) {
                    case 1:
                        System.out.printf("Your current balance is $%.2f\n", balance);
                        break;
                    case 2:
                        System.out.print("Enter the amount to deposit: ");
                        double depositAmount = scanner.nextDouble();
                        balance += depositAmount;
                        updateBalance(connection, userId, balance);
                        System.out.printf("$%.2f deposited successfully.\n", depositAmount);
                        break;
                    case 3:
                        System.out.print("Enter the amount to withdraw: ");
                        double withdrawalAmount = scanner.nextDouble();
                        if (withdrawalAmount > balance) {
                            System.out.println("Insufficient funds.");
                        } else {
                            balance -= withdrawalAmount;
                            updateBalance(connection, userId, balance);
                            System.out.printf("$%.2f withdrawn successfully.\n", withdrawalAmount);
                        }
                        break;
                    case 4:
                        System.out.println("Thank you for using our ATM. Goodbye!");
                        return;
                    default:
                        System.out.println("Invalid choice. Please try again.");
                }
            }
        } catch (SQLException e) {
            System.err.println("Error connecting to the database: " + e.getMessage());
        }
    }

    private static void updateBalance(Connection connection, int userId, double newBalance) throws SQLException {
        PreparedStatement statement = connection.prepareStatement("UPDATE users SET balance = ? WHERE id = ?");
        statement.setDouble(1, newBalance);
        statement.setInt(2, userId);
        statement.executeUpdate();
    }
}
```

4. Compile and run the program:

```bash
javac -cp /path/to/mysql-connector-java-x.x.xx.jar ATM.java
java -cp .:/path/to/mysql-connector-java-x.x.xx.jar ATM
```

Replace `/path/to/mysql-connector-java-x.x.xx.jar` with the path to your MySQL Connector/J JAR file.

Now you have a simple Java program that simulates an ATM machine using a MySQL database. Users can enter their PIN, check their balance, deposit money, and withdraw money. The program interacts with the MySQL database to store user information and transaction history.