OneCompiler

Mobile Demo

124

import java.sql.*;

public class MobileDatabase {
// JDBC URL, username, and password of MySQL server
private static final String URL = "jdbc:mysql://localhost:3306/mobile_database";
private static final String USER = "username";
private static final String PASSWORD = "password";

// JDBC variables for opening, closing and managing connection
private static Connection connection;
private static Statement statement;
private static ResultSet resultSet;

public static void main(String[] args) {
    try {
        connection = DriverManager.getConnection(URL, USER, PASSWORD);
        statement = connection.createStatement();

        createMobileTable();

        if (args.length < 1) {
            System.out.println("Usage: java MobileDatabase <operation>");
            return;
        }

        int operation = Integer.parseInt(args[0]);

        switch (operation) {
            case 1:
                insertMobile();
                break;
            case 2:
                modifyMobile();
                break;
            case 3:
                deleteMobile();
                break;
            case 4:
                searchMobile();
                break;
            case 5:
                viewAllMobiles();
                break;
            case 6:
                System.out.println("Exiting program");
                break;
            default:
                System.out.println("Invalid operation");
        }

        statement.close();
        connection.close();
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

private static void createMobileTable() throws SQLException {
    String createTableSQL = "CREATE TABLE IF NOT EXISTS MOBILE (" +
            "Model_Number INT PRIMARY KEY, " +
            "Model_Name VARCHAR(255), " +
            "Model_Color VARCHAR(255), " +
            "Sim_Type VARCHAR(50), " +
            "Network_Type VARCHAR(50), " +
            "Battery_Capacity INT, " +
            "Internal_Storage INT, " +
            "RAM INT, " +
            "Processor_Type VARCHAR(255))";

    statement.execute(createTableSQL);
}

private static void insertMobile() throws SQLException {
    // Implement insertion logic here
}

private static void modifyMobile() throws SQLException {
    // Implement modification logic here
}

private static void deleteMobile() throws SQLException {
    // Implement deletion logic here
}

private static void searchMobile() throws SQLException {
    // Implement search logic here
}

private static void viewAllMobiles() throws SQLException {
    // Implement view all logic here
}

}