fork download
  1. import java.sql.*;
  2. import java.util.Scanner;
  3. public class Main{
  4. public static void main(String[]args){
  5. final String DRIVER = "com.mysql.jdbc.Driver";
  6. final String DB_URL = "jdbc:mysql://localhost/school?serverTimezone=UTC";
  7. final String DB_USER = "root";
  8. final String DB_PASS = "password";
  9.  
  10. Connection conn = null;
  11. PreparedStatement psInsert = null, psUpdate = null, psDelete = null, psSelect = null;
  12. Scanner sc = new Scanner(System.in);
  13.  
  14. try{
  15. Class.forName(DRIVER);
  16. }
  17. System.out.println("Unable to load driver");
  18. e.printStackTrace();
  19. return;
  20. }
  21.  
  22. try{
  23. conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS);
  24. psInsert = conn.prepareStatement("INSERT INTO students (name, year_level, program) VALUES(?, ?, ?)");
  25. psUpdate = conn.prepareStatement("UPDATE students SET name = ?, year_level = ?, program = ? WHERE id = ?");
  26. psDelete = conn.prepareStatement("DELETE FROM students WHERE id = ?");
  27. psSelect = conn.prepareStatement("SELECT * FROM students");
  28. }
  29. catch(SQLException e){
  30. System.out.println("Unable to connect to database");
  31. e.printStackTrace();
  32. return;
  33. }
  34.  
  35. int choice = 0;
  36. do{
  37. System.out.println("Student Data Manager");
  38. System.out.println("1. Insert");
  39. System.out.println("2. Update");
  40. System.out.println("3. Delete");
  41. System.out.println("4. View");
  42. System.out.println("5. Exit");
  43. System.out.print("Enter choice: ");
  44. choice = sc.nextInt();
  45. if(choice == 1){
  46. System.out.print("Enter name: ");
  47. String name = sc.next();
  48. System.out.print("Enter year level: ");
  49. int yearLevel = sc.nextInt();
  50. System.out.print("Enter program: ");
  51. String program = sc.next();
  52. try{
  53. psInsert.setString(1, name);
  54. psInsert.setInt(2, yearLevel);
  55. psInsert.setString(3, program);
  56. psInsert.executeUpdate();
  57. }
  58. catch(SQLException e){
  59. System.out.println("Unable to execute SQL statement");
  60. e.printStackTrace();
  61. }
  62. }
  63. else if(choice == 2){
  64. System.out.print("Enter id: ");
  65. int id = sc.nextInt();
  66. System.out.print("Enter name: ");
  67. String name = sc.next();
  68. System.out.print("Enter year level: ");
  69. int yearLevel = sc.nextInt();
  70. System.out.print("Enter program: ");
  71. String program = sc.next();
  72. try{
  73. psUpdate.setString(1, name);
  74. psUpdate.setInt(2, yearLevel);
  75. psUpdate.setString(3, program);
  76. psUpdate.setInt(4, id);
  77. psUpdate.executeUpdate();
  78. }
  79. catch(SQLException e){
  80. System.out.println("Unable to execute SQL statement");
  81. e.printStackTrace();
  82. }
  83. }
  84. else if(choice == 3){
  85. System.out.print("Enter id: ");
  86. int id = sc.nextInt();
  87. try{
  88. psDelete.setInt(1, id);
  89. psDelete.executeUpdate();
  90. }
  91. catch(SQLException e){
  92. System.out.println("Unable to execute SQL statement");
  93. e.printStackTrace();
  94. }
  95. }
  96. else if(choice == 4){
  97. System.out.println("Student List");
  98. try{
  99. ResultSet rs = psSelect.executeQuery();
  100. while(rs.next()){
  101. int id = rs.getInt("id");
  102. String name = rs.getString("name");
  103. int yearLevel = rs.getInt("year_level");
  104. String program = rs.getString("program");
  105. System.out.println("Student id = " + id + ", name = " + name + ", year level = " + yearLevel + ", program = " + program);
  106. }
  107. }
  108. catch(SQLException e){
  109. System.out.println("Unable to execute SQL statement");
  110. e.printStackTrace();
  111. }
  112. }
  113. else if(choice == 5){
  114. System.out.println("Program Terminated!");
  115. }
  116. else{
  117. System.out.println("Invalid Choice!");
  118. }
  119. }while(choice != 5);
  120.  
  121. try{
  122. psInsert.close();
  123. psUpdate.close();
  124. psDelete.close();
  125. psSelect.close();
  126. conn.close();
  127. }catch(SQLException e){
  128. System.out.println("Unable to close connections");
  129. e.printStackTrace();
  130. }
  131. }
Success #stdin #stdout #stderr 0.16s 56844KB
stdin
Standard input is empty
stdout
Unable to load driver
stderr
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
	at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
	at java.base/java.lang.Class.forName0(Native Method)
	at java.base/java.lang.Class.forName(Class.java:332)
	at Main.main(Main.java:15)