Advertisement
Guest User

Untitled

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