Advertisement
Guest User

Untitled

a guest
May 2nd, 2016
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.78 KB | None | 0 0
  1. import java.util.*;
  2. import java.sql.*;
  3.  
  4. public class StudentApplication
  5. {
  6. private static Scanner scanner;
  7. private static Connection conn = null;
  8. private static Statement stmt = null;
  9. private static Throwable e;
  10.  
  11. public static boolean ConnectToDB() {
  12. try {
  13.  
  14. Class.forName("com.mysql.jdbc.Driver");
  15. String url = "jdbc:mysql://localhost:3306/University";
  16. conn = DriverManager.getConnection(url);
  17. System.out.println("Connection Successful!");
  18. stmt = conn.createStatement();
  19. return true;
  20.  
  21. }catch(Exception e){
  22. System.err.println(e.getMessage());
  23.  
  24. }
  25. return false;
  26. }
  27.  
  28. public static void main (String[] args) {
  29.  
  30. boolean dbConnection = false;
  31. try{
  32. dbConnection = ConnectToDB();
  33. }
  34. catch(Exception e)
  35. {
  36. System.out.println("Sorry could not connect to DB.");
  37. return;
  38. }
  39. if(!dbConnection){
  40. System.out.println("Exiting System as unable to connect to DB");
  41. return;
  42. }
  43. int choice;
  44. scanner = new Scanner (System.in);
  45.  
  46. choice = menu();
  47. while (choice != 7) {
  48.  
  49. switch (choice) {
  50. case 1: choice1(); break;
  51. case 2: choice2(); break;
  52. case 3: choice3(); break;
  53. case 4: choice4(); break;
  54. case 5: choice5(); break;
  55. case 6: choice6(); break;
  56. default: break;
  57. }
  58.  
  59. choice = menu();
  60. };
  61. }
  62.  
  63. public static int menu() {
  64.  
  65. int choice;
  66.  
  67. System.out.println ("\n\n");
  68. System.out.println ("1. Add a new student to the system");
  69. System.out.println ("2. Display information about a student with a given student ID");
  70. System.out.println ("3. Change a student's telephone number");
  71. System.out.println ("4. List all the students in the system");
  72. System.out.println ("5. Delete a student from the system with a given Student ID");
  73. System.out.println ("6. Register a Student in a Course");
  74. System.out.println ("7. Exit");
  75.  
  76. System.out.print ("\n\nPleasemake a selection: ");
  77.  
  78. choice = scanner.nextInt();
  79. return choice;
  80. }
  81.  
  82. //Add a new student to the Database
  83. public static void choice1() {
  84. String firstName, lastName;
  85. String phone;
  86.  
  87. System.out.println ("Enter the student's first name, last name, and telephone number: ");
  88.  
  89. firstName = scanner.next();
  90. lastName = scanner.next();
  91. phone = scanner.next();
  92.  
  93. // create and insert Student in Student Table
  94. try{
  95. String sql = "SELECT MAX(StudentID) as maxID FROM Student";
  96. ResultSet rs = stmt.executeQuery(sql);
  97. int maxID = 0;
  98. while(rs.next())
  99. {
  100. maxID=rs.getInt("maxID");
  101. System.out.println(maxID);
  102. }
  103. int nextID = maxID + 10;
  104. //NOTE THE CONSTRUCTION OF SQL STATEMENT
  105. sql = "INSERT INTO STUDENT VALUES(" + nextID +
  106. ",'" + firstName + "','"+lastName+ "','"+phone +"')";
  107. System.out.println("INSERT STATEMENT IS :\n " + sql);
  108. stmt.execute(sql);
  109. System.out.println("New Student created with ID : "+ nextID);
  110. }
  111. catch(Exception e)
  112. {
  113. e.printStackTrace();
  114. System.out.println("Sorry unable to create Student");
  115. return;
  116. }
  117. }
  118.  
  119. //Display information about a student with a given student ID
  120. public static void choice2() {
  121.  
  122. int studentID;
  123. StudentTwo student;
  124.  
  125. System.out.print("Please enter a student ID: ");
  126. studentID = scanner.nextInt();
  127.  
  128. student = getStudent(studentID);
  129.  
  130. if (student == null)
  131. System.out.println ("There is no student with this ID.");
  132. else
  133. System.out.println (student.toString());
  134. }
  135.  
  136. //Change a student's telephone number
  137. public static void choice3() {
  138.  
  139. int studentID;
  140. StudentTwo student;
  141.  
  142. System.out.print("Please enter a student ID: ");
  143. studentID = scanner.nextInt();
  144.  
  145. student = getStudent(studentID);
  146.  
  147. if (student == null)
  148. System.out.println ("There is no student with this ID.");
  149. else {
  150. String newPhone;
  151. System.out.print ("Please enter the new telephone number: ");
  152. newPhone = scanner.next();
  153. //student.setPhone(newPhone);
  154. String sql = "UPDATE Student SET Phone = '"+newPhone+"' WHERE StudentID = "+ studentID;
  155. try{
  156. stmt.executeUpdate(sql);
  157. }
  158. catch(Exception e){
  159. e.printStackTrace();
  160. System.out.println("Sorry could not update the phone number");
  161. return;
  162.  
  163. }
  164. System.out.println ("Student's telephone number has been updated.");
  165. }
  166.  
  167. }
  168.  
  169. //List all the students in the system
  170. public static void choice4() {
  171. StudentTwo st = new StudentTwo();
  172. try{
  173. String sql = "SELECT * FROM Student";
  174. ResultSet rs = stmt.executeQuery(sql);
  175. System.out.println("List of Students:");
  176. while(rs.next())
  177. {
  178. st.setID(rs.getInt("StudentID"));//get by column name
  179. st.setFirstName(rs.getString("FirstName"));
  180. st.setLastName(rs.getString(3));//get by column index
  181. st.setPhone(rs.getString(4));
  182. System.out.println(st.toString());
  183. }
  184. }
  185. catch(Exception e){
  186. e.printStackTrace();
  187. System.out.println("Sorry unable to fetch Students from DB");
  188. st = null;
  189. }
  190. }
  191.  
  192. //Delete a Student from the DB
  193. public static void choice5() {
  194. int studentID;
  195.  
  196. System.out.print("Please enter a student ID: ");
  197. studentID = scanner.nextInt();
  198. try{
  199. String sql = "DELETE FROM Student WHERE StudentID = "+ studentID;
  200. int res = stmt.executeUpdate(sql);
  201. System.out.println(res + " Student deleted");
  202. }
  203. catch(Exception e){
  204. e.printStackTrace();
  205. System.out.println("Sorry Unable to delete Student");
  206. }
  207. }
  208.  
  209. //Register a Student in a Course
  210. public static void choice6() {
  211. int studentID;
  212. int courseID;
  213. System.out.print("Please enter a student ID: \n");
  214. studentID = scanner.nextInt();
  215. System.out.print("Please select a Course(Enter 1 or 2 or 3) : ");
  216. System.out.print("1. OOP with Java\n");
  217. System.out.print("2. Intro to C#\n");
  218. System.out.print("3. Introduction to JDBC\n");
  219. courseID = scanner.nextInt();
  220. if(courseID != 1 &&courseID != 2 &&courseID != 3)
  221. {
  222. System.out.println("Please enter correct choice");
  223. return;
  224. }
  225. try{
  226. String sql = "INSERT INTO Registration(StudentID, CourseID) VALUES("+ studentID+","+courseID+")";
  227. boolean res = stmt.execute(sql);
  228. System.out.println("Student Registered \n");
  229. }
  230. catch(Exception e){
  231. e.printStackTrace();
  232. System.out.println("Sorry Unable to register Student");
  233. }
  234. }
  235. public static StudentTwo getStudent(int studentID) {
  236. StudentTwo s = new StudentTwo();
  237. try{
  238.  
  239. String sql = "SELECT * FROM Student WHERE StudentID = "+ studentID;
  240. ResultSet rs = stmt.executeQuery(sql);
  241. while(rs.next())
  242. {
  243. s.setID(rs.getInt("StudentID"));//get by column name
  244. s.setFirstName(rs.getString("FirstName"));
  245. s.setLastName(rs.getString(3));//get by column index
  246. s.setPhone(rs.getString(4));
  247. }
  248. }
  249. catch(Exception e){
  250. e.printStackTrace();
  251. System.out.println("Sorry unable to fetch Student from DB");
  252. s = null;
  253. }
  254. return s;
  255. }
  256.  
  257. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement