Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.57 KB | None | 0 0
  1. package Zbior;
  2.  
  3.  
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.ResultSet;
  7. import java.sql.Statement;
  8.  
  9. public class Kino {
  10.  
  11. static String baza = "cinema";
  12. static String pole = "RODZAJ";
  13. static String wartosc = "Acer";
  14.  
  15. public static void main(String[] args) {
  16.  
  17. //Kino.odczytajzbazy();
  18. //Kino.dodajdobazy();
  19. //Kino.zmiendobazy();
  20. Kino.usunwszystko();
  21. }
  22.  
  23. public static void odczytajzbazy() {
  24.  
  25. Connection conn = null;
  26. String polaczenieURL = "jdbc:mysql://127.0.0.1/kino?user=root&password=root";
  27. try {
  28.  
  29. //Ustawiamy dane dotyczące podłączenia
  30. conn = DriverManager.getConnection(polaczenieURL);
  31.  
  32. //Ustawiamy sterownik MySQL
  33. Class.forName("com.mysql.jdbc.Driver");
  34.  
  35. //Uruchamiamy zapytanie do bazy danych
  36. Statement stat = conn.createStatement();
  37.  
  38. String szukajSQL = "Select * FROM cinema where autor = 2";
  39.  
  40. ResultSet wynik = stat.executeQuery(szukajSQL);
  41. System.out.println("Wynik polecenia:\n" + szukajSQL);
  42. while (wynik.next()) {
  43. int id = wynik.getInt("id");
  44. System.out.println("id: " + id);
  45. System.out.println("autor: " + wynik.getString("autor"));
  46. System.out.println("rok: " + wynik.getString("rok"));
  47. System.out.println("chuj: " + wynik.getString("chuj"));
  48. System.out.println(" ---------------------- ");
  49. }
  50.  
  51. wynik.close();
  52. stat.close();
  53. conn.close();
  54. } catch (Exception ex)
  55.  
  56. {
  57. System.out.println("Nie mozna sie polaczyc" + ex.getMessage());
  58. }
  59.  
  60.  
  61. }
  62.  
  63. public static void dodajdobazy() {
  64.  
  65. Connection conn = null;
  66. String polaczenieURL = "jdbc:mysql://127.0.0.1/kino?user=root&password=root";
  67. try {
  68.  
  69. //Ustawiamy dane dotyczące podłączenia
  70. conn = DriverManager.getConnection(polaczenieURL);
  71.  
  72. //Ustawiamy sterownik MySQL
  73. Class.forName("com.mysql.jdbc.Driver");
  74.  
  75. //Uruchamiamy zapytanie do bazy danych
  76. Statement stat = conn.createStatement();
  77.  
  78.  
  79. String dodajSQL = "INSERT INTO cinema(id,autor,rok,chuj) Values(14,12,12,'pizda')";
  80. stat.executeUpdate(dodajSQL);
  81.  
  82.  
  83. stat.close();
  84. conn.close();
  85. } catch (Exception ex)
  86.  
  87. {
  88. System.out.println("Nie mozna sie polaczyc" + ex.getMessage());
  89. }
  90.  
  91.  
  92. }
  93.  
  94.  
  95. public static void zmiendobazy() {
  96.  
  97. Connection conn = null;
  98. String polaczenieURL = "jdbc:mysql://127.0.0.1/kino?user=root&password=root";
  99. try {
  100.  
  101. //Ustawiamy dane dotyczące podłączenia
  102. conn = DriverManager.getConnection(polaczenieURL);
  103.  
  104. //Ustawiamy sterownik MySQL
  105. Class.forName("com.mysql.jdbc.Driver");
  106.  
  107. //Uruchamiamy zapytanie do bazy danych
  108. Statement stat = conn.createStatement();
  109.  
  110.  
  111. String zmienSQL = "UPDATE cinema SET autor = 14,rok = 125 WHERE id=9";
  112. System.out.println("Polecenie zmiany:\n" + zmienSQL);
  113. // Uwaga: wywołujemy metodę executeUpdate
  114. stat.executeUpdate(zmienSQL);
  115.  
  116.  
  117. //String dodajSQL = "INSERT INTO cinema(id,autor,rok,chuj) Values(14,12,12,'pizda')";
  118. //stat.executeUpdate(dodajSQL);
  119.  
  120.  
  121. stat.close();
  122. conn.close();
  123. } catch (Exception ex)
  124.  
  125. {
  126. System.out.println("Nie mozna sie polaczyc" + ex.getMessage());
  127. }
  128.  
  129.  
  130. }
  131.  
  132.  
  133. public static void usunwszystko() {
  134.  
  135. Connection conn = null;
  136. String polaczenieURL = "jdbc:mysql://127.0.0.1/kino?user=root&password=root";
  137. try {
  138.  
  139. //Ustawiamy dane dotyczące podłączenia
  140. conn = DriverManager.getConnection(polaczenieURL);
  141.  
  142. //Ustawiamy sterownik MySQL
  143. Class.forName("com.mysql.jdbc.Driver");
  144.  
  145. //Uruchamiamy zapytanie do bazy danych
  146. Statement stat = conn.createStatement();
  147.  
  148.  
  149. String usunSQL = "DELETE FROM cinema ";
  150.  
  151. System.out.println("Polecenie:\n" + usunSQL);
  152. stat.executeUpdate(usunSQL);
  153.  
  154.  
  155. stat.close();
  156. conn.close();
  157. } catch (Exception ex)
  158.  
  159. {
  160. System.out.println("Nie mozna sie polaczyc" + ex.getMessage());
  161. }
  162.  
  163.  
  164. }
  165.  
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement