Advertisement
Guest User

JDBC

a guest
Nov 26th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.36 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 lab_jdbc;
  7.  
  8. import java.sql.CallableStatement;
  9. import java.sql.Connection;
  10. import java.sql.DriverManager;
  11. import java.sql.PreparedStatement;
  12. import java.sql.ResultSet;
  13. import java.sql.SQLException;
  14. import java.sql.Statement;
  15. import java.util.Properties;
  16. import java.util.logging.Level;
  17. import java.util.logging.Logger;
  18.  
  19. /**
  20.  *
  21.  * @author student
  22.  */
  23. public class Lab_JDBC {
  24.  
  25.     /**
  26.      * @param args the command line arguments
  27.      */
  28.     public static void main(String[] args) {
  29.         Connection conn = null;
  30.         Properties connectionProps = new Properties();
  31.         connectionProps.put("user", "inf127216");
  32.         connectionProps.put("password", "******");
  33.         try {
  34.             conn = DriverManager.getConnection("jdbc:oracle:thin:@//admlab2.cs.put.poznan.pl:1521/dblab02_students.cs.put.poznan.pl", connectionProps);
  35.             System.out.println("Połączono z bazą danych");
  36.             Statement stmt = null;
  37.             ResultSet rs = null;
  38.             try {
  39.                 stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
  40.                 /*rs = stmt.executeQuery("select id_prac, rpad(nazwisko, 15), placa_pod "
  41.                         + "from pracownicy");
  42.                 rs.afterLast();
  43.                 while (rs.previous()) {
  44.                     System.out.println(rs.getInt(1) + " " + rs.getString(2) + " "
  45.                             + rs.getFloat(3));
  46.                 }
  47.                 int changes;
  48.                 changes = stmt.executeUpdate("INSERT INTO pracownicy(id_prac,nazwisko) "
  49.                         + "VALUES(5,'Marecki')");
  50.                 System.out.println("Wstawiono " + changes + " krotek.");
  51.                 changes = stmt.executeUpdate("UPDATE pracownicy SET "
  52.                         + "placa_pod=placa_pod*1.5");
  53.                 System.out.println("Zmodyfikowano " + changes + " krotek.");
  54.                 changes = stmt.executeUpdate("DELETE FROM pracownicy WHERE id_prac=5");
  55.                 System.out.println("Usunieto " + changes + " krotek.");
  56.                 conn.setAutoCommit(false);
  57.                 stmt = conn.createStatement();
  58.                 stmt.executeUpdate("INSERT INTO pracownicy(id_prac,nazwisko)"
  59.                         + "VALUES(200,'Nowacki')");
  60.                 //conn.rollback();
  61.                 PreparedStatement pstmt = conn.prepareStatement("SELECT nazwisko FROM pracownicy WHERE id_prac=?");
  62.                 pstmt.setInt(1, 120);
  63.                 rs = pstmt.executeQuery();
  64.                 //rs = stmt.executeQuery("select id_prac, rpad(nazwisko, 15), placa_pod " + "from pracownicy");
  65.                 //rs.afterLast();
  66.                 while (rs.next()) {
  67.                     System.out.println(rs.getInt(1) + " " + rs.getString(2) + " "
  68.                             + rs.getFloat(3));
  69.                 }
  70.                 rs.close();
  71.                 pstmt.setInt(1, 150);
  72.                 rs = pstmt.executeQuery();
  73.                 while (rs.next()) {
  74.                     System.out.println(rs.getInt(1) + " " + rs.getString(2) + " "
  75.                             + rs.getFloat(3));
  76.                 }
  77.                 rs.close();
  78.                 pstmt.close();
  79.  
  80.                 PreparedStatement pstmt;
  81.                 pstmt = conn.prepareStatement("UPDATE pracownicy SET "
  82.                         + "placa_pod=placa_pod * ? WHERE id_prac = ? ");
  83.  
  84.                 pstmt.setFloat(1, new Float(1));
  85.                 pstmt.setInt(2, 130);
  86.                 pstmt.addBatch();
  87.                 pstmt.setFloat(1, new Float(0));
  88.                 pstmt.setInt(2, 0);
  89.                 pstmt.addBatch();
  90.                
  91.                 pstmt.executeBatch(); */
  92.  
  93.                 CallableStatement stmt1 = conn.prepareCall("{call WstawZespol(?,?,?)}");
  94.                 stmt1.setInt(1, 60);
  95.                 stmt1.setString(2, "NOWY ZESPÓŁ");
  96.                 stmt1.setString(3, "PIOTROWO 3A");
  97.                 stmt1.execute();
  98.                 stmt1.close();
  99.  
  100.                 rs = stmt.executeQuery("select id_zesp, nazwa, adres " + "from zespoly");
  101.                 //rs.afterLast();
  102.                 while (rs.next()) {
  103.                     System.out.println(rs.getInt(1) + " " + rs.getString(2) + " "
  104.                             + rs.getString(3));
  105.                 }
  106.  
  107.             } catch (SQLException ex) {
  108.                 System.out.println("Bład wykonania polecenia" + ex.toString());
  109.             } finally {
  110.                 if (rs != null) {
  111.                     try {
  112.                         rs.close();
  113.                     } catch (SQLException e) {
  114.                         /* kod obsługi */ }
  115.                 }
  116.                 if (stmt != null) {
  117.                     try {
  118.                         stmt.close();
  119.                     } catch (SQLException e) {
  120.                         /* kod obsługi */ }
  121.                 }
  122.             }
  123.             conn.close();
  124.             System.out.println("Rozłączono z bazą danych");
  125.         } catch (SQLException ex) {
  126.             Logger.getLogger(Lab_JDBC.class.getName()).log(Level.SEVERE,
  127.                     "nie udało się połączyć z bazą danych", ex);
  128.             System.exit(-1);
  129.         }
  130.  
  131.     }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement