Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package a_Zadania.a_Dzien_1.c_Pobieranie_danych;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.Scanner;
- import com.mysql.jdbc.Connection;
- public class Main5 {
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- showCinemas();
- System.out.println();
- System.out.println("Jeśli chcesz edytować wpisz E, jeśli chcesz usunąć wpisz U, jeśli chcesz usunąć wpisz X: ");
- String decision = scan.nextLine();
- if(decision.equalsIgnoreCase("e")) {
- changeRecord();
- } else if(decision.equalsIgnoreCase("u")) {
- deleteRecord();
- } else if (decision.equalsIgnoreCase("x")) {
- System.out.println("Do widzenia!!!");
- }
- }
- public static void showCinemas() {
- try (Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/cinemas_ex?useSSL=false", "root", "coderslab")){
- String str = "SELECT * FROM cinemas";
- PreparedStatement ps = conn.prepareStatement(str);
- ResultSet rs = ps.executeQuery();
- while (rs.next()) {
- String name = rs.getString("name");
- int id = rs.getInt("id");
- String adres = rs.getString("adress");
- System.out.println(id + " " + name + " " + adres);
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public static void changeRecord() {
- Scanner scan = new Scanner(System.in);
- System.out.println();
- System.out.print("Podaj id kina które chcesz zmienić: ");
- int id = scan.nextInt();
- System.out.println();
- System.out.print("Podaj nazwę kina: ");
- String name1 = scan.next();
- System.out.println();
- System.out.print("Podaj nowy adres kina: ");
- String adress1 = scan.next();
- System.out.println();
- try (Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/cinemas_ex?useSSL=false", "root", "coderslab")){
- String sql = "UPDATE cinemas SET name = ?, adress = ? WHERE id = ?";
- PreparedStatement ps = conn.prepareStatement(sql);
- ps.setString(1, name1);
- ps.setString(2, adress1);
- ps.setInt(3, id);
- ps.executeUpdate();
- }catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public static void deleteRecord() {
- Scanner scan = new Scanner(System.in);
- System.out.print("Podaj id kina które chcesz usunąć: ");
- int x = scan.nextInt();
- try (Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/cinemas_ex?useSSL=false", "root", "coderslab")){
- String sql = "DELETE FROM cinemas WHERE id = ?";
- PreparedStatement ps = conn.prepareStatement(sql);
- ps.setInt(1, x);
- ps.executeUpdate();
- }catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
Add Comment
Please, Sign In to add comment