Guest User

Untitled

a guest
Oct 2nd, 2018
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.17 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package jbdc_nogalski;
  7.  
  8. import java.sql.*;
  9. import java.util.Properties;
  10. import java.util.logging.Level;
  11. import java.util.logging.Logger;
  12.  
  13. /**
  14.  *
  15.  * @author student
  16.  */
  17. public class JBDC_Nogalski {
  18.  
  19.     /**
  20.      * @param args the command line arguments
  21.      */
  22.     public static void main(String[] args) {
  23.         Connection conn = null;
  24.         Properties connectionProps = new Properties();
  25.         connectionProps.put("user", "inf132294");
  26.         connectionProps.put("password", "inf132294");
  27.         try {
  28.             conn = DriverManager.getConnection("jdbc:oracle:thin:@//admlab2.cs.put.poznan.pl:1521/dblab02_students.cs.put.poznan.pl",connectionProps);
  29.             System.out.println("Połączono z bazą danych");
  30.         } catch (SQLException ex) {
  31.             Logger.getLogger(JBDC_Nogalski.class.getName()).log(Level.SEVERE,   "nie udało się połączyć z bazą danych", ex);
  32.             System.exit(-1);
  33.         }
  34.         Statement stmt = null;
  35.         ResultSet rs = null;
  36.         try {
  37.             stmt = conn.createStatement();
  38.             rs = stmt.executeQuery("select count(*) " + "from pracownicy");
  39.             while (rs.next()) {
  40.                 System.out.println("Zatrudniono " + rs.getInt(1) + "Pracowników, w tym:");
  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) {
  49.                     /* kod obsługi */
  50.                 }
  51.             }
  52.             if (stmt != null) {
  53.                 try {
  54.                     stmt.close();
  55.                 } catch (SQLException e) {
  56.                     /* kod obsługi */ }
  57.             }
  58.         }
  59.        
  60.         try {
  61.             stmt = conn.createStatement();
  62.             rs = stmt.executeQuery("select nazwisko, nazwa  " + "from pracownicy JOIN zespoly");
  63.             while (rs.next()) {
  64.                 System.out.println("Zatrudniono " + rs.getString(1) + "Pracowników, w tym:");
  65.             }
  66.         } catch (SQLException ex) {
  67.             System.out.println("Bład wykonania polecenia" + ex.toString());
  68.         } finally {
  69.             if (rs != null) {
  70.                 try {
  71.                     rs.close();
  72.                 } catch (SQLException e) {
  73.                     /* kod obsługi */
  74.                 }
  75.             }
  76.             if (stmt != null) {
  77.                 try {
  78.                     stmt.close();
  79.                 } catch (SQLException e) {
  80.                     /* kod obsługi */ }
  81.             }
  82.         }
  83.        
  84.        
  85.        
  86.         try {
  87.             conn.close();
  88.             System.out.println("Rozłączono z bazą danych");
  89.         } catch (SQLException ex) {
  90.             Logger.getLogger(JBDC_Nogalski.class.getName()).log(Level.SEVERE, null, ex);
  91.         }
  92.              
  93.     }
  94.    
  95. }
Add Comment
Please, Sign In to add comment