Guest User

Untitled

a guest
Apr 19th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.90 KB | None | 0 0
  1. import java.io.Console;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7. import java.util.Scanner;
  8.  
  9. public class Faculty {
  10.  
  11. private static final String oracleServer = "dbs2.cs.umb.edu";
  12. private static final String oracleServerSid = "dbs2";
  13. private static Connection conn = null;
  14.  
  15. public static void main(String args[]) {
  16.  
  17. conn = getConnection();
  18.  
  19. System.out.print("Please enter faculty ID (-1 to register): ");
  20. Scanner input = new Scanner(System.in);
  21. int fid = Integer.parseInt(input.nextLine());
  22.  
  23. if(fid == -1) {
  24.  
  25. fid = registerFaculty();
  26.  
  27. } else {
  28.  
  29. try {
  30.  
  31. // check that ID exists
  32. Statement stmt = conn.createStatement();
  33. ResultSet fidExists = stmt.executeQuery("SELECT COUNT(*) FROM Faculty WHERE fid = " + fid);
  34. fidExists.next();
  35.  
  36. if(fidExists.getInt(1) == 0) {
  37. System.out.println("Invalid faculty ID specified! Please re-enter or register.");
  38. return;
  39. }
  40.  
  41. } catch (SQLException e) {
  42. System.out.println(e.getMessage());
  43. }
  44.  
  45. }
  46.  
  47. printMenu();
  48. getCommands(fid);
  49.  
  50. try {
  51.  
  52. if (conn != null) {
  53. conn.close();
  54. }
  55.  
  56. } catch (SQLException e) {
  57. System.out.println(e.getMessage());
  58. System.exit(1);
  59. }
  60.  
  61. }
  62.  
  63. public static int registerFaculty() {
  64.  
  65. Scanner input = new Scanner(System.in);
  66.  
  67. System.out.println("Entering faculty registration process.");
  68. System.out.println("--------------------------------------");
  69.  
  70. System.out.print("Enter new faculty ID: ");
  71. int fid = Integer.parseInt(input.nextLine());
  72.  
  73. System.out.print("Enter new faculty name: ");
  74. String fname = input.nextLine();
  75.  
  76. try {
  77. Statement stmt = conn.createStatement();
  78. stmt.executeUpdate("INSERT INTO Faculty VALUES (" + fid + ", '" + fname + "')");
  79. stmt.close();
  80. } catch(SQLException e) {
  81. System.out.println(e.getMessage());
  82. }
  83.  
  84. return fid;
  85.  
  86. }
  87.  
  88. public static void printMenu() {
  89.  
  90. System.out.println("-----------------------");
  91. System.out.println("L : list all courses.");
  92. System.out.println("C : create my course.");
  93. System.out.println("D : delete my course.");
  94. System.out.println("S : search all courses.");
  95. System.out.println("M : list my courses.");
  96. System.out.println("X : exit application.");
  97. System.out.println("-----------------------");
  98.  
  99. }
  100.  
  101. public static void getCommands(int fid) {
  102.  
  103. String command = "";
  104. Scanner input = new Scanner(System.in);
  105.  
  106. while(!command.equalsIgnoreCase("X")) {
  107.  
  108. System.out.print("Enter a command: ");
  109. command = input.nextLine();
  110.  
  111. if(command.equalsIgnoreCase("L")) {
  112. performCommandL(fid);
  113. } else if(command.equalsIgnoreCase("C")) {
  114. performCommandC(fid);
  115. } else if (command.equalsIgnoreCase("D")) {
  116. performCommandD(fid);
  117. } else if(command.equalsIgnoreCase("S")) {
  118. performCommandS(fid);
  119. } else if(command.equalsIgnoreCase("M")) {
  120. performCommandM(fid);
  121. } else if(command.equalsIgnoreCase("X")) {
  122. return;
  123. }
  124.  
  125. }
  126.  
  127. }
  128.  
  129. private static void performCommandM(int fid) {
  130.  
  131. try {
  132.  
  133. Statement stmt = conn.createStatement();
  134. ResultSet allCourses = stmt.executeQuery("SELECT cid, cname, credits FROM Courses WHERE fid = " + fid);
  135.  
  136. System.out.println("-----------------------");
  137.  
  138. while(allCourses.next()) {
  139.  
  140. System.out.println("Course ID: " + allCourses.getInt(1) + "\t\tCourse Name: " + allCourses.getString(2) + "\t\tCredits: " + allCourses.getInt(3));
  141.  
  142. }
  143.  
  144. System.out.println("-----------------------");
  145.  
  146. allCourses.close();
  147. stmt.close();
  148.  
  149. } catch (SQLException e) {
  150. System.out.println(e.getMessage());
  151. }
  152.  
  153. }
  154.  
  155. private static void performCommandS(int fid) {
  156.  
  157. Scanner scan = new Scanner(System.in);
  158. String searchName = null;
  159.  
  160. try {
  161.  
  162. System.out.print("Please enter the course name to search: ");
  163. searchName = scan.nextLine().toUpperCase();
  164.  
  165. Statement stmt = conn.createStatement();
  166. ResultSet allCourses = stmt.executeQuery("SELECT cid, cname, credits, fid FROM Courses WHERE UPPER(cname) LIKE '%" + searchName + "%'");
  167.  
  168. System.out.println("-----------------------");
  169.  
  170. while(allCourses.next()) {
  171.  
  172. System.out.println("Course ID: " + allCourses.getInt(1) + "\t\tCourse Name: " + allCourses.getString(2) + "\t\tCredits: " + allCourses.getInt(3) + "\t\tFaculty ID: " + allCourses.getInt(4));
  173.  
  174. }
  175.  
  176. System.out.println("-----------------------");
  177.  
  178. allCourses.close();
  179. stmt.close();
  180.  
  181. } catch (SQLException e) {
  182. System.out.println(e.getMessage());
  183. }
  184.  
  185. }
  186.  
  187. private static void performCommandD(int fid) {
  188.  
  189. Scanner scan = new Scanner(System.in);
  190. int cid = -1;
  191.  
  192. try {
  193.  
  194. System.out.println("-----------------------");
  195.  
  196. System.out.print("Please enter the course ID to delete: ");
  197. cid = Integer.parseInt(scan.nextLine());
  198.  
  199. Statement stmt = conn.createStatement();
  200. ResultSet cfid = stmt.executeQuery("SELECT fid FROM courses WHERE cid = " + cid);
  201. // cfid.first(); seems like Oracle driver doesn't support this
  202. cfid.next();
  203.  
  204. if(!(cfid.getInt(1) == fid)) {
  205. System.out.println("You do not have the authority to delete this course!");
  206. System.out.println("-----------------------");
  207. cfid.close();
  208. stmt.close();
  209. return;
  210. }
  211.  
  212. cfid.close();
  213. stmt.close();
  214.  
  215. stmt = conn.createStatement();
  216. ResultSet enrl = stmt.executeQuery("SELECT COUNT(*) FROM Enrolled WHERE cid = " + cid );
  217. // enrl.first(); seems like Oracle driver doesn't support this
  218. enrl.next();
  219.  
  220. if(enrl.getInt(1) > 4) {
  221. System.out.println("You cannot delete this course if there 5 or more students enrolled!");
  222. System.out.println("-----------------------");
  223. enrl.close();
  224. stmt.close();
  225. return;
  226. }
  227.  
  228. enrl.close();
  229. stmt.close();
  230.  
  231. stmt = conn.createStatement();
  232. stmt.executeUpdate("DELETE FROM Courses WHERE cid = " + cid);
  233.  
  234. System.out.println("Course deleted!");
  235. System.out.println("-----------------------");
  236.  
  237. } catch (SQLException e) {
  238. System.out.println(e.getMessage());
  239. }
  240.  
  241. }
  242.  
  243. private static void performCommandC(int fid) {
  244.  
  245. Scanner scan = new Scanner(System.in);
  246. int cid = -1;
  247. String cname = null;
  248. int credits = -1;
  249.  
  250. try {
  251.  
  252. System.out.println("-----------------------");
  253.  
  254. System.out.print("Please enter the new course ID: ");
  255. cid = Integer.parseInt(scan.nextLine());
  256. System.out.print("Please enter the new course name: ");
  257. cname = scan.nextLine();
  258. System.out.print("Please enter the new course number of credits: ");
  259. credits = Integer.parseInt(scan.nextLine());
  260.  
  261. Statement stmt = conn.createStatement();
  262. stmt.executeUpdate("INSERT INTO Courses VALUES (" + cid + ", '" + cname + "', " + credits + ", " + fid + ")");
  263. stmt.close();
  264.  
  265. System.out.println("New course submitted!");
  266. System.out.println("-----------------------");
  267.  
  268. } catch (SQLException e) {
  269. System.out.println(e.getMessage());
  270. }
  271.  
  272. }
  273.  
  274. private static void performCommandL(int fid) {
  275.  
  276. try {
  277.  
  278. Statement stmt = conn.createStatement();
  279. ResultSet allCourses = stmt.executeQuery("SELECT cid, cname, credits, fid FROM Courses");
  280.  
  281. System.out.println("-----------------------");
  282.  
  283. while(allCourses.next()) {
  284.  
  285. System.out.println("Course ID: " + allCourses.getInt(1) + "\t\tCourse Name: " + allCourses.getString(2) + "\t\tCredits: " + allCourses.getInt(3) + "\t\tFaculty ID: " + allCourses.getInt(4));
  286.  
  287. }
  288.  
  289. System.out.println("-----------------------");
  290.  
  291. allCourses.close();
  292. stmt.close();
  293.  
  294. } catch (SQLException e) {
  295. System.out.println(e.getMessage());
  296. }
  297.  
  298. }
  299.  
  300. public static Connection getConnection() {
  301.  
  302. // Get username and password
  303. Scanner input = new Scanner(System.in);
  304. System.out.print("Username: ");
  305. String username = input.nextLine();
  306. System.out.print("Password: ");
  307. //the following is used to mask the password
  308. Console console = System.console();
  309. String password = new String(console.readPassword());
  310. String connString = "jdbc:oracle:thin:@" + oracleServer + ":1521:" + oracleServerSid;
  311.  
  312. System.out.println("Please wait for the connection to establish...");
  313.  
  314. // load the driver
  315. String jdbcDriver = "oracle.jdbc.OracleDriver";
  316.  
  317. try {
  318. Class.forName(jdbcDriver);
  319. } catch (Exception e) {
  320. System.out.println(e.getMessage());
  321. }
  322.  
  323. Connection conn;
  324.  
  325. try { // Connect to the database
  326. conn = DriverManager.getConnection(connString, username, password);
  327. } catch(SQLException e) {
  328. System.out.println("Connection Error!");
  329. System.out.println(e.getMessage());
  330. return null;
  331. }
  332.  
  333. return conn;
  334.  
  335. }
  336.  
  337. }
Add Comment
Please, Sign In to add comment