Advertisement
ridjis

DBF

Mar 10th, 2015
460
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.26 KB | None | 0 0
  1. package cas3;
  2.  
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7.  
  8. public class DBF {
  9.     static PreparedStatement pst = null;
  10.  
  11.     public static boolean unesiNastavnika(Nastavnik n) {
  12.         try {
  13.             String unos = "INSERT INTO NASTAVNIK VALUES (?, ?, ?, ?)";
  14.             pst = DBConnection.getConnection().prepareStatement(unos);
  15.             pst.setInt(1, n.getNastavnik_id());
  16.             pst.setString(2, n.getIme());
  17.             pst.setString(3, n.getPrezime());
  18.             pst.setString(4, n.getZvanje());
  19.             pst.executeUpdate();
  20.            
  21.             return true;
  22.         } catch(Exception e) {
  23.             System.err.println(e.getMessage());
  24.            
  25.             return false;
  26.         } finally {
  27.             if (pst != null) {         
  28.                 try {
  29.                     pst.close();
  30.                 } catch (SQLException e) {
  31.                     System.err.println(e.getMessage());
  32.                 }
  33.             }
  34.         }
  35.     } // kraj unesiNastavnika
  36.    
  37.     @SuppressWarnings("finally")
  38.     public static boolean obrisiNastavnika(int nastavnik_id) {
  39.         boolean rez = false;
  40.         String izPredaje = "DELETE FROM PREDAJE WHERE nastavnik_id= ?";
  41.         String izNastavnik = "DELETE FROM NASTAVNIK WHERE nastavnik_id= ?";
  42.         try {
  43.             DBConnection.getConnection().setAutoCommit(false);
  44.            
  45.             pst = DBConnection.getConnection().prepareStatement(izPredaje);
  46.             pst.setInt(1, nastavnik_id);
  47.             pst.executeUpdate();
  48.            
  49.             pst = DBConnection.getConnection().prepareStatement(izNastavnik);
  50.             pst.setInt(1, nastavnik_id);
  51.             pst.executeUpdate();
  52.            
  53.             DBConnection.getConnection().commit();
  54.             rez = true;
  55.         } catch (Exception e) {
  56.             System.err.println(e.getMessage());
  57.             try {
  58.                 DBConnection.getConnection().rollback();
  59.             } catch (SQLException e1) {
  60.                 System.err.println(e.getMessage());
  61.             }
  62.         } finally {
  63.             if (pst != null) {         
  64.                 try {
  65.                     pst.close();
  66.                 } catch (SQLException e) {
  67.                     System.err.println(e.getMessage());
  68.                 }
  69.             }
  70.            
  71.             return rez;
  72.         }
  73.     }
  74.    
  75.     public static void prikaziPredmete(int nastavnik_id) {
  76.         Statement stmt = null;
  77.         ResultSet rez = null;
  78.         String upit = "SELECT naziv FROM PREDMET WHERE predmet_id IN (SELECT predmet_id FROM PREDAJE WHERE nastavnik_id=" + nastavnik_id + ")";
  79.        
  80.         try {
  81.             stmt = DBConnection.getConnection().createStatement();
  82.             rez = stmt.executeQuery(upit);
  83.            
  84.             while (rez.next()) {
  85.                 System.out.println(rez.getString(1));
  86.             }
  87.         } catch (Exception e) {
  88.             System.err.println(e.getMessage());
  89.         } finally {
  90.             if (rez != null) {
  91.                 try {
  92.                     rez.close();
  93.                 } catch (Exception e) {
  94.                     e.printStackTrace();
  95.                 }
  96.             }
  97.             if (stmt != null) {
  98.                 try {
  99.                     stmt.close();
  100.                 } catch (Exception e) {
  101.                     e.printStackTrace();
  102.                 }
  103.             }
  104.         }
  105.  
  106.     }
  107.    
  108.     @SuppressWarnings("finally")
  109.     public static boolean promjeniPrezime(int nastavnik_id, String prezime) {
  110.         boolean rez = false;
  111.         String upit = "UPDATE NASTAVNIK SET prezime=? WHERE nastavnik_id=?";
  112.        
  113.         try {
  114.             pst = DBConnection.getConnection().prepareStatement(upit);
  115.             pst.setString(1, prezime);
  116.             pst.setInt(2, nastavnik_id);
  117.             pst.executeUpdate();
  118.            
  119.             rez = true;
  120.         } catch(Exception e) {
  121.             System.err.println(e.getMessage());
  122.         } finally {
  123.             if (pst != null) {         
  124.                 try {
  125.                     pst.close();
  126.                 } catch (SQLException e) {
  127.                     System.err.println(e.getMessage());
  128.                 }
  129.             }
  130.            
  131.             return rez;
  132.         }
  133.        
  134.        
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement