MOCK

 avatar
Akash
java
2 years ago
4.5 kB
0
Indexable
Never
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;

public class JDBCCon {

    public static void insert(int id, int salary, String firstname) throws SQLException {
        String sql;

        sql = "INSERT into EMP values (" + Integer.toString(id) + ", " + Integer.toString(salary) + ", \"" + firstname + "\")";

        stmt.executeUpdate(sql);
    }

    public static void update(int id, int salary) throws SQLException {
        String sql;

        sql = "Update EMP SET salary = " + Integer.toString(salary) + " WHERE Emp_id = " + Integer.toString(id);

        stmt.executeUpdate(sql);
    }

    public static void delete(int id) throws SQLException {
        String sql;

        sql = "Delete FROM EMP WHERE Emp_id = " + Integer.toString(id);

        stmt.executeUpdate(sql);
    }

    public static void read() throws SQLException {
        String sql;
        int id = 0;
        int salary = 0;
        String firstname = "";

        sql = "SELECT * FROM EMP";
        ResultSet rs = stmt.executeQuery(sql);

        while (rs.next()) {

            id = rs.getInt("Emp_id");
            salary = rs.getInt("salary");
            firstname = rs.getString("fname");

            System.out.print("ID: " + id);
            System.out.print(", Age: " + salary);
            System.out.print(", First Name: " + firstname);
            System.out.println();
        }

        rs.close();
    }
    
    static Connection conn = null;
    static Statement stmt = null;

    public static void main(String[] args) {

        try {

            Class.forName("com.mysql.cj.jdbc.Driver");

            System.out.println("Connecting to database...");
            conn = DriverManager.getConnection("jdbc:mysql://10.10.12.108:3306/te31141Adb","te31141A","te31141A");

            System.out.println("Creating statement...");
            stmt = conn.createStatement();
            int id = 0;
            int salary = 0;
            String firstname = "";

            Scanner sc = new Scanner(System.in);
            boolean loop = true;
            while (loop) {
                System.out.println("\n----MENU----");
                System.out.println("1. Display Table");
                System.out.println("2. Insert Values Into Table");
                System.out.println("3. Update Salary");
                System.out.println("4. Delete a Value from Table");
                System.out.println("-1.Exit");
                System.out.print("Enter Your Choice: ");
                Integer choice = sc.nextInt();
                switch (choice) {
                    case 1:
                        read();
                        break;

                    case 2:
                        System.out.print("Enter ID: ");
                        id = sc.nextInt();
                        System.out.print("Enter Salary: ");
                        salary = sc.nextInt();
                        System.out.print("Enter Name: ");
                        firstname = sc.next();
                        insert(id, salary, firstname);
                        break;

                    case 3:
                        System.out.print("Enter ID: ");
                        id = sc.nextInt();
                        System.out.print("Enter Salary: ");
                        salary = sc.nextInt();
                        update(id, salary);
                        break;

                    case 4:
                        System.out.print("Enter ID: ");
                        id = sc.nextInt();
                        delete(id);
                        break;

                    case -1:
                        System.out.println("Thak You!");
                        sc.close();
                        loop = false;
                }
            }
            stmt.close();
            conn.close();
        } 
        catch (SQLException se) {

            se.printStackTrace();
        } 
        catch (Exception e) {

            e.printStackTrace();
        } 
        finally {

            try {
                if (stmt != null)
                    stmt.close();
            } 
            catch (SQLException se2) {}
            try {
                if (conn != null)
                    conn.close();
            } 
            catch (SQLException se) {
                se.printStackTrace();
            }
        }
        System.out.println("Good Bye!");
    }
}