Advertisement
Guest User

Untitled

a guest
Dec 9th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.76 KB | None | 0 0
  1. import java.sql.*;
  2. import java.util.Scanner;
  3. import java.util.concurrent.TimeUnit;
  4.  
  5. final class Main {
  6.     final static String user = "jlb6776";
  7.     final static String password = "Apple123";
  8.     final static String db = "";
  9.     final static String jdbc = "jdbc:mysql://localhost:3306/"+db+"?user="+user+"&password="+password;
  10.  
  11.     final static Scanner input = new Scanner(System.in);
  12.  
  13.     public static void main ( String[] args ) throws Exception {
  14.         System.out.println("Let start work with Java and JDBC.");
  15.  
  16.  
  17.         Class.forName("com.mysql.jdbc.Driver").newInstance();
  18.         Connection con = DriverManager.getConnection(jdbc);
  19.         Statement stmt = con.createStatement();
  20.         ResultSet rs = stmt.executeQuery("select * from Plane");
  21.         while (rs.next())
  22.             System.out.println(rs.getString("Maker")+" "+rs.getString("Model"));
  23.         rs.close();
  24.  
  25.  
  26.  
  27.         try {
  28.             Statement st = con.createStatement();
  29.             String code = " Create VIEW vp As select * from Plane";
  30.             st.executeUpdate(code);
  31.  
  32.             System.out.println(" VIEW successfully created!");
  33.         } catch (SQLException s) {
  34.             System.out.println("VIEW already exists!");
  35.         }
  36.  
  37.  
  38.         //stmt.close();
  39.  
  40.  
  41.         while(true){
  42.             int choice = 0;
  43.             System.out.println("Please Type One of the Following Numbers and Press Enter: ");
  44.             System.out.println("1. Check if Pilot is busy on a certain day and show the pilot assignments for this day");
  45.             System.out.println("2. Assign a Pilot to a flight leg instance");
  46.             System.out.println("3. Add a Pilot");
  47.             System.out.println("4. Quit");
  48.  
  49.             try{
  50.                 choice = input.nextInt();
  51.             }
  52.             catch(Exception e){
  53.                 System.out.println("Please Enter a number");
  54.                 TimeUnit.SECONDS.sleep(1);
  55.                 input.next();
  56.                 continue;
  57.             }
  58.             if(choice < 1 || choice > 4){
  59.                 System.out.println("Please Enter 1-4");
  60.                 TimeUnit.SECONDS.sleep(1);
  61.                 continue;
  62.             }
  63.  
  64.             if(choice == 1){
  65.                 System.out.println("Please Enter the Pilots Name");
  66.                 String name = input.next();
  67.                 System.out.println("Please Enter the Date in the Following Format(): ");
  68.                 String date = input.next();
  69.  
  70.                 Statement flyAssignments = con.createStatement();
  71.                 var query = flyAssignments.executeQuery("select * from PilotFlyAssignments");
  72.                 while (query.next())
  73.                     System.out.println(query.getString("PilotID")+" "+query.getString("Pilot_Name"));
  74.  
  75.                 query.close();
  76.                 flyAssignments.close();
  77.  
  78.                 input.next();
  79.                 continue;
  80.             }
  81.             else if(choice == 2){
  82.                 System.out.println("ID of Pilot: ");
  83.                 String pilotId = input.next();
  84.                 System.out.println("Flight Number: ");
  85.                 String flno = input.next();
  86.                 System.out.println("Seq: ");
  87.                 String seq = input.next();
  88.                 System.out.println("Flight Date");
  89.                 String fldate = input.next();
  90.                 System.out.println("Departure Time");
  91.                 String depart = input.next();
  92.                 System.out.println("Arrival Time");
  93.                 String arrive = input.next();
  94.  
  95.                 Statement addPilot = con.createStatement();
  96.                 String thing = "insert into FlightLegInstance values(" + flno + ", " + seq + ", " + fldate + ", " + depart + ", " + arrive + ", " + pilotId + ");";
  97.                 var query = addPilot.executeQuery(thing);
  98.                 query.close();
  99.                 addPilot.close();
  100.                 System.out.println("Table Updated ");
  101.                 TimeUnit.SECONDS.sleep(1);
  102.                 continue;
  103.             }
  104.             else if(choice == 3){
  105.                 System.out.println("Enter Id");
  106.                 String id = input.next();
  107.                 System.out.println("Enter Name");
  108.                 String name = input.next();
  109.                 System.out.println("Enter Date hired");
  110.                 String date = input.next();
  111.  
  112.                 Statement addPilot = con.createStatement();
  113.                 String thing = "insert into Pilot values(" + id + ", " + name + ", " + date + ");";
  114.                 var query = addPilot.executeQuery(thing);
  115.  
  116.                 System.out.println("Pilot Added");
  117.                 TimeUnit.SECONDS.sleep(1);
  118.                 continue;
  119.             }
  120.             else{
  121.                 con.close();
  122.                 System.exit(0);
  123.             }
  124.         }
  125.     }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement