Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package cas4;
- import java.sql.PreparedStatement;
- import java.sql.SQLException;
- public class DBF {
- static PreparedStatement pst = null;
- static String upit;
- /**
- * Dodaje novog radnika u bazu podataka iz već postojećeg objekta Radnik
- * @param r - instanca objekta koji se prosleđuje upitu za dodavanje u bazu
- * */
- @SuppressWarnings("finally")
- public static boolean unesiRadnika(Radnik r) {
- boolean rez = false;
- upit = "INSERT INTO RADNIK VALUES (?,?,?,?,?)";
- try {
- DBConnection.getConnection().setAutoCommit(false);
- pst = DBConnection.getConnection().prepareStatement(upit);
- pst.setInt(1, r.getMbr());
- pst.setString(2, r.getIme());
- pst.setString(3, r.getPrezime());
- pst.setString(4, r.getDatr());
- pst.setInt(5, r.getOzrm());
- pst.executeUpdate();
- DBConnection.getConnection().commit();
- rez = true;
- } catch(Exception e) {
- System.err.println("Greška pri dodavanju u bazu: " + e.getMessage());
- try {
- DBConnection.getConnection().rollback();
- } catch (SQLException e1) {
- System.err.println("Greška pri rollbacku: " + e1.getMessage());
- }
- } finally {
- if (pst != null) {
- try {
- pst.close();
- } catch (Exception e) {
- System.err.println(e.getMessage());
- }
- }
- return rez;
- }
- } // kraj unesiRadnka
- /**
- * Briše radno mjesto iz baze kaskadno
- * pri tome ćemo obrisati prvo radnike koji rade na tom radnom mjestu
- * a zatim i radno mjesto
- * @param ozrm - oznaka radnog mjesta koje želimo izbrisati
- * */
- @SuppressWarnings("finally")
- public static boolean obrisiRadnoMjesto(int ozrm) {
- boolean uspješno = false;
- upit = "DELETE FROM RADNIK WHERE ozrm=?";
- String upit2 = "DELETE FROM RADNOMESTO WHERE ozrm=?";
- try {
- DBConnection.getConnection().setAutoCommit(false);
- pst = DBConnection.getConnection().prepareStatement(upit);
- pst.setInt(1, 2);
- pst.executeUpdate();
- pst = DBConnection.getConnection().prepareStatement(upit2);
- pst.setInt(1, 2);
- pst.executeUpdate();
- DBConnection.getConnection().commit();
- uspješno = true;
- } catch (Exception e) {
- System.err.println(e.getMessage());
- try {
- DBConnection.getConnection().rollback();
- } catch (SQLException e1) {
- System.err.println(e1.getMessage());
- }
- } finally {
- if (pst != null) {
- try {
- pst.close();
- } catch (SQLException e) {
- System.err.println(e.getMessage());
- }
- }
- return uspješno;
- }
- } // kraj obrisiRadnoMjesto
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement