Write a Java program that establishes a connection to a MariaDB database, creates a "users" table with columns for id, name, email, and age, and inserts sample data into the table. The program should also include functionality to query all users and display their information.
PLAINFORMAT
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class MariaDBConnection {

    public static void main(String[] args) {
        String url = "jdbc:mariadb://localhost:3306/testdb";
        String user = "root";
        String password = "your_password";

        try (Connection connection = DriverManager.getConnection(url, user, password)) {
            System.out.println("Connected to the database");

            // Create table
            createUsersTable(connection);

            // Insert sample data
            insertSampleData(connection);

            // Query all users and display their information
            queryAllUsers(connection);
        } catch (SQLException e) {
            System.err.println("Error connecting to the database");
            e.printStackTrace();
        }
    }

    private static void createUsersTable(Connection connection) throws SQLException {
        String sql = "CREATE TABLE IF NOT EXISTS users (" +
                "id INT AUTO_INCREMENT PRIMARY KEY," +
                "name VARCHAR(255) NOT NULL," +
                "email VARCHAR(255) UNIQUE NOT NULL," +
                "age INT NOT NULL" +
                ");";

        try (Statement statement = connection.createStatement()) {
            statement.execute(sql);
            System.out.println("Created 'users' table");
        }
    }

    private static void insertSampleData(Connection connection) throws SQLException {
        String sql = "INSERT INTO users (name, email, age) VALUES" +
                "('John Doe', 'john.doe@example.com', 30)," +
                "('Jane Smith', 'jane.smith@example.com', 28);";

        try (Statement statement = connection.createStatement()) {
            int rowsAffected = statement.executeUpdate(sql);
            System.out.println("Inserted " + rowsAffected + " rows of sample data");
        }
    }

    private static void queryAllUsers(Connection connection) throws SQLException {
        String sql = "SELECT * FROM users;";

        try (Statement statement = connection.createStatement()) {
            ResultSet resultSet = statement.executeQuery(sql);

            System.out.println("All users:");
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                String email = resultSet.getString("email");
                int age = resultSet.getInt("age");

                System.out.printf("%d: %s (%s) - %d years old%n", id, name, email, age);
            }
        }
    }
}