Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package a_Zadania.a_Dzien_2.a_Zmiana_usuwanie_danych;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.Scanner;
- public class Main5 {
- private static final String GET_CINEMAS_QUERY =
- "SELECT * FROM cinemas";
- private static final String GET_EDIT_QUERY =
- "UPDATE cinemas SET name = ?, adress = ? WHERE id = ?";
- private static final String GET_DELETE_QUERY =
- "DELETE FROM cinemas where id = ?";
- public static void main(String[] args) throws SQLException {
- Scanner scanner = new Scanner(System.in);
- try(Connection connection = createConnection("cinemas_ex");
- PreparedStatement getCinemasStatement = connection.prepareStatement(GET_CINEMAS_QUERY);
- PreparedStatement updateEditStatement = connection.prepareStatement(GET_EDIT_QUERY);
- PreparedStatement updateDeleteStatement = connection.prepareStatement(GET_DELETE_QUERY);
- ResultSet resultSet = getCinemasStatement.executeQuery();){
- while (resultSet.next()){
- int id = resultSet.getInt("id");
- String name = resultSet.getString("name");
- String adress = resultSet.getString("adress");
- System.out.println(String.format("Ticket: id: %d name: %s adress:%s", id, name, adress));
- }
- System.out.println("Jeśli edytować wiersz kliknij e, jeśli usunąć wiersz kliknij u, wyjście x");
- String choice = scanner.next();
- while(true){
- if(choice.equals("x")){
- scanner.close();
- break;
- } else if(choice.equals("u")){
- System.out.println("Który wiersz chcesz usunąć? Podaj id");
- int choiceRow = scanner.nextInt();
- updateDeleteStatement.setInt(1, choiceRow);
- updateDeleteStatement.executeUpdate();
- scanner.close();
- break;
- } else if(choice.equals("e")){
- System.out.println("Który wiersz chcesz edytować? Podaj id");
- int idToChange = scanner.nextInt();
- System.out.println("Podaj nową nazwę a następnie nowy adres");
- String newName = scanner.next();
- String newAdress = scanner.next();
- updateEditStatement.setString(1, newName);
- updateEditStatement.setString(2, newAdress);
- updateEditStatement.setInt(3, idToChange);
- updateEditStatement.executeUpdate();
- scanner.close();
- break;
- }
- }
- }
- }
- private static Connection createConnection(String databaseName) throws SQLException {
- return DriverManager.getConnection("jdbc:mysql://localhost:3306/cinemas_ex",
- "root","coderslab");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement