Advertisement
Guest User

Untitled

a guest
Sep 30th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.58 KB | None | 0 0
  1. package learn.studentsDb;
  2.  
  3. import com.sun.org.apache.xpath.internal.SourceTree;
  4.  
  5. import java.lang.invoke.SwitchPoint;
  6. import java.sql.*;
  7. import java.util.Date;
  8. import java.util.Scanner;
  9.  
  10. public class Main {
  11. static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  12. static final String DB_URL = "jdbc:mysql://localhost:3306/students";
  13. static final String USER = "root";
  14. static final String PASS = "";
  15.  
  16. public static void main(String[] args) {
  17. Scanner in = new Scanner(System.in);
  18. Connection conn = null;
  19. Statement stmt = null;
  20. String sql;
  21.  
  22. //showing information for input
  23. info();
  24. int response = in.nextInt();
  25.  
  26. try {
  27.  
  28. Class.forName(JDBC_DRIVER);
  29. System.out.println("Connecting to database...");
  30.  
  31. conn = DriverManager.getConnection(DB_URL, USER, PASS);
  32. stmt = conn.createStatement();
  33.  
  34. switch (response){
  35. case 1: // list all students info
  36.  
  37. sql = "SELECT id, name, lesson, grade, time FROM class";
  38. ResultSet rs = stmt.executeQuery(sql);
  39.  
  40. while (rs.next()){
  41. //getting values
  42. int id = rs.getInt("id");
  43. String name = rs.getString("name");
  44. String lesson = rs.getString("lesson");
  45. int grade = rs.getInt("grade");
  46. Date time = rs.getDate("time");
  47.  
  48.  
  49. //Displaying
  50. System.out.print("student ID: " + id +
  51. " Name: " + name +
  52. " Lesson: " + lesson +
  53. " Grade: " + grade +
  54. " Date: " + time
  55. );
  56. System.out.println();
  57. }
  58. //disconnect from database
  59. disconncetDb(conn, stmt);
  60. break;
  61.  
  62. case 2: // Insert new info
  63. System.out.println("Please Enter student's name: ");
  64. String newName = in.next();
  65. System.out.println("Lesson's name: ");
  66. String newLesson = in.next();
  67. System.out.println("Student's grade: ");
  68. int newGrade = in.nextInt();
  69. sql = "INSERT INTO class(name, lesson, grade) VALUES('"+ newName +"','"+ newLesson+"',"+ newGrade +")";
  70. stmt.execute(sql);
  71. System.out.println("new information added successfully!");
  72. disconncetDb(conn,stmt);
  73. break;
  74.  
  75. case 3: // Delete info
  76. System.out.println("Please enter the Student's ID which you want to DELETE: ");
  77. int studentId = in.nextInt();
  78. sql = "DELETE FROM class WHERE id="+ studentId;
  79. stmt.execute(sql);
  80. disconncetDb(conn,stmt);
  81. break;
  82.  
  83. case 4: // update info
  84. System.out.println("What do you want to update? (name, lesson or grade") ;
  85. String answer = in.next();
  86. switch (answer){
  87. case "name":
  88. System.out.println("Please enter Student's ID which you want to update: ");
  89. int updateID = in.nextInt();
  90.  
  91. System.out.println("Enter the name: ");
  92. String updateName = in.next();
  93.  
  94. sql = "UPDATE class SET name='"+ updateName +"' WHERE id="+ updateID;
  95. stmt.execute(sql);
  96. System.out.println("Student's name updated by ID of " + updateID);
  97. break;
  98. case "lesson":
  99. System.out.println("Please enter Student's ID which you want to update: ");
  100. updateID = in.nextInt();
  101.  
  102. System.out.println("Enter the name: ");
  103. String updateLesson = in.next();
  104.  
  105. sql = "UPDATE class SET lesson='"+ updateLesson +"' WHERE id="+ updateID;
  106. stmt.execute(sql);
  107. System.out.println("Student's lesson updated by ID of " + updateID);
  108. break;
  109. case "grade":
  110. System.out.println("Please enter Student's ID which you want to update: ");
  111. updateID = in.nextInt();
  112.  
  113. System.out.println("Enter the name: ");
  114. int updateGrade = in.nextInt();
  115.  
  116. sql = "UPDATE class SET grade="+ updateGrade +" WHERE id="+ updateID;
  117. stmt.execute(sql);
  118. System.out.println("Student's grade updated by ID of " + updateID);
  119. break;
  120. default:
  121. System.out.println("Incorrect input, you can choose only between name, lesson or grade.");
  122. break;
  123. }
  124. disconncetDb(conn, stmt);
  125. break;
  126. default: // exit mode
  127. System.out.println("OK");
  128. break;
  129. }
  130.  
  131. } catch (ClassNotFoundException e) {
  132. e.printStackTrace();
  133. } catch (SQLException e) {
  134. e.printStackTrace();
  135. }
  136.  
  137. }
  138.  
  139. public static void info(){
  140. System.out.println(
  141. "Enter:" +
  142. "\n 1: To list all students info" +
  143. "\n 2: To add new student info" +
  144. "\n 3: To delete student info" +
  145. "\n 4: To update student info" +
  146. "\n 5: EXIT"
  147. );
  148. System.out.println("Your input is: ");
  149. }
  150.  
  151. public static void disconncetDb(Connection conn, Statement stmt){
  152. try {
  153. stmt.close();
  154. } catch (SQLException e) {
  155. e.printStackTrace();
  156. }
  157.  
  158. try {
  159. conn.close();
  160. } catch (SQLException e) {
  161. e.printStackTrace();
  162. }
  163.  
  164. System.out.println("Disconnected from database.");
  165. }
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement