Guest User

Untitled

a guest
Mar 16th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.18 KB | None | 0 0
  1.  
  2. package laboratory2;
  3.  
  4. import java.io.BufferedReader;
  5. import java.io.FileInputStream;
  6. import java.io.File;
  7. import java.io.FileReader;
  8. import java.sql.PreparedStatement;
  9. import java.sql.SQLException;
  10. import java.sql.DriverManager;
  11. import java.sql.Connection;
  12. import java.sql.Statement;
  13. import java.util.Scanner;
  14. import java.util.Vector;
  15.  
  16. public class DatabaseGenerator {
  17.  
  18. public static void main(String[] args)throws Exception
  19. {
  20. String s = new String();
  21. StringBuffer sb = new StringBuffer();
  22. SimpleDataSource.init("database.properties");
  23. // be sure to not have line starting with "--" or "/*" or any other non aplhabetical character
  24.  
  25.  
  26. if (args.length != 0)
  27. {
  28. File file = new File(args[0]);
  29. System.out.println("ARGS: " + args[0]);
  30.  
  31. FileReader fr = new FileReader(file);
  32. BufferedReader br = new BufferedReader(fr);
  33. s = br.readLine();
  34. while(s != null)
  35. {
  36. sb.append(s);
  37. s = br.readLine();
  38. }
  39. br.close();
  40. }
  41.  
  42. // here is our splitter ! We use ";" as a delimiter for each request
  43. // then we are sure to have well formed statements
  44. String[] stmts = sb.toString().split(";");
  45.  
  46. try
  47. {
  48. Connection c = SimpleDataSource.getConnection();
  49. Statement st = c.createStatement();
  50. for(int i = 0; i < stmts.length; i++)
  51. {
  52. if (stmts[i].isEmpty()) continue;
  53. System.out.println("QQQQQ: " + stmts[i]);
  54. st.execute(stmts[i]);
  55. }
  56. } catch(Exception e) {
  57. System.out.println("*** Error : "+e.toString());
  58. System.out.println("*** ");
  59. System.out.println("*** Error : ");
  60. e.printStackTrace();
  61. System.out.println("################################################");
  62. }
  63.  
  64.  
  65. Scanner input = new Scanner(System.in);
  66. boolean done = false;
  67. while(!done)
  68. {
  69. System.out.println(" - 1 to add a student");
  70. System.out.println(" - 2 to show all students in the database");
  71. System.out.println(" - 3 to remove student");
  72. System.out.println(" - 4 to add a module");
  73. System.out.println(" - 5 to show all modules in the database");
  74. System.out.println(" - 6 to remove module");
  75. System.out.println(" - 7 enroll student to module");
  76. System.out.println(" - 8 Remove student from module");
  77. System.out.println(" - -1 to quit application");
  78. System.out.println("Choice: ");
  79.  
  80. int choice = input.nextInt();
  81.  
  82. switch (choice)
  83. {
  84. case 1:
  85. {
  86. SimpleDataSource.executeAddStudentQuery("S10349", "Mohammed Weed", "BSc Computer Science");
  87. System.out.println("Student Added");
  88. break;
  89. }
  90. case 2:
  91. {
  92. Vector<String> buffer = SimpleDataSource.executeGetStudentListQuery();
  93. for(int i = 0; i < buffer.size(); i++)
  94. {
  95. System.out.println( ". " + buffer.elementAt(i));
  96.  
  97. }
  98. break;
  99. }
  100.  
  101. case 3:
  102. {
  103. SimpleDataSource.executeRemoveStudentUpdate("S10349");
  104. System.out.println("Student Removed");
  105. break;
  106. }
  107.  
  108. case 4:
  109. {
  110. SimpleDataSource.executeAddModuleQuery("CS205", "Computer Laboratoty",10);
  111. System.out.println("Module Added");
  112. break;
  113. }
  114.  
  115. case 5:
  116. {
  117. Vector<String> buffer = SimpleDataSource.executeGetModuleListQuery();
  118. for(int i = 0; i < buffer.size(); i++)
  119. {
  120. System.out.println( ". " + buffer.elementAt(i));
  121.  
  122. }
  123. break;
  124. }
  125.  
  126. case 6:
  127. {
  128. SimpleDataSource.executeRemoveModuleUpdate("CS205");
  129. System.out.println("Module Removed");
  130. break;
  131. }
  132.  
  133. case 7:
  134. {
  135. SimpleDataSource.executeAddStudentModuleQuery("S10349", "CS204");
  136. System.out.println("Student Registered to Module");
  137. break;
  138. }
  139. case 8:
  140. {
  141. SimpleDataSource.executeRemoveStudentModuleUpdate("S10349");
  142. System.out.println("Student Removed From Module");
  143. break;
  144. }
  145.  
  146. case -1:
  147. {
  148. done = true;
  149. break;
  150. }
  151. default:
  152. break;
  153. }
  154.  
  155. }
  156. }
  157. }
  158.  
  159.  
  160.  
  161.  
  162. /*
  163.  
  164. public static void main(String[] args) throws Exception {}
  165.  
  166.  
  167. private static final String DRIVER_NAME = "com.mysql.jdbc.Driver";
  168.  
  169. static
  170. {
  171. try
  172. {
  173. Class.forName(DRIVER_NAME).newInstance();
  174. System.out.println("*** Driver loaded");
  175. }
  176. catch(Exception e)
  177. {
  178. System.out.println("*** Error : "+e.toString());
  179. System.out.println("*** ");
  180. System.out.println("*** Error : ");
  181. e.printStackTrace();
  182. }
  183.  
  184. }
  185.  
  186. private static final String url = "jdbc:mysql://localhost:3306/university";
  187. private static final String user = "root";
  188. private static final String password = "puss";
  189. private static String INSTRUCTIONS = new String();
  190.  
  191. public static Connection getConnection() throws SQLException
  192. {
  193. return DriverManager.getConnection(url, user, password);
  194. }
  195.  
  196. public static void resetDatabase() throws SQLException
  197. {
  198. String s = new String();
  199. StringBuffer sb = new StringBuffer();
  200.  
  201. try
  202. {
  203. FileReader fr = new FileReader(new File("database.sql"));
  204. // be sure to not have line starting with "--" or "/*" or any other non aplhabetical character
  205.  
  206. BufferedReader br = new BufferedReader(fr);
  207.  
  208. while((s = br.readLine()) != null)
  209. {
  210. sb.append(s);
  211. }
  212. br.close();
  213.  
  214. // here is our splitter ! We use ";" as a delimiter for each request
  215. // then we are sure to have well formed statements
  216. String[] inst = sb.toString().split(";");
  217.  
  218. Connection c = SimpleDataSource.getConnection();
  219. Statement st = c.createStatement();
  220.  
  221. for(int i = 0; i<inst.length; i++)
  222. {
  223. // we ensure that there is no spaces before or after the request string
  224. // in order to not execute empty statements
  225. if(!inst[i].trim().equals(""))
  226. {
  227. st.executeUpdate(inst[i]);
  228. System.out.println(">>"+inst[i]);
  229. }
  230. }
  231.  
  232. }
  233. catch(Exception e)
  234. {
  235. System.out.println("*** Error : "+e.toString());
  236. System.out.println("*** ");
  237. System.out.println("*** Error : ");
  238. e.printStackTrace();
  239. System.out.println("################################################");
  240. System.out.println(sb.toString());
  241. }
  242.  
  243. }
  244. }
  245. *
  246. */
Add Comment
Please, Sign In to add comment