Advertisement
TeamFocus-Matija

PS

Apr 14th, 2018
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.53 KB | None | 0 0
  1. //DB
  2. //DatabaseConnection
  3.  
  4. public class DatabaseConnection {
  5.     private final Connection connection;
  6.     private static DatabaseConnection instance;
  7.  
  8.     public DatabaseConnection() throws Exception {
  9.         DatabaseRecources dbr = new DatabaseRecources();
  10.         connection = DriverManager.getConnection(dbr.getUrl(), dbr.getUsername(), dbr.getPassword());
  11.         System.out.println("Uspostavljena konekcija");
  12.     }
  13.  
  14.     public Connection getConnection() {
  15.         return connection;
  16.     }
  17.  
  18.     public static DatabaseConnection getInstance() throws Exception{
  19.         if(instance == null){
  20.             instance = new DatabaseConnection();
  21.         }
  22.        
  23.         return instance;
  24.     }
  25. }
  26.  
  27. //DatabaseRecources
  28.  
  29. public class DatabaseRecources {
  30.     Properties properties;
  31.    
  32.     public DatabaseRecources() throws Exception {
  33.         properties = new Properties();
  34.         FileInputStream fis = new FileInputStream("C:\\Users\\Ana Milekic\\Documents\\NetBeansProjects\\PS-Kolokvijum\\resources\\database.config");
  35.         properties.load(fis);
  36.     }
  37.    
  38.     public String getUrl(){
  39.         return properties.getProperty(Constants.URL);
  40.     }
  41.    
  42.     public String getUsername(){
  43.         return properties.getProperty("username");
  44.     }
  45.    
  46.     public String getPassword(){
  47.         return properties.getProperty("password");
  48.     }
  49. }
  50.  
  51. //DatabaseRepository
  52. public class DatabaseRepository {
  53.    
  54.     public Predmet savePredmet(Predmet predmet) throws Exception {
  55.         Connection connection = DatabaseConnection.getInstance().getConnection();
  56.         connection.setAutoCommit(false);
  57.  
  58.         String queryBill = "INSERT INTO predmet(Naziv,Problem,Datum,Advokat,KlijentID,VrstaPostupkaId) values(?,?,?,?,?,?)";
  59.         PreparedStatement ps = connection.prepareStatement(queryBill,Statement.RETURN_GENERATED_KEYS);
  60.         ps.setString(1, predmet.getNaziv());
  61.         ps.setString(2, predmet.getProblem());
  62.         ps.setDate(3, Date.valueOf(LocalDate.now()));
  63.         ps.setString(4, predmet.getAdvokat().getIme());
  64.         ps.setLong(5, predmet.getKlijent().getKlijentID());
  65.         ps.setLong(6, predmet.getVrstaPostupka().getVrstaPostupkaID());
  66.         System.out.println(ps);
  67.  
  68.         ps.executeUpdate();
  69.         ResultSet rs = ps.getGeneratedKeys();
  70.         if (rs.next()) {
  71.             Long predmetId = rs.getLong(1);
  72.             System.out.println(predmetId);
  73.            
  74.             connection.commit();
  75.             ps.close();
  76.         }
  77.        
  78.         return predmet;
  79.     }
  80.    
  81.     public List<Klijent> findAllKlijent() throws Exception {
  82.         List<Klijent> clients = new ArrayList<>();
  83.         Connection connection = DatabaseConnection.getInstance().getConnection();
  84.         String query = "SELECT * FROM klijent ORDER BY Prezime ASC";
  85.         Statement s = connection.createStatement();
  86.  
  87.         ResultSet rs = s.executeQuery(query);
  88.         while (rs.next()) {
  89.             Long klijentID = rs.getLong("KlijentID");
  90.             String ime = rs.getString("Ime");
  91.             String prezime = rs.getString("Prezime");
  92.             String telefon = rs.getString("Telefon");
  93.             String elPosta = rs.getString("ElPosta");
  94.             String adresa = rs.getString("Adresa");
  95.            
  96.             Klijent client = new Klijent(klijentID, ime, prezime, telefon, elPosta, adresa);
  97.             clients.add(client);
  98.         }
  99.         rs.close();
  100.         s.close();
  101.         return clients;
  102.     }
  103.  
  104.     public List<VrstaPostupka> findAllVrstaPostupka() throws Exception {
  105.         List<VrstaPostupka> vrste = new ArrayList<>();
  106.         Connection connection = DatabaseConnection.getInstance().getConnection();
  107.         String query = "SELECT * FROM vrstapostupka ORDER BY naziv DESC";
  108.         Statement s = connection.createStatement();
  109.  
  110.         ResultSet rs = s.executeQuery(query);
  111.         while (rs.next()) {
  112.             Long vrstaPostupkaID = rs.getLong("VrstaPostupkaID");
  113.             String naziv = rs.getString("Naziv");
  114.            
  115.             VrstaPostupka vrsta = new VrstaPostupka(vrstaPostupkaID, naziv);
  116.             vrste.add(vrsta);
  117.         }
  118.         rs.close();
  119.         s.close();
  120.         return vrste;
  121.     }
  122.    
  123. }
  124. //Constants
  125.  
  126. public class Constants {
  127.     public static final String URL = "url";
  128. }
  129.  
  130. //GUI
  131.  
  132. //Controller
  133. public class Controller {
  134.    
  135.     public static List<Klijent> getAllClients() throws Exception{
  136.         DatabaseRepository dbr = new DatabaseRepository();
  137.         return dbr.findAllKlijent();
  138.     }
  139.  
  140.     public static List<VrstaPostupka> getAllVrstaPostupka() throws Exception{
  141.         DatabaseRepository dbr = new DatabaseRepository();
  142.         return dbr.findAllVrstaPostupka();
  143.     }
  144.    
  145.     public static Predmet savePredmet(Predmet predmet) throws Exception{
  146.         DatabaseRepository dbr = new DatabaseRepository();
  147.         return dbr.savePredmet(predmet);
  148.     }
  149.    
  150. }
  151.  
  152. //Table - Model
  153. public class PredmetTableModel extends AbstractTableModel{
  154.  
  155.     private final List<Predmet> predmeti;
  156.     private String[] columnNames = new String[]{"Advokat","Klijent","Datum","Naziv predmeta"};
  157.  
  158.     public PredmetTableModel(List<Predmet> predmeti) {
  159.         this.predmeti = predmeti;
  160.     }
  161.    
  162.     @Override
  163.     public int getRowCount() {
  164.         if(predmeti == null || predmeti.size() == 0){
  165.             return 0;
  166.         }else{
  167.             return predmeti.size();
  168.         }
  169.     }
  170.  
  171.     @Override
  172.     public int getColumnCount() {
  173.         return columnNames.length;
  174.     }
  175.  
  176.     @Override
  177.     public Object getValueAt(int rowIndex, int columnIndex) {
  178.         Predmet predmet = predmeti.get(rowIndex);
  179.        
  180.         switch (columnIndex){
  181.             case 0:
  182.                 return predmet.getAdvokat().getIme() + " " + predmet.getAdvokat().getPrezime();
  183.             case 1:
  184.                 return predmet.getKlijent().getIme() + " " + predmet.getKlijent().getPrezime();
  185.             case 2:
  186.                 return predmet.getDatum();
  187.             case 3:
  188.                 return predmet.getNaziv();
  189.             default:
  190.                 return "N/A";
  191.                
  192.         }
  193.     }
  194.  
  195.     @Override
  196.     public String getColumnName(int column) {
  197.         return columnNames[column];
  198.     }
  199.    
  200.    
  201. }
  202.  
  203. //Forma
  204.  
  205. //Login
  206.    private void jButtonPrijavaActionPerformed(java.awt.event.ActionEvent evt) {                                              
  207.        String username = jTextFieldUsername.getText();
  208.        String password = jTextFieldPassword.getText();
  209.        
  210.         Advokat a = new Advokat(username, password);
  211.        
  212.         for (Advokat advokat : advokati) {
  213.             if(advokat.getUsername().equals(a.getUsername()) && advokat.getPassword().equals(a.getPassword())){
  214.                 FPredmet forma = new FPredmet(advokat);  
  215.                 forma.setVisible(true);
  216.                 this.dispose();
  217.                 return;
  218.             }
  219.         }
  220.         JOptionPane.showMessageDialog(this, "Pogresno uneto korisnicko ime ili lozinka!");
  221.     }
  222.  
  223. //populate table
  224. private void populateTablePredmeti() {
  225.         try {
  226.             List<Predmet> predmeti = this.predmeti;
  227.             TableModel tm = new PredmetTableModel(predmeti);
  228.             jTable1.setModel(tm);
  229.         } catch (Exception e) {
  230.             Logger.getLogger(FPredmet.class.getName()).log(Level.SEVERE, null, e);
  231.         }
  232.     }
  233.  
  234. //brisanje iz tabele
  235. private void jButtonObrisiActionPerformed(java.awt.event.ActionEvent evt) {                                              
  236.         int row = jTable1.getSelectedRow();
  237.        
  238.         if(row >= 0){
  239.             predmeti.remove(row);
  240.             populateTablePredmeti();
  241.         }else{
  242.             JOptionPane.showMessageDialog(null, "Molimo vas izaberite red!");
  243.         }
  244.     }
  245.  
  246. //populate combobox
  247. private void populateComboBox() {
  248.         try {
  249.             jComboBoxKlijent.removeAllItems();
  250.             jComboBoxVrstaPostupka.removeAllItems();
  251.            
  252.             List<Klijent> klijenti = Controller.getAllClients();
  253.             List<VrstaPostupka> vrste = Controller.getAllVrstaPostupka();
  254.  
  255.             for (VrstaPostupka vrstaPostupka : vrste) {
  256.                 jComboBoxVrstaPostupka.addItem(vrstaPostupka);
  257.             }
  258.            
  259.             for (Klijent klijent : klijenti) {
  260.                 jComboBoxKlijent.addItem(klijent);
  261.             }
  262.            
  263.         } catch (Exception ex) {
  264.             Logger.getLogger(FPredmet.class.getName()).log(Level.SEVERE, null, ex);
  265.         }
  266.     }
  267.  
  268. //center form
  269.  private void centerForm() {
  270.         setLocationRelativeTo(null);
  271.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement