Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Zbior;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.Statement;
- public class Kino {
- static String baza = "cinema";
- static String pole = "RODZAJ";
- static String wartosc = "Acer";
- public static void main(String[] args) {
- //Kino.odczytajzbazy();
- //Kino.dodajdobazy();
- //Kino.zmiendobazy();
- Kino.usunwszystko();
- }
- public static void odczytajzbazy() {
- Connection conn = null;
- String polaczenieURL = "jdbc:mysql://127.0.0.1/kino?user=root&password=root";
- try {
- //Ustawiamy dane dotyczące podłączenia
- conn = DriverManager.getConnection(polaczenieURL);
- //Ustawiamy sterownik MySQL
- Class.forName("com.mysql.jdbc.Driver");
- //Uruchamiamy zapytanie do bazy danych
- Statement stat = conn.createStatement();
- String szukajSQL = "Select * FROM cinema where autor = 2";
- ResultSet wynik = stat.executeQuery(szukajSQL);
- System.out.println("Wynik polecenia:\n" + szukajSQL);
- while (wynik.next()) {
- int id = wynik.getInt("id");
- System.out.println("id: " + id);
- System.out.println("autor: " + wynik.getString("autor"));
- System.out.println("rok: " + wynik.getString("rok"));
- System.out.println("chuj: " + wynik.getString("chuj"));
- System.out.println(" ---------------------- ");
- }
- wynik.close();
- stat.close();
- conn.close();
- } catch (Exception ex)
- {
- System.out.println("Nie mozna sie polaczyc" + ex.getMessage());
- }
- }
- public static void dodajdobazy() {
- Connection conn = null;
- String polaczenieURL = "jdbc:mysql://127.0.0.1/kino?user=root&password=root";
- try {
- //Ustawiamy dane dotyczące podłączenia
- conn = DriverManager.getConnection(polaczenieURL);
- //Ustawiamy sterownik MySQL
- Class.forName("com.mysql.jdbc.Driver");
- //Uruchamiamy zapytanie do bazy danych
- Statement stat = conn.createStatement();
- String dodajSQL = "INSERT INTO cinema(id,autor,rok,chuj) Values(14,12,12,'pizda')";
- stat.executeUpdate(dodajSQL);
- stat.close();
- conn.close();
- } catch (Exception ex)
- {
- System.out.println("Nie mozna sie polaczyc" + ex.getMessage());
- }
- }
- public static void zmiendobazy() {
- Connection conn = null;
- String polaczenieURL = "jdbc:mysql://127.0.0.1/kino?user=root&password=root";
- try {
- //Ustawiamy dane dotyczące podłączenia
- conn = DriverManager.getConnection(polaczenieURL);
- //Ustawiamy sterownik MySQL
- Class.forName("com.mysql.jdbc.Driver");
- //Uruchamiamy zapytanie do bazy danych
- Statement stat = conn.createStatement();
- String zmienSQL = "UPDATE cinema SET autor = 14,rok = 125 WHERE id=9";
- System.out.println("Polecenie zmiany:\n" + zmienSQL);
- // Uwaga: wywołujemy metodę executeUpdate
- stat.executeUpdate(zmienSQL);
- //String dodajSQL = "INSERT INTO cinema(id,autor,rok,chuj) Values(14,12,12,'pizda')";
- //stat.executeUpdate(dodajSQL);
- stat.close();
- conn.close();
- } catch (Exception ex)
- {
- System.out.println("Nie mozna sie polaczyc" + ex.getMessage());
- }
- }
- public static void usunwszystko() {
- Connection conn = null;
- String polaczenieURL = "jdbc:mysql://127.0.0.1/kino?user=root&password=root";
- try {
- //Ustawiamy dane dotyczące podłączenia
- conn = DriverManager.getConnection(polaczenieURL);
- //Ustawiamy sterownik MySQL
- Class.forName("com.mysql.jdbc.Driver");
- //Uruchamiamy zapytanie do bazy danych
- Statement stat = conn.createStatement();
- String usunSQL = "DELETE FROM cinema ";
- System.out.println("Polecenie:\n" + usunSQL);
- stat.executeUpdate(usunSQL);
- stat.close();
- conn.close();
- } catch (Exception ex)
- {
- System.out.println("Nie mozna sie polaczyc" + ex.getMessage());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement