Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package laboratory2;
- import java.io.BufferedReader;
- import java.io.FileInputStream;
- import java.io.File;
- import java.io.FileReader;
- import java.sql.PreparedStatement;
- import java.sql.SQLException;
- import java.sql.DriverManager;
- import java.sql.Connection;
- import java.sql.Statement;
- import java.util.Scanner;
- import java.util.Vector;
- public class DatabaseGenerator {
- public static void main(String[] args)throws Exception
- {
- String s = new String();
- StringBuffer sb = new StringBuffer();
- SimpleDataSource.init("database.properties");
- // be sure to not have line starting with "--" or "/*" or any other non aplhabetical character
- if (args.length != 0)
- {
- File file = new File(args[0]);
- System.out.println("ARGS: " + args[0]);
- FileReader fr = new FileReader(file);
- BufferedReader br = new BufferedReader(fr);
- s = br.readLine();
- while(s != null)
- {
- sb.append(s);
- s = br.readLine();
- }
- br.close();
- }
- // here is our splitter ! We use ";" as a delimiter for each request
- // then we are sure to have well formed statements
- String[] stmts = sb.toString().split(";");
- try
- {
- Connection c = SimpleDataSource.getConnection();
- Statement st = c.createStatement();
- for(int i = 0; i < stmts.length; i++)
- {
- if (stmts[i].isEmpty()) continue;
- System.out.println("QQQQQ: " + stmts[i]);
- st.execute(stmts[i]);
- }
- } catch(Exception e) {
- System.out.println("*** Error : "+e.toString());
- System.out.println("*** ");
- System.out.println("*** Error : ");
- e.printStackTrace();
- System.out.println("################################################");
- }
- Scanner input = new Scanner(System.in);
- boolean done = false;
- while(!done)
- {
- System.out.println(" - 1 to add a student");
- System.out.println(" - 2 to show all students in the database");
- System.out.println(" - 3 to remove student");
- System.out.println(" - 4 to add a module");
- System.out.println(" - 5 to show all modules in the database");
- System.out.println(" - 6 to remove module");
- System.out.println(" - 7 enroll student to module");
- System.out.println(" - 8 Remove student from module");
- System.out.println(" - -1 to quit application");
- System.out.println("Choice: ");
- int choice = input.nextInt();
- switch (choice)
- {
- case 1:
- {
- SimpleDataSource.executeAddStudentQuery("S10349", "Mohammed Weed", "BSc Computer Science");
- System.out.println("Student Added");
- break;
- }
- case 2:
- {
- Vector<String> buffer = SimpleDataSource.executeGetStudentListQuery();
- for(int i = 0; i < buffer.size(); i++)
- {
- System.out.println( ". " + buffer.elementAt(i));
- }
- break;
- }
- case 3:
- {
- SimpleDataSource.executeRemoveStudentUpdate("S10349");
- System.out.println("Student Removed");
- break;
- }
- case 4:
- {
- SimpleDataSource.executeAddModuleQuery("CS205", "Computer Laboratoty",10);
- System.out.println("Module Added");
- break;
- }
- case 5:
- {
- Vector<String> buffer = SimpleDataSource.executeGetModuleListQuery();
- for(int i = 0; i < buffer.size(); i++)
- {
- System.out.println( ". " + buffer.elementAt(i));
- }
- break;
- }
- case 6:
- {
- SimpleDataSource.executeRemoveModuleUpdate("CS205");
- System.out.println("Module Removed");
- break;
- }
- case 7:
- {
- SimpleDataSource.executeAddStudentModuleQuery("S10349", "CS204");
- System.out.println("Student Registered to Module");
- break;
- }
- case 8:
- {
- SimpleDataSource.executeRemoveStudentModuleUpdate("S10349");
- System.out.println("Student Removed From Module");
- break;
- }
- case -1:
- {
- done = true;
- break;
- }
- default:
- break;
- }
- }
- }
- }
- /*
- public static void main(String[] args) throws Exception {}
- private static final String DRIVER_NAME = "com.mysql.jdbc.Driver";
- static
- {
- try
- {
- Class.forName(DRIVER_NAME).newInstance();
- System.out.println("*** Driver loaded");
- }
- catch(Exception e)
- {
- System.out.println("*** Error : "+e.toString());
- System.out.println("*** ");
- System.out.println("*** Error : ");
- e.printStackTrace();
- }
- }
- private static final String url = "jdbc:mysql://localhost:3306/university";
- private static final String user = "root";
- private static final String password = "puss";
- private static String INSTRUCTIONS = new String();
- public static Connection getConnection() throws SQLException
- {
- return DriverManager.getConnection(url, user, password);
- }
- public static void resetDatabase() throws SQLException
- {
- String s = new String();
- StringBuffer sb = new StringBuffer();
- try
- {
- FileReader fr = new FileReader(new File("database.sql"));
- // be sure to not have line starting with "--" or "/*" or any other non aplhabetical character
- BufferedReader br = new BufferedReader(fr);
- while((s = br.readLine()) != null)
- {
- sb.append(s);
- }
- br.close();
- // here is our splitter ! We use ";" as a delimiter for each request
- // then we are sure to have well formed statements
- String[] inst = sb.toString().split(";");
- Connection c = SimpleDataSource.getConnection();
- Statement st = c.createStatement();
- for(int i = 0; i<inst.length; i++)
- {
- // we ensure that there is no spaces before or after the request string
- // in order to not execute empty statements
- if(!inst[i].trim().equals(""))
- {
- st.executeUpdate(inst[i]);
- System.out.println(">>"+inst[i]);
- }
- }
- }
- catch(Exception e)
- {
- System.out.println("*** Error : "+e.toString());
- System.out.println("*** ");
- System.out.println("*** Error : ");
- e.printStackTrace();
- System.out.println("################################################");
- System.out.println(sb.toString());
- }
- }
- }
- *
- */
Add Comment
Please, Sign In to add comment