Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package a_Zadania.a_Dzien_1.b_Dodawanie_danych;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.Scanner;
- public class Main4 {
- private static Connection createConnection() throws SQLException {
- return DriverManager.getConnection("jdbc:mysql://localhost:3306/cinemas_ex", "root", "coderslab");
- }
- public static void main(String[] args) throws SQLException {
- Scanner scan = new Scanner(System.in);
- System.out.println("Proszę o podanien numeru ID");
- while (true) {
- try {
- String temp = scan.nextLine();
- int id = Integer.parseInt(temp);
- System.out.println("Proszę podać nazwę");
- String name = scan.nextLine();
- System.out.println("Proszę podać adres");
- String adress = scan.nextLine();
- addCinema(id, name, adress);
- break;
- } catch (Exception e) {
- System.out.println("Podano nieprawidłowy numer, podaj nr jeszcze raz.");
- }
- }
- }
- static void addCinema(int id, String name, String address) {
- String query1 = "INSERT INTO cinemas(id, name, adress) VALUES(?, ?, ?);";
- try (Connection connection = createConnection();
- PreparedStatement statement = connection.prepareStatement(query1);) {
- statement.setInt(1, id);
- statement.setString(2, name);
- statement.setString(3, address);
- System.out.println(statement.executeUpdate());
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement