Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package withOutAbstract.model;
- import java.sql.*;
- import java.util.Scanner;
- public class Menu {
- static Scanner scanner = new Scanner(System.in);
- public static void main(String[] args) throws ClassNotFoundException, SQLException {
- Class.forName("com.mysql.cj.jdbc.Driver");
- Connection connection =
- DriverManager.getConnection(
- "jdbc:mysql://127.0.0.1:3306/studentbd?serverTimezone=CET",
- "root",
- "Dbernacki1992");
- menu(connection);
- }
- private static void switchMenu(Connection connection) throws SQLException {
- System.out.println("Podaj opcję, którą chcesz wykonać: ");
- String text = scanner.next();
- switch (text) {
- case "1":
- showRecords(connection);
- break;
- case "2":
- addRecords(connection);
- break;
- case "3":
- deleteRecord(connection);
- break;
- case "4":
- updateRecord(connection);
- break;
- case "5":
- finishWork(connection);
- break;
- }
- }
- private static void finishWork(Connection connection) {
- System.exit(0);
- }
- private static void updateRecord(Connection connection) throws SQLException {
- System.out.println("Zmodyfikuj dane uzytkownika [ podaj ID ]: ");
- String id = scanner.next();
- System.out.println("Podaj imie: ");
- String name1 = scanner.next();
- System.out.println("Podaj nazwisko:");
- String lastname1 = scanner.next();
- Statement st = connection.createStatement();
- st.executeUpdate("UPDATE student SET name = '" + name1 + "', lastname = '" + lastname1 + "' where id = '" + id + "';");
- System.out.println();
- menu(connection);
- }
- private static void deleteRecord(Connection connection) throws SQLException {
- System.out.println("Usuń uzytkownika [ podaj ID ]: ");
- String id = scanner.next();
- Statement st = connection.createStatement();
- st.executeUpdate("delete from student where id = '" + id + "';");
- System.out.println();
- menu(connection);
- }
- private static void addRecords(Connection connection) throws SQLException {
- System.out.println("Podaj imie: ");
- String name1 = scanner.next();
- System.out.println("Podaj nazwisko:");
- String lastname1 = scanner.next();
- Statement st = connection.createStatement();
- st.executeUpdate("insert into student (name,lastname) values ('" + name1 + "','" + lastname1 + "');");
- System.out.println();
- menu(connection);
- }
- private static void showRecords(Connection connection) throws SQLException {
- System.out.println();
- PreparedStatement preparedStatement1 =
- connection.prepareStatement("SELECT id,name,lastname FROM studentbd.student;");
- ResultSet resultSet1 = preparedStatement1.executeQuery();
- while (resultSet1.next()) {
- String nazwa1 = resultSet1.getString("id");
- String nazwa2 = resultSet1.getString("name");
- String nazwa3 = resultSet1.getString("lastname");
- System.out.println(nazwa1 + " " + nazwa2 + " " + nazwa3);
- }
- System.out.println();
- menu(connection);
- }
- private static void menu(Connection connection) throws SQLException {
- System.out.println("1. Pokaż rekordy");
- System.out.println("2. Dodaj rekord");
- System.out.println("3. Usuń rekord [id= ]");
- System.out.println("4. Nadpisz rekord [id= ]");
- System.out.println("5. Koniec");
- switchMenu(connection);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement