Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package pkg310project;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.sql.*;
- public class Main {
- public static final String QUIT_INPUT = "7";
- public static String TEST_MODE = "ON ";
- public static void main(String[] args) throws IOException, ClassNotFoundException, Exception {
- String input;
- InputStreamReader inputStreamReader = new InputStreamReader(System.in);
- BufferedReader reader = new BufferedReader(inputStreamReader);
- if (dbConnect() != 0) return;
- do{
- printMainMenu();
- input = reader.readLine();
- switch (input){
- case "1":
- addStudentMenu();
- break;
- case "2":
- break;
- case "3":
- break;
- case "4":
- break;
- case "5":
- break;
- case "6":
- if ("ON ".equals(TEST_MODE))
- TEST_MODE = "OFF";
- else
- TEST_MODE = "ON ";
- System.out.println("Test Mode is "+TEST_MODE+"!");
- break;
- case "7":
- System.out.println("Bye!");
- return;
- default:
- System.out.println("Not sure how to handle that input");
- }
- } while (!QUIT_INPUT.equals(input));
- }
- public static void printMainMenu(){
- System.out.println("|===============================||");
- System.out.println("|-- CSCE 310 -------------------||");
- System.out.println("|-- Project GUI ----------------||");
- System.out.println("|-- Testmode: "+TEST_MODE+" --------------||");
- System.out.println("|- 1.Add Student --||");
- System.out.println("|- 2. --||");
- System.out.println("|- 3. --||");
- System.out.println("|- 4. --||");
- System.out.println("|- 5. --||");
- System.out.println("|- 6. Toggle Testmode --||");
- System.out.println("|- 7. Quit --||");
- System.out.println("|-------------------------------||");
- System.out.println("|===============================||");
- }
- public static Connection connection = null;
- public static int addStudentMenu() throws IOException{
- InputStreamReader inputStreamReader = new InputStreamReader(System.in);
- BufferedReader reader = new BufferedReader(inputStreamReader);
- String name, department, classification, major;
- System.out.println("What should the student be called?");
- name = reader.readLine();
- System.out.println("Which department does the the student belong to?");
- department = reader.readLine();
- System.out.println("Which major does the student belong to?");
- classification = reader.readLine();
- System.out.println("What is the student's classification?");
- major = reader.readLine();
- try{
- int id = addUser(name, department);
- if (id < 0)
- throw new Exception("Couldn't add user");
- else{
- System.out.println("Added user"+id);
- return addStudent(id, classification, major);
- }
- } catch (SQLException s){
- return -1;
- } catch (Exception z){
- return -2;
- }
- }
- public static int addStudent(int id, String classification, String major) throws SQLException, Exception{
- if (connection == null){
- throw new Exception("Not Connected");
- }
- String addUserStatment = "INSERT INTO students" + (("ON ".equals(TEST_MODE)) ? "_test" : "")
- + "(student_id, classification, major) VALUES"
- + "('"+ id +"', '"+ classification +"', '"+ major+"')";
- Statement statement = connection.createStatement();
- statement.executeUpdate(addUserStatment);
- return 0;
- }
- public static int addUser(String name, String department) throws SQLException, Exception{
- if (connection == null){
- throw new Exception("Not Connected");
- }
- String addUserStatment = "INSERT INTO users" + (("ON ".equals(TEST_MODE)) ? "_test" : "")
- + "(name, department) VALUES"
- + "('"+ name +"', '"+ department+"')";
- Statement statement = connection.createStatement();
- statement.executeUpdate(addUserStatment, Statement.RETURN_GENERATED_KEYS);
- ResultSet res;
- int autoIncKeyFromApi = -1;
- res = statement.getGeneratedKeys();
- if (res.next()){
- autoIncKeyFromApi = res.getInt(1);
- } else {
- throw new Exception("addUser Error");
- }
- res.close();
- return autoIncKeyFromApi;
- }
- public static int dbConnect() throws ClassNotFoundException, Exception{
- try {
- Class.forName("com.mysql.jdbc.Driver");
- connection = DriverManager.getConnection("jdbc:mysql://database2.cs.tamu.edu/jose6929","jose6929", "Password2!");
- } catch (SQLException e) {
- throw new Exception("Couldn't Connect");
- }
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement