Advertisement
Guest User

Untitled

a guest
Apr 20th, 2014
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6.  
  7. package pkg310project;
  8.  
  9. import java.io.BufferedReader;
  10. import java.io.IOException;
  11. import java.io.InputStreamReader;
  12. import java.sql.*;
  13.  
  14. public class Main {
  15.    
  16.     public static final String QUIT_INPUT = "7";
  17.     public static String TEST_MODE = "ON ";
  18.    
  19.     public static void main(String[] args) throws IOException, ClassNotFoundException, Exception {
  20.         String input;
  21.         InputStreamReader inputStreamReader = new InputStreamReader(System.in);
  22.         BufferedReader reader = new BufferedReader(inputStreamReader);
  23.         if (dbConnect() != 0) return;
  24.         do{
  25.             printMainMenu();
  26.             input = reader.readLine();
  27.             switch (input){
  28.                 case "1":
  29.                     addStudentMenu();
  30.                     break;
  31.                 case "2":
  32.                     break;
  33.                 case "3":
  34.                     break;
  35.                 case "4":
  36.                     break;
  37.                 case "5":
  38.                     break;
  39.                 case "6":
  40.                     if ("ON ".equals(TEST_MODE))
  41.                         TEST_MODE = "OFF";
  42.                     else
  43.                         TEST_MODE = "ON ";
  44.                     System.out.println("Test Mode is "+TEST_MODE+"!");
  45.                     break;
  46.                 case "7":
  47.                     System.out.println("Bye!");
  48.                     return;
  49.                 default:
  50.                     System.out.println("Not sure how to handle that input");
  51.             }
  52.         } while (!QUIT_INPUT.equals(input));
  53.     }
  54.     public static void printMainMenu(){        
  55.         System.out.println("|===============================||");
  56.         System.out.println("|-- CSCE 310 -------------------||");
  57.         System.out.println("|-- Project GUI ----------------||");
  58.         System.out.println("|-- Testmode: "+TEST_MODE+" --------------||");
  59.         System.out.println("|- 1.Add Student              --||");
  60.         System.out.println("|- 2.                         --||");
  61.         System.out.println("|- 3.                         --||");
  62.         System.out.println("|- 4.                         --||");
  63.         System.out.println("|- 5.                         --||");
  64.         System.out.println("|- 6. Toggle Testmode         --||");
  65.         System.out.println("|- 7. Quit                    --||");
  66.         System.out.println("|-------------------------------||");
  67.         System.out.println("|===============================||");
  68.     }
  69.     public static Connection connection = null;
  70.    
  71.     public static int addStudentMenu() throws IOException{
  72.         InputStreamReader inputStreamReader = new InputStreamReader(System.in);
  73.         BufferedReader reader = new BufferedReader(inputStreamReader);
  74.         String name, department, classification, major;
  75.         System.out.println("What should the student be called?");
  76.         name = reader.readLine();
  77.         System.out.println("Which department does the the student belong to?");
  78.         department = reader.readLine();
  79.         System.out.println("Which major does the student belong to?");
  80.         classification = reader.readLine();
  81.         System.out.println("What is the student's classification?");
  82.         major = reader.readLine();
  83.         try{
  84.             int id = addUser(name, department);
  85.             if (id < 0)
  86.                 throw new Exception("Couldn't add user");
  87.             else{
  88.                 System.out.println("Added user"+id);
  89.                 return addStudent(id, classification, major);
  90.             }
  91.         } catch (SQLException s){
  92.             return -1;
  93.         } catch (Exception z){
  94.             return -2;
  95.         }
  96.     }
  97.    
  98.     public static int addStudent(int id, String classification, String major) throws SQLException, Exception{
  99.         if (connection == null){
  100.             throw new Exception("Not Connected");
  101.         }
  102.         String addUserStatment = "INSERT INTO students" + (("ON ".equals(TEST_MODE)) ? "_test" : "")
  103.                 + "(student_id, classification, major) VALUES"
  104.                 + "('"+ id +"', '"+ classification +"', '"+ major+"')";
  105.        
  106.         Statement statement = connection.createStatement();
  107.         statement.executeUpdate(addUserStatment);
  108.         return 0;
  109.     }
  110.    
  111.     public static int addUser(String name, String department) throws SQLException, Exception{
  112.         if (connection == null){
  113.             throw new Exception("Not Connected");
  114.         }
  115.         String addUserStatment = "INSERT INTO users" + (("ON ".equals(TEST_MODE)) ? "_test" : "")
  116.                 + "(name, department) VALUES"
  117.                 + "('"+ name +"', '"+ department+"')";
  118.        
  119.         Statement statement = connection.createStatement();
  120.         statement.executeUpdate(addUserStatment, Statement.RETURN_GENERATED_KEYS);
  121.         ResultSet res;
  122.         int autoIncKeyFromApi = -1;
  123.         res = statement.getGeneratedKeys();
  124.         if (res.next()){
  125.             autoIncKeyFromApi = res.getInt(1);
  126.         } else {
  127.             throw new Exception("addUser Error");
  128.         }
  129.         res.close();
  130.         return autoIncKeyFromApi;
  131.     }
  132.     public static int dbConnect() throws ClassNotFoundException, Exception{
  133.         try {
  134.             Class.forName("com.mysql.jdbc.Driver");
  135.             connection = DriverManager.getConnection("jdbc:mysql://database2.cs.tamu.edu/jose6929","jose6929", "Password2!");
  136.     } catch (SQLException e) {
  137.             throw new Exception("Couldn't Connect");
  138.     }
  139.     return 0;
  140.     }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement