Advertisement
patryk

SBD - Lab 04.11

Nov 4th, 2015
400
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.22 KB | None | 0 0
  1. package lab_jdbc;
  2.  
  3. import java.sql.*;
  4. import java.util.Properties;
  5. import java.util.logging.Level;
  6. import java.util.logging.Logger;
  7.  
  8. public class Lab_JDBC {
  9.  
  10.     private static void zad1() {
  11.         Connection conn = null;
  12.         Properties connectionProps = new Properties();
  13.         connectionProps.put("user", "inf117288");
  14.         connectionProps.put("password", "inf117288");
  15.         try {
  16.             conn = DriverManager.getConnection("jdbc:oracle:thin:@//admlab2-main.cs.put.poznan.pl:1521/dblab01.cs.put.poznan.pl",
  17.                     connectionProps);
  18.  
  19.             System.out.println("Połączono z bazą danych");
  20.         } catch (SQLException ex) {
  21.             Logger.getLogger(Lab_JDBC.class.getName()).log(Level.SEVERE,
  22.                     "nie udało się połączyć z bazą danych", ex);
  23.             System.exit(-1);
  24.         }
  25.  
  26.         Statement stmt = null;
  27.         ResultSet rs = null;
  28.         try {
  29.             stmt = conn.createStatement();
  30.             rs = stmt.executeQuery("select p.count(*), p.nazwisko, z.nazwa "
  31.                     + "from pracownicy p join zespoly z on p.id_zesp = z.id_zesp");
  32.  
  33.             System.out.println("Zatrudniono " + rs.getString(1) + " pracowników, w tym: ");
  34.             while (rs.next()) {
  35.                 System.out.print(rs.getString(2) + " w zespole " + rs.getString(3));
  36.                 if (rs.next()) {
  37.                     System.out.println(",");
  38.                 } else {
  39.                     System.out.println();
  40.                 }
  41.             }
  42.         } catch (SQLException ex) {
  43.             System.out.println("Bład wykonania polecenia" + ex.toString());
  44.         } finally {
  45.             if (rs != null) {
  46.                 try {
  47.                     rs.close();
  48.                 } catch (SQLException e) { /* kod obsługi */ }
  49.             }
  50.             if (stmt != null) {
  51.                 try {
  52.                     stmt.close();
  53.                 } catch (SQLException e) { /* kod obsługi */ }
  54.             }
  55.  
  56.             try {
  57.                 conn.close();
  58.                 System.out.println("Rozlaczono z baza danych");
  59.             } catch (SQLException ex) {
  60.                 Logger.getLogger(Lab_JDBC.class.getName()).log(Level.SEVERE, null, ex);
  61.             }
  62.         }
  63.     }
  64.  
  65.     private static void zad2() {
  66.         Connection conn = null;
  67.         Properties connectionProps = new Properties();
  68.         connectionProps.put("user", "inf117288");
  69.         connectionProps.put("password", "inf117288");
  70.         try {
  71.             conn = DriverManager.getConnection("jdbc:oracle:thin:@//admlab2-main.cs.put.poznan.pl:1521/dblab01.cs.put.poznan.pl",
  72.                     connectionProps);
  73.  
  74.             System.out.println("Połączono z bazą danych");
  75.         } catch (SQLException ex) {
  76.             Logger.getLogger(Lab_JDBC.class.getName()).log(Level.SEVERE,
  77.                     "nie udało się połączyć z bazą danych", ex);
  78.             System.exit(-1);
  79.         }
  80.  
  81.         Statement stmt = null;
  82.         ResultSet rs = null;
  83.         try {
  84.             stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
  85.                     ResultSet.CONCUR_READ_ONLY);
  86.  
  87.             rs = stmt.executeQuery("select p.nazwisko "
  88.                     + "from pracownicy p join etaty e where e.nazwa = ASYSTENT ");
  89.  
  90.             rs.afterLast();
  91.             while (rs.previous()) {
  92.                 System.out.println(rs.getString(1));
  93.             }
  94.  
  95.         } catch (SQLException ex) {
  96.             System.out.println("Bład wykonania polecenia" + ex.toString());
  97.         } finally {
  98.             if (rs != null) {
  99.                 try {
  100.                     rs.close();
  101.                 } catch (SQLException e) { /* kod obsługi */ }
  102.             }
  103.             if (stmt != null) {
  104.                 try {
  105.                     stmt.close();
  106.                 } catch (SQLException e) { /* kod obsługi */ }
  107.             }
  108.  
  109.             try {
  110.                 conn.close();
  111.                 System.out.println("Rozlaczono z baza danych");
  112.             } catch (SQLException ex) {
  113.                 Logger.getLogger(Lab_JDBC.class.getName()).log(Level.SEVERE, null, ex);
  114.             }
  115.         }
  116.     }
  117.  
  118.     public static void main(String[] args) {
  119.         zad2();
  120.     }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement