Advertisement
Guest User

Untitled

a guest
Oct 9th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.26 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.ArrayList;
  10. import java.util.List;
  11. import java.util.Properties;
  12. import java.util.logging.Level;
  13. import java.util.logging.Logger;
  14.  
  15. /**
  16.  *
  17.  * @author student
  18.  */
  19. public class JBDC_Nogalski {
  20.  
  21.     /**
  22.      * @param args the command line arguments
  23.      */
  24.     public static void main(String[] args) {
  25.         Connection conn = null;
  26.         Properties connectionProps = new Properties();
  27.         connectionProps.put("user", "inf132294");
  28.         connectionProps.put("password", "inf132294");
  29.         try {
  30.             conn = DriverManager.getConnection("jdbc:oracle:thin:@//admlab2.cs.put.poznan.pl:1521/dblab02_students.cs.put.poznan.pl",connectionProps);
  31.             System.out.println("Połączono z bazą danych");
  32.         } catch (SQLException ex) {
  33.             Logger.getLogger(JBDC_Nogalski.class.getName()).log(Level.SEVERE,   "nie udało się połączyć z bazą danych", ex);
  34.             System.exit(-1);
  35.         }
  36.        
  37.         //Zadanie 1
  38.         Statement stmt = null;
  39.         ResultSet rs = null;
  40.         try {
  41.             stmt = conn.createStatement();
  42.             rs = stmt.executeQuery("select count(*) " + "from pracownicy");
  43.             while (rs.next()) {
  44.                 System.out.println("Zatrudniono " + rs.getInt(1) + " pracowników, w tym:");
  45.             }
  46.         } catch (SQLException ex) {
  47.             System.out.println("Bład wykonania polecenia" + ex.toString());
  48.         } finally {
  49.             if (rs != null) {
  50.                 try {
  51.                     rs.close();
  52.                 } catch (SQLException e) {
  53.                     /* kod obsługi */
  54.                 }
  55.             }
  56.             if (stmt != null) {
  57.                 try {
  58.                     stmt.close();
  59.                 } catch (SQLException e) {
  60.                     /* kod obsługi */ }
  61.             }
  62.         }
  63.         System.out.println("Zadanie 1");
  64.         System.out.println("");
  65.         try {
  66.             stmt = conn.createStatement();
  67.             rs = stmt.executeQuery("SELECT p.nazwisko, z.nazwa FROM pracownicy p natural join zespoly z");
  68.             while (rs.next()) {
  69.                 System.out.println(rs.getString(1) + "w zespole " + rs.getString(2) + ",");
  70.             }
  71.         } catch (SQLException ex) {
  72.             System.out.println("Bład wykonania polecenia" + ex.toString());
  73.         } finally {
  74.             if (rs != null) {
  75.                 try {
  76.                     rs.close();
  77.                 } catch (SQLException e) {
  78.                     /* kod obsługi */
  79.                 }
  80.             }
  81.             if (stmt != null) {
  82.                 try {
  83.                     stmt.close();
  84.                 } catch (SQLException e) {
  85.                     /* kod obsługi */ }
  86.             }
  87.         }
  88.         System.out.println("");
  89.         System.out.println("Zadanie 2");
  90.         System.out.println("");
  91.         //zadanie 2
  92.         try {
  93.             stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
  94.             rs = stmt.executeQuery("SELECT nazwisko, placa_pod FROM pracownicy WHERE etat = 'ASYSTENT' ORDER BY placa_pod DESC");
  95.             rs.last();
  96.                 System.out.println(rs.getString(1) + ": " + rs.getFloat(2) + ",");
  97.            
  98.             rs.absolute(-3);
  99.                 System.out.println(rs.getString(1) + ": " + rs.getFloat(2) + ",");
  100.            
  101.             rs.absolute(2);
  102.                 System.out.println(rs.getString(1) + ": " + rs.getFloat(2) + ",");
  103.            
  104.         } catch (SQLException ex) {
  105.             System.out.println("Bład wykonania polecenia" + ex.toString());
  106.         } finally {
  107.             if (rs != null) {
  108.                 try {
  109.                     rs.close();
  110.                 } catch (SQLException e) {
  111.                     /* kod obsługi */
  112.                 }
  113.             }
  114.             if (stmt != null) {
  115.                 try {
  116.                     stmt.close();
  117.                 } catch (SQLException e) {
  118.                     /* kod obsługi */ }
  119.             }
  120.         }
  121.        
  122.         //zadanie 3
  123.         System.out.println("");
  124.         try {
  125.             int changes = 0;
  126.             int [] zwolnienia={150, 200, 230};
  127.             String [] zatrudnienia={"Kandefer", "Rygiel", "Boczar"};
  128.             stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
  129.             for(int i = 0; i < zwolnienia.length; i++){
  130.                //changes += stmt.executeUpdate("DELETE FROM pracownicy WHERE id_prac = " + zwolnienia[i]);
  131.             }
  132.             System.out.println("Usunieto " + changes + " pracownikow");
  133.            
  134.             Statement stmtSEQ = conn.createStatement();
  135.  
  136.             ResultSet seq = null;
  137.             ArrayList<Integer> ids = new ArrayList<>();
  138.             for (int i = 0; i < zatrudnienia.length; i++){
  139.                 seq = stmtSEQ.executeQuery("SELECT my_seq.NEXTVAL FROM dual");
  140.                 seq.next();
  141.                 int id = seq.getInt(1);
  142.                 ids.add(id);
  143.             }
  144.             seq.close();
  145.             System.out.println(ids.size());
  146.             changes = 0;
  147.             for (int i = 0; i < zatrudnienia.length; i++){
  148.                String query = "INSERT INTO pracownicy(id_prac, nazwisko) " + "VALUES(" + ids.get(i) + ",'" + zatrudnienia[i] + "')";
  149.                System.out.println(query);
  150.                changes += stmt.executeUpdate(query);
  151.             }
  152.         } catch (SQLException ex) {
  153.             System.out.println("Bład wykonania polecenia" + ex.toString());
  154.         } finally {
  155.             if (rs != null) {
  156.                 try {
  157.                     rs.close();
  158.                 } catch (SQLException e) {
  159.                     /* kod obsługi */
  160.                 }
  161.             }
  162.             if (stmt != null) {
  163.                 try {
  164.                     stmt.close();
  165.                 } catch (SQLException e) {
  166.                     /* kod obsługi */ }
  167.             }
  168.         }
  169.         //zadanie 4
  170.         System.out.println("");
  171.         System.out.println("Zadanie 4");
  172.         System.out.println("");
  173.        
  174.         try {
  175.             conn.setAutoCommit(false);
  176.             stmt = conn.createStatement();
  177.             rs = stmt.executeQuery("select *" + "from etaty");
  178.             System.out.println("Przed dodaniem");
  179.             while (rs.next()) {
  180.                 System.out.println("nazwa " + rs.getString(1) + " placa min: " + rs.getFloat(2) + " placa max: " + rs.getFloat(3));
  181.             }
  182.             System.out.println("");
  183.             stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
  184.             int changes = stmt.executeUpdate("INSERT INTO etaty(nazwa, placa_min, placa_max) " + "VALUES('test123', 123, 456)");
  185.            
  186.             stmt = conn.createStatement();
  187.             rs = stmt.executeQuery("select *" + "from etaty");
  188.             System.out.println("Po dodaniu");
  189.             while (rs.next()) {
  190.                 System.out.println("nazwa " + rs.getString(1) + " placa min: " + rs.getFloat(2) + " placa max: " + rs.getFloat(3));
  191.             }
  192.             System.out.println("");
  193.             conn.rollback();
  194.             System.out.println("po rollbacku");
  195.             stmt = conn.createStatement();
  196.             rs = stmt.executeQuery("select *" + "from etaty");
  197.             while (rs.next()) {
  198.                 System.out.println("nazwa " + rs.getString(1) + " placa min: " + rs.getFloat(2) + " placa max: " + rs.getFloat(3));
  199.             }
  200.             System.out.println("");
  201.             stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
  202.             System.out.println("przed Insertem");
  203.             changes = stmt.executeUpdate("INSERT INTO etaty(nazwa, placa_min, placa_max) " + "VALUES('test123', 123, 456)");
  204.             conn.commit();
  205.             stmt = conn.createStatement();
  206.             System.out.println("Po insercie");
  207.             rs = stmt.executeQuery("select *" + "from etaty");
  208.             while (rs.next()) {
  209.                 System.out.println("nazwa " + rs.getString(1) + " placa min: " + rs.getFloat(2) + " placa max: " + rs.getFloat(3));
  210.             }
  211.         } catch (SQLException ex) {
  212.             System.out.println("Bład wykonania polecenia" + ex.toString());
  213.         } finally {
  214.             if (rs != null) {
  215.                 try {
  216.                     rs.close();
  217.                 } catch (SQLException e) {
  218.                     /* kod obsługi */
  219.                 }
  220.             }
  221.             if (stmt != null) {
  222.                 try {
  223.                     stmt.close();
  224.                 } catch (SQLException e) {
  225.                     /* kod obsługi */ }
  226.             }
  227.         }
  228.        
  229.         try {
  230.             conn.close();
  231.             System.out.println("Rozłączono z bazą danych");
  232.         } catch (SQLException ex) {
  233.             Logger.getLogger(JBDC_Nogalski.class.getName()).log(Level.SEVERE, null, ex);
  234.         }
  235.              
  236.     }
  237.    
  238. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement