Advertisement
ridjis

DBF Radnik

Mar 17th, 2015
446
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.54 KB | None | 0 0
  1. package cas4;
  2.  
  3. import java.sql.PreparedStatement;
  4. import java.sql.SQLException;
  5.  
  6. public class DBF {
  7.     static PreparedStatement pst = null;
  8.     static String upit;
  9.    
  10.     /**
  11.      * Dodaje novog radnika u bazu podataka iz već postojećeg objekta Radnik
  12.      * @param r - instanca objekta koji se prosleđuje upitu za dodavanje u bazu
  13.      * */
  14.     @SuppressWarnings("finally")
  15.     public static boolean unesiRadnika(Radnik r) {
  16.         boolean rez = false;
  17.         upit = "INSERT INTO RADNIK VALUES (?,?,?,?,?)";
  18.        
  19.         try {
  20.             DBConnection.getConnection().setAutoCommit(false);
  21.            
  22.             pst = DBConnection.getConnection().prepareStatement(upit);
  23.             pst.setInt(1, r.getMbr());
  24.             pst.setString(2, r.getIme());
  25.             pst.setString(3, r.getPrezime());
  26.             pst.setString(4, r.getDatr());
  27.             pst.setInt(5, r.getOzrm());        
  28.             pst.executeUpdate();
  29.            
  30.             DBConnection.getConnection().commit();
  31.             rez = true;
  32.         } catch(Exception e) {
  33.             System.err.println("Greška pri dodavanju u bazu: " + e.getMessage());
  34.            
  35.             try {
  36.                 DBConnection.getConnection().rollback();
  37.             } catch (SQLException e1) {
  38.                 System.err.println("Greška pri rollbacku: " + e1.getMessage());
  39.             }
  40.         } finally {
  41.             if (pst != null) {
  42.                 try {
  43.                     pst.close();
  44.                 } catch (Exception e) {
  45.                     System.err.println(e.getMessage());
  46.                 }
  47.             }
  48.             return rez;
  49.         }
  50.     } // kraj unesiRadnka
  51.  
  52.     /**
  53.      * Briše radno mjesto iz baze kaskadno
  54.      * pri tome ćemo obrisati prvo radnike koji rade na tom radnom mjestu
  55.      * a zatim i radno mjesto
  56.      * @param ozrm - oznaka radnog mjesta koje želimo izbrisati
  57.      * */
  58.     @SuppressWarnings("finally")
  59.     public static boolean obrisiRadnoMjesto(int ozrm) {
  60.         boolean uspješno = false;
  61.         upit = "DELETE FROM RADNIK WHERE ozrm=?";
  62.         String upit2 = "DELETE FROM RADNOMESTO WHERE ozrm=?";
  63.        
  64.         try {
  65.             DBConnection.getConnection().setAutoCommit(false);
  66.            
  67.             pst = DBConnection.getConnection().prepareStatement(upit);
  68.             pst.setInt(1, 2);
  69.             pst.executeUpdate();
  70.            
  71.             pst = DBConnection.getConnection().prepareStatement(upit2);
  72.             pst.setInt(1, 2);
  73.             pst.executeUpdate();
  74.            
  75.             DBConnection.getConnection().commit();
  76.             uspješno = true;
  77.         } catch (Exception e) {
  78.             System.err.println(e.getMessage());
  79.            
  80.             try {
  81.                 DBConnection.getConnection().rollback();
  82.             } catch (SQLException e1) {            
  83.                 System.err.println(e1.getMessage());
  84.             }
  85.         } finally {
  86.             if (pst != null) {
  87.                 try {
  88.                     pst.close();
  89.                 } catch (SQLException e) {                 
  90.                     System.err.println(e.getMessage());
  91.                 }
  92.             }
  93.             return uspješno;
  94.         }
  95.     } // kraj obrisiRadnoMjesto
  96.  
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement