Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. package withOutAbstract.model;
  2.  
  3.  
  4. import java.sql.*;
  5. import java.util.Scanner;
  6.  
  7. public class Menu {
  8. static Scanner scanner = new Scanner(System.in);
  9.  
  10. public static void main(String[] args) throws ClassNotFoundException, SQLException {
  11. Class.forName("com.mysql.cj.jdbc.Driver");
  12. Connection connection =
  13. DriverManager.getConnection(
  14. "jdbc:mysql://127.0.0.1:3306/studentbd?serverTimezone=CET",
  15. "root",
  16. "Dbernacki1992");
  17. menu(connection);
  18. }
  19.  
  20. private static void switchMenu(Connection connection) throws SQLException {
  21. System.out.println("Podaj opcję, którą chcesz wykonać: ");
  22. String text = scanner.next();
  23. switch (text) {
  24. case "1":
  25. showRecords(connection);
  26. break;
  27. case "2":
  28. addRecords(connection);
  29. break;
  30. case "3":
  31. deleteRecord(connection);
  32. break;
  33. case "4":
  34. updateRecord(connection);
  35. break;
  36. case "5":
  37. finishWork(connection);
  38. break;
  39. }
  40.  
  41. }
  42.  
  43. private static void finishWork(Connection connection) {
  44. System.exit(0);
  45. }
  46.  
  47. private static void updateRecord(Connection connection) throws SQLException {
  48. System.out.println("Zmodyfikuj dane uzytkownika [ podaj ID ]: ");
  49. String id = scanner.next();
  50. System.out.println("Podaj imie: ");
  51. String name1 = scanner.next();
  52. System.out.println("Podaj nazwisko:");
  53. String lastname1 = scanner.next();
  54.  
  55.  
  56. Statement st = connection.createStatement();
  57. st.executeUpdate("UPDATE student SET name = '" + name1 + "', lastname = '" + lastname1 + "' where id = '" + id + "';");
  58.  
  59. System.out.println();
  60. menu(connection);
  61. }
  62.  
  63. private static void deleteRecord(Connection connection) throws SQLException {
  64. System.out.println("Usuń uzytkownika [ podaj ID ]: ");
  65. String id = scanner.next();
  66.  
  67. Statement st = connection.createStatement();
  68. st.executeUpdate("delete from student where id = '" + id + "';");
  69.  
  70. System.out.println();
  71. menu(connection);
  72. }
  73.  
  74. private static void addRecords(Connection connection) throws SQLException {
  75. System.out.println("Podaj imie: ");
  76. String name1 = scanner.next();
  77.  
  78. System.out.println("Podaj nazwisko:");
  79. String lastname1 = scanner.next();
  80.  
  81. Statement st = connection.createStatement();
  82. st.executeUpdate("insert into student (name,lastname) values ('" + name1 + "','" + lastname1 + "');");
  83.  
  84. System.out.println();
  85. menu(connection);
  86. }
  87.  
  88. private static void showRecords(Connection connection) throws SQLException {
  89. System.out.println();
  90. PreparedStatement preparedStatement1 =
  91. connection.prepareStatement("SELECT id,name,lastname FROM studentbd.student;");
  92. ResultSet resultSet1 = preparedStatement1.executeQuery();
  93. while (resultSet1.next()) {
  94. String nazwa1 = resultSet1.getString("id");
  95. String nazwa2 = resultSet1.getString("name");
  96. String nazwa3 = resultSet1.getString("lastname");
  97. System.out.println(nazwa1 + " " + nazwa2 + " " + nazwa3);
  98. }
  99. System.out.println();
  100. menu(connection);
  101. }
  102.  
  103. private static void menu(Connection connection) throws SQLException {
  104. System.out.println("1. Pokaż rekordy");
  105. System.out.println("2. Dodaj rekord");
  106. System.out.println("3. Usuń rekord [id= ]");
  107. System.out.println("4. Nadpisz rekord [id= ]");
  108. System.out.println("5. Koniec");
  109. switchMenu(connection);
  110. }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement