Advertisement
Paczek

Java laczenie z baza 2

Dec 1st, 2016
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.14 KB | None | 0 0
  1. package bazy;
  2.  
  3. import java.sql.*;
  4.  
  5. public class Bazy {
  6.  
  7.     public static void main(String[] args) {
  8.        
  9.         String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  10.         String DB_URL = "jdbc:mysql://localhost/panstwa";
  11.         String USER = "root";
  12.         String PASS = "";
  13.         Connection conn = null;
  14.         Statement stmt = null;
  15.         ResultSet rs = null;
  16.        
  17.         try {
  18.             //ladowanie sterownika
  19.             System.out.println("Ładuję sterownik...");
  20.             Class.forName(JDBC_DRIVER).newInstance();
  21.             System.out.println("Sterownik załadowany");
  22.            
  23.             //polaczenie do bazy
  24.             System.out.println("Łączę z bazą...");
  25.             conn = DriverManager.getConnection(DB_URL, USER, PASS);
  26.            
  27.             //ZAPYTANIA
  28.             String sql;
  29.             System.out.println("Tworzę zapytania...");
  30.             stmt = conn.createStatement();
  31.            
  32.             //usuwanie tabeli
  33.             System.out.println("Usuwanie tabeli...");
  34.             sql = "Drop Table panstwa";
  35.             stmt.executeUpdate(sql);
  36.  
  37.          
  38.             //tworzenie tabeli
  39.             System.out.println("Tworzenie tabeli...");
  40.             sql = "CREATE TABLE panstwa (nazwa varchar(30), stolica varchar(30), obszar double, ludnosc integer, dlgranicy double);";
  41.             stmt.executeUpdate(sql);
  42.            
  43.             //dodawanie rekordow
  44.             System.out.println("Dodawanie rekordow...");
  45.             sql = "INSERT INTO panstwa(nazwa,stolica,obszar,ludnosc,dlgranicy) VALUES ('Rosja','Moskwa','17075400','144192450','999'), ('Chiny','Peki','9597000','1357000000','999'), ('Indie','Nowe Delhi','3287590','1236344631','999'), ('Brunei','Bandar Seri Begawan','5770','381371','999')";
  46.             stmt.executeUpdate(sql);
  47.            
  48.             System.out.println("Pobieranie danych...");
  49.             sql = "SELECT * FROM panstwa ORDER BY nazwa ASC";
  50.             rs = stmt.executeQuery(sql);
  51.            
  52.             System.out.println();
  53.             while(rs.next()){
  54.               //Przetwarzanie po kolumnach
  55.                 String nazwa = rs.getString("nazwa");
  56.                 String stolica = rs.getString("stolica");
  57.                 int ludnosc = rs.getInt("ludnosc");
  58.                 double obszar = rs.getDouble("obszar");
  59.                 double dlgranicy = rs.getDouble("dlgranicy");
  60.               //Wyświetlenie wyniku
  61.                 System.out.print("Nazwa: " + nazwa);
  62.                 System.out.print(", obszar: " + obszar);
  63.                 System.out.print(", ludnosc: " + ludnosc);
  64.                 System.out.print(", gestosc zaludnienia: " + ludnosc/obszar);
  65.                 System.out.print(", dlgranicy/obszar: " + dlgranicy/obszar+"\n");
  66.             }
  67.         }
  68.         catch (Exception ex) {
  69.             System.out.println(ex);
  70.         }
  71.         finally {
  72.             try{
  73.                //KROK 7 - próba zamknięcia połączeń
  74.                rs.close();
  75.                stmt.close();
  76.                conn.close();
  77.             }
  78.             catch (Exception ex) {
  79.                 System.out.println(ex);
  80.             }
  81.         }
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement