Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package cas3;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- public class DBF {
- static PreparedStatement pst = null;
- public static boolean unesiNastavnika(Nastavnik n) {
- try {
- String unos = "INSERT INTO NASTAVNIK VALUES (?, ?, ?, ?)";
- pst = DBConnection.getConnection().prepareStatement(unos);
- pst.setInt(1, n.getNastavnik_id());
- pst.setString(2, n.getIme());
- pst.setString(3, n.getPrezime());
- pst.setString(4, n.getZvanje());
- pst.executeUpdate();
- return true;
- } catch(Exception e) {
- System.err.println(e.getMessage());
- return false;
- } finally {
- if (pst != null) {
- try {
- pst.close();
- } catch (SQLException e) {
- System.err.println(e.getMessage());
- }
- }
- }
- } // kraj unesiNastavnika
- @SuppressWarnings("finally")
- public static boolean obrisiNastavnika(int nastavnik_id) {
- boolean rez = false;
- String izPredaje = "DELETE FROM PREDAJE WHERE nastavnik_id= ?";
- String izNastavnik = "DELETE FROM NASTAVNIK WHERE nastavnik_id= ?";
- try {
- DBConnection.getConnection().setAutoCommit(false);
- pst = DBConnection.getConnection().prepareStatement(izPredaje);
- pst.setInt(1, nastavnik_id);
- pst.executeUpdate();
- pst = DBConnection.getConnection().prepareStatement(izNastavnik);
- pst.setInt(1, nastavnik_id);
- pst.executeUpdate();
- DBConnection.getConnection().commit();
- rez = true;
- } catch (Exception e) {
- System.err.println(e.getMessage());
- try {
- DBConnection.getConnection().rollback();
- } catch (SQLException e1) {
- System.err.println(e.getMessage());
- }
- } finally {
- if (pst != null) {
- try {
- pst.close();
- } catch (SQLException e) {
- System.err.println(e.getMessage());
- }
- }
- return rez;
- }
- }
- public static void prikaziPredmete(int nastavnik_id) {
- Statement stmt = null;
- ResultSet rez = null;
- String upit = "SELECT naziv FROM PREDMET WHERE predmet_id IN (SELECT predmet_id FROM PREDAJE WHERE nastavnik_id=" + nastavnik_id + ")";
- try {
- stmt = DBConnection.getConnection().createStatement();
- rez = stmt.executeQuery(upit);
- while (rez.next()) {
- System.out.println(rez.getString(1));
- }
- } catch (Exception e) {
- System.err.println(e.getMessage());
- } finally {
- if (rez != null) {
- try {
- rez.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- if (stmt != null) {
- try {
- stmt.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- }
- @SuppressWarnings("finally")
- public static boolean promjeniPrezime(int nastavnik_id, String prezime) {
- boolean rez = false;
- String upit = "UPDATE NASTAVNIK SET prezime=? WHERE nastavnik_id=?";
- try {
- pst = DBConnection.getConnection().prepareStatement(upit);
- pst.setString(1, prezime);
- pst.setInt(2, nastavnik_id);
- pst.executeUpdate();
- rez = true;
- } catch(Exception e) {
- System.err.println(e.getMessage());
- } finally {
- if (pst != null) {
- try {
- pst.close();
- } catch (SQLException e) {
- System.err.println(e.getMessage());
- }
- }
- return rez;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement