Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.Scanner;
- public class Main5 {
- private static final String GET_CINEMAS_QUERY="SELECT*FROM cinemas";
- private static final String UPDATE_CINEMA_QUERY="UPDATE cinemas SET name=?, address=? WHERE id=?";
- private static final String DELETE_CINEMA_QUERY="DELETE FROM cinemas WHERE id=?";
- private static final String MOVIE_CINEMAS_TEMPLATE = "Cinema: id:%d name:%s, address:%s";
- public static void main(String[] args) {
- String url = "jdbc:mysql://localhost:3306/cinemas_ex";
- Scanner scan=new Scanner(System.in);
- try(Connection connection=getConnection(url)){
- printCinemas(connection);
- System.out.println("Edycja: wpisz e, usunięcie: wpisz u, wyjście z programu: wpisz x");
- String input=scan.next();
- while(!input.equals("x")) {
- if(input.equals("e")) {
- System.out.println("podaj numer wiersza:");
- int toEdit=scan.nextInt();
- System.out.println("podaj nową nazwę:");
- scan.nextLine();
- String name=scan.nextLine();
- System.out.println("podaj nowy adres");
- String address=scan.nextLine();
- System.out.println( toEdit+ " "+ name+" "+address);
- updateCinema(connection, toEdit,name,address);
- printCinemas(connection);
- }else if(input.equals("u")) {
- System.out.println("czy na pewno chcesz usunąć wiersz? tak wpisz T, nie wpisz N");
- if(scan.next().equals("T")){
- System.out.println("podaj numer wiersza:");
- int toDelete=scan.nextInt();
- deleteMovie(connection,toDelete);
- printCinemas(connection);
- }else {
- System.out.println("Edycja: wpisz e, usunięcie: wpisz u, wyjście z programu: wpisz x");
- scan.next();
- }
- }else {
- System.out.println("Edycja: wpisz e, usunięcie: wpisz u, wyjście z programu: wpisz x");
- }
- input=scan.next();
- }
- if(input.equals("x")) {
- System.out.println("exit");
- }
- }catch (SQLException e) {
- e.printStackTrace();
- }
- }
- private static Connection getConnection(String url) throws SQLException {
- return DriverManager.getConnection(url, "root", "coderslab");
- }
- private static void updateCinema(Connection connection, int id, String name, String address) throws SQLException {
- try (PreparedStatement preparedStatement = connection.prepareStatement(UPDATE_CINEMA_QUERY);) {
- preparedStatement.setString(1,name);
- preparedStatement.setString(2,address);
- preparedStatement.setInt(3,id);
- int updatedRecords = preparedStatement.executeUpdate();
- if (updatedRecords == 1) {
- System.out.println("Record with id " + id + " was updated");
- } else {
- System.out.println("Record was not updated");
- }
- }
- }
- private static void deleteMovie(Connection connection, int id) throws SQLException {
- try (PreparedStatement preparedStatement = connection.prepareStatement(DELETE_CINEMA_QUERY);) {
- preparedStatement.setInt(1, id);
- int deletedRecords = preparedStatement.executeUpdate();
- if (deletedRecords == 1) {
- System.out.println("Record with id " + id + " was deleted");
- } else {
- System.out.println("Record was not deleted");
- }
- }
- }
- public static void printCinemas(Connection connection) throws SQLException {
- try(PreparedStatement stat=connection.prepareStatement(GET_CINEMAS_QUERY)){
- try (ResultSet resultSet = stat.executeQuery()) {
- while (resultSet.next()) {
- int id = resultSet.getInt("id");
- String name = resultSet.getString("name");
- String address = resultSet.getString("address");
- System.out.println(String.format(MOVIE_CINEMAS_TEMPLATE, id, name, address));
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement