Employee management system 2

This is a bit enhanced version of employee management system project that I've done before. The GUI is generally the same, except that the data is now displayed inside tables and some colors are changed.
Important thing is that ORM is done with Hibernate and HQL. 
The code is shown for just two frames (PrikazFrame and IzmenaFrame) out of 5.


hibernate.cfg.xml

1:  <?xml version="1.0" encoding="UTF-8"?>  
2:  <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  
3:    
4:  <hibernate-configuration>  
5:   <session-factory>  
6:    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>  
7:    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>  
8:    <property name="hibernate.connection.url">jdbc:mysql://localhost/zaposleniDB</property>  
9:    <property name="hibernate.connection.username">root</property>  
10:    <property name="hibernate.connection.password">*****</property>  
11:    <property name="hibernate.show_sql">true</property>  
12:    <mapping resource="zaposleni/entity/Zaposleni.hbm.xml"/>  
13:   </session-factory>  
14:  </hibernate-configuration>  
15:    

zaposleni.util.NewHibernateUtil.java

1:  package zaposleni.util;  
2:    
3:  import org.hibernate.cfg.AnnotationConfiguration;  
4:  import org.hibernate.SessionFactory;  
5:  import org.hibernate.cfg.Configuration;  
6:    
7:  /**  
8:   * Hibernate Utility class with a convenient method to get Session Factory  
9:   * object.  
10:   *  
11:   * @author djordje  
12:   */  
13:  public class NewHibernateUtil {  
14:    
15:    private static final SessionFactory sessionFactory = buildSessionFactory();  
16:     
17:    private static SessionFactory buildSessionFactory() {  
18:      try {  
19:        return new Configuration().configure().buildSessionFactory();  
20:      } catch (Throwable ex) {  
21:        System.err.println("Initial SessionFactory creation failed." + ex);  
22:        throw new ExceptionInInitializerError(ex);  
23:      }  
24:    }  
25:     
26:    public static SessionFactory getSessionFactory() {  
27:      return sessionFactory;  
28:    }  
29:      
30:  }  
31:    

zaposleni.entity.Zaposleni.hbm.xml

1:  <?xml version="1.0" encoding="UTF-8"?>  
2:  <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.org/dtd/hibernate-mapping-3.0.dtd">  
3:    
4:  <hibernate-mapping>  
5:   <class name="zaposleni.entity.Zaposleni" table="zaposleni">  
6:     <id name="zaposleni_id" type="int" column="zaposleni_id">  
7:       <generator class="increment" />  
8:     </id>  
9:     <property name="ime" column="ime" type="string" />  
10:     <property name="godine" column="godine" type="int" />  
11:     <property name="adresa" column="adresa" type="string" />  
12:     <property name="dohodak" column="dohodak" type="int" />  
13:   </class>  
14:  </hibernate-mapping>  
15:    

zaposleni.entity.Zaposleni.java

1:  package zaposleni.entity;  
2:    
3:  import java.io.Serializable;  
4:    
5:  public class Zaposleni implements Serializable{  
6:      
7:    private int zaposleni_id;  
8:    private String ime;  
9:    private int godine;  
10:    private String adresa;  
11:    private int dohodak;  
12:    
13:    public int getZaposleni_id() {  
14:      return zaposleni_id;  
15:    }  
16:    public String getIme() {  
17:      return ime;  
18:    }  
19:    public int getGodine() {  
20:      return godine;  
21:    }  
22:    public String getAdresa() {  
23:      return adresa;  
24:    }  
25:    public int getDohodak() {  
26:      return dohodak;  
27:    }  
28:    public void setZaposleni_id(int zaposleni_id) {  
29:      this.zaposleni_id = zaposleni_id;  
30:    }  
31:    public void setIme(String ime) {  
32:      this.ime = ime;  
33:    }  
34:    public void setGodine(int godine) {  
35:      this.godine = godine;  
36:    }  
37:    public void setAdresa(String adresa) {  
38:      this.adresa = adresa;  
39:    }  
40:    public void setDohodak(int dohodak) {  
41:      this.dohodak = dohodak;  
42:    }  
43:    
44:    public Zaposleni(String ime, int godine, String adresa, int dohodak) {  
45:      this.ime = ime;  
46:      this.godine = godine;  
47:      this.adresa = adresa;  
48:      this.dohodak = dohodak;  
49:    }  
50:    public Zaposleni() {}  
51:      
52:      
53:      
54:  }  
55:    

zaposlenisyshib.Main.java

1:  /*  
2:   * Zaposleni Sistem / Hibernate  
3:   */  
4:  package zaposlenisyshib;  
5:    
6:  import java.awt.Color;  
7:  import javax.swing.UIManager;  
8:    
9:  /**  
10:   *  
11:   * @author Djordje Gavrilovic  
12:   */  
13:  public class Main {  
14:    public static void main(String[] args) {  
15:        
16:      try {   
17:        UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");   
18:      } catch(Exception e){ }  
19:        
20:      zaposleni.ui.InitialFrame frame = new zaposleni.ui.InitialFrame();  
21:      frame.setSize(650,400);  
22:      frame.getContentPane().setBackground(Color.decode("#8593ae"));  
23:      frame.setLocationRelativeTo(null);  
24:      frame.setVisible(true);  
25:        
26:    }  
27:      
28:  }  
29:    

zaposleni.ui.PrikazFrame.java

1:  package zaposleni.ui;  
2:    
3:  import java.awt.Color;  
4:  import java.util.List;  
5:  import java.util.Vector;  
6:  import javax.swing.table.DefaultTableModel;  
7:  import org.hibernate.HibernateException;  
8:  import org.hibernate.Query;  
9:  import org.hibernate.Session;  
10:  import zaposleni.entity.Zaposleni;  
11:  import zaposleni.util.NewHibernateUtil;  
12:    
13:  /**  
14:   *  
15:   * @author Djordje Gavrilovic  
16:   */  
17:  public class PrikazFrame extends javax.swing.JFrame {  
18:    
19:    /**  
20:     * Creates new form PrikazFrame  
21:     */  
22:    public PrikazFrame() {  
23:      initComponents();  
24:    }  
25:    
26:    /**  
27:     * This method is called from within the constructor to initialize the form.  
28:     * WARNING: Do NOT modify this code. The content of this method is always  
29:     * regenerated by the Form Editor.  
30:     */  
31:    @SuppressWarnings("unchecked")  
32:    // <editor-fold defaultstate="collapsed" desc="Generated Code">               
33:    private void initComponents() {  
34:    
35:      jScrollPane1 = new javax.swing.JScrollPane();  
36:      tabela = new javax.swing.JTable();  
37:      povratakBtn = new javax.swing.JButton();  
38:      jLabel1 = new javax.swing.JLabel();  
39:      jLabel2 = new javax.swing.JLabel();  
40:      jLabel3 = new javax.swing.JLabel();  
41:      jLabel4 = new javax.swing.JLabel();  
42:      imeTf = new javax.swing.JTextField();  
43:      godineTf = new javax.swing.JTextField();  
44:      adresaTf = new javax.swing.JTextField();  
45:      dohodakTf = new javax.swing.JTextField();  
46:      imeBtn = new javax.swing.JButton();  
47:      godineBtn = new javax.swing.JButton();  
48:      adresaBtn = new javax.swing.JButton();  
49:      dohodakBtn = new javax.swing.JButton();  
50:      jButton1 = new javax.swing.JButton();  
51:    
52:      setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);  
53:      setTitle("Prikaz zaposlenih");  
54:      addWindowListener(new java.awt.event.WindowAdapter() {  
55:        public void windowOpened(java.awt.event.WindowEvent evt) {  
56:          formWindowOpened(evt);  
57:        }  
58:      });  
59:    
60:      jScrollPane1.setBackground(new java.awt.Color(0, 59, 70));  
61:      jScrollPane1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "  Zaposleni:", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Dialog", 0, 10), new java.awt.Color(196, 223, 230))); // NOI18N  
62:    
63:      tabela.setForeground(new java.awt.Color(90, 78, 77));  
64:      tabela.setModel(new javax.swing.table.DefaultTableModel(  
65:        new Object [][] {  
66:          {null, null, null, null},  
67:          {null, null, null, null},  
68:          {null, null, null, null},  
69:          {null, null, null, null}  
70:        },  
71:        new String [] {  
72:          "Title 1", "Title 2", "Title 3", "Title 4"  
73:        }  
74:      ));  
75:      jScrollPane1.setViewportView(tabela);  
76:      if (tabela.getColumnModel().getColumnCount() > 0) {  
77:        tabela.getColumnModel().getColumn(0).setPreferredWidth(50);  
78:        tabela.getColumnModel().getColumn(0).setMaxWidth(70);  
79:      }  
80:    
81:      povratakBtn.setBackground(new java.awt.Color(126, 103, 94));  
82:      povratakBtn.setForeground(java.awt.Color.white);  
83:      povratakBtn.setText("Povratak u Meni");  
84:      povratakBtn.addActionListener(new java.awt.event.ActionListener() {  
85:        public void actionPerformed(java.awt.event.ActionEvent evt) {  
86:          povratakBtnActionPerformed(evt);  
87:        }  
88:      });  
89:    
90:      jLabel1.setForeground(java.awt.Color.white);  
91:      jLabel1.setText("Ime i prezime");  
92:    
93:      jLabel2.setForeground(java.awt.Color.white);  
94:      jLabel2.setText("Godine");  
95:    
96:      jLabel3.setForeground(java.awt.Color.white);  
97:      jLabel3.setText("Adresa");  
98:    
99:      jLabel4.setForeground(java.awt.Color.white);  
100:      jLabel4.setText("Dohodak");  
101:    
102:      imeBtn.setBackground(new java.awt.Color(126, 103, 94));  
103:      imeBtn.setForeground(java.awt.Color.white);  
104:      imeBtn.setText("Prikaži");  
105:      imeBtn.addActionListener(new java.awt.event.ActionListener() {  
106:        public void actionPerformed(java.awt.event.ActionEvent evt) {  
107:          imeBtnActionPerformed(evt);  
108:        }  
109:      });  
110:    
111:      godineBtn.setBackground(new java.awt.Color(126, 103, 94));  
112:      godineBtn.setForeground(java.awt.Color.white);  
113:      godineBtn.setText("Prikaži");  
114:      godineBtn.addActionListener(new java.awt.event.ActionListener() {  
115:        public void actionPerformed(java.awt.event.ActionEvent evt) {  
116:          godineBtnActionPerformed(evt);  
117:        }  
118:      });  
119:    
120:      adresaBtn.setBackground(new java.awt.Color(126, 103, 94));  
121:      adresaBtn.setForeground(java.awt.Color.white);  
122:      adresaBtn.setText("Prikaži");  
123:      adresaBtn.addActionListener(new java.awt.event.ActionListener() {  
124:        public void actionPerformed(java.awt.event.ActionEvent evt) {  
125:          adresaBtnActionPerformed(evt);  
126:        }  
127:      });  
128:    
129:      dohodakBtn.setBackground(new java.awt.Color(126, 103, 94));  
130:      dohodakBtn.setForeground(java.awt.Color.white);  
131:      dohodakBtn.setText("Prikaži");  
132:      dohodakBtn.addActionListener(new java.awt.event.ActionListener() {  
133:        public void actionPerformed(java.awt.event.ActionEvent evt) {  
134:          dohodakBtnActionPerformed(evt);  
135:        }  
136:      });  
137:    
138:      jButton1.setBackground(new java.awt.Color(126, 103, 94));  
139:      jButton1.setForeground(java.awt.Color.white);  
140:      jButton1.setText("Svi zaposleni");  
141:      jButton1.addActionListener(new java.awt.event.ActionListener() {  
142:        public void actionPerformed(java.awt.event.ActionEvent evt) {  
143:          jButton1ActionPerformed(evt);  
144:        }  
145:      });  
146:    
147:      javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());  
148:      getContentPane().setLayout(layout);  
149:      layout.setHorizontalGroup(  
150:        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)  
151:        .addGroup(layout.createSequentialGroup()  
152:          .addContainerGap()  
153:          .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 876, Short.MAX_VALUE)  
154:          .addContainerGap())  
155:        .addGroup(layout.createSequentialGroup()  
156:          .addGap(33, 33, 33)  
157:          .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)  
158:            .addGroup(layout.createSequentialGroup()  
159:              .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)  
160:                .addComponent(jLabel1)  
161:                .addComponent(jLabel2)  
162:                .addComponent(jLabel3)  
163:                .addComponent(jLabel4))  
164:              .addGap(45, 45, 45)  
165:              .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)  
166:                .addComponent(imeTf)  
167:                .addComponent(godineTf)  
168:                .addComponent(adresaTf)  
169:                .addComponent(dohodakTf, javax.swing.GroupLayout.DEFAULT_SIZE, 280, Short.MAX_VALUE))  
170:              .addGap(30, 30, 30)  
171:              .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)  
172:                .addComponent(imeBtn)  
173:                .addComponent(godineBtn)  
174:                .addComponent(adresaBtn)  
175:                .addComponent(dohodakBtn))  
176:              .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))  
177:            .addGroup(layout.createSequentialGroup()  
178:              .addComponent(jButton1)  
179:              .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)  
180:              .addComponent(povratakBtn)  
181:              .addGap(20, 20, 20))))  
182:      );  
183:      layout.setVerticalGroup(  
184:        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)  
185:        .addGroup(layout.createSequentialGroup()  
186:          .addGap(23, 23, 23)  
187:          .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)  
188:          .addGap(28, 28, 28)  
189:          .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)  
190:            .addComponent(jLabel1)  
191:            .addComponent(imeTf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)  
192:            .addComponent(imeBtn))  
193:          .addGap(16, 16, 16)  
194:          .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)  
195:            .addComponent(jLabel2)  
196:            .addComponent(godineTf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)  
197:            .addComponent(godineBtn))  
198:          .addGap(16, 16, 16)  
199:          .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)  
200:            .addComponent(jLabel3)  
201:            .addComponent(adresaTf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)  
202:            .addComponent(adresaBtn))  
203:          .addGap(16, 16, 16)  
204:          .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)  
205:            .addComponent(jLabel4)  
206:            .addComponent(dohodakTf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)  
207:            .addComponent(dohodakBtn))  
208:          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 101, Short.MAX_VALUE)  
209:          .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)  
210:            .addComponent(povratakBtn)  
211:            .addComponent(jButton1))  
212:          .addGap(20, 20, 20))  
213:      );  
214:    
215:      pack();  
216:    }// </editor-fold>              
217:    
218:    // HQL  
219:      
220:    private static String upitNaOsnovuImena = "from Zaposleni z where z.ime like '";  
221:    private static String upitNaOsnovuGodina = "from Zaposleni z where z.godine like '";  
222:    private static String upitNaOsnovuAdrese = "from Zaposleni z where z.adresa like '";  
223:    private static String upitNaOsnovuDohotka = "from Zaposleni z where z.dohodak like '";  
224:    private static String prikazSvihZaposlenih = "from Zaposleni";  
225:      
226:    private void izvrsiHQL(String hql) {  
227:      try{  
228:        Session s = NewHibernateUtil.getSessionFactory().openSession();  
229:        s.beginTransaction();  
230:        Query q = s.createQuery(hql);  
231:        List listaRezultata = q.list();  
232:        prikaziRezultat(listaRezultata);  
233:        s.getTransaction().commit();  
234:          
235:      } catch (HibernateException e) {System.out.println("GRESKA! :" + e.getMessage());}  
236:    }  
237:      
238:    private void prikaziRezultat(List listaRezultata) {  
239:      Vector<String> nasloviKolona = new Vector<>();  
240:      Vector podaci = new Vector();  
241:        
242:      nasloviKolona.add("ID");  
243:      nasloviKolona.add("Ime i Prezime");  
244:      nasloviKolona.add("Godine");  
245:      nasloviKolona.add("Adresa");  
246:      nasloviKolona.add("Dohodak");  
247:        
248:      for(Object o : listaRezultata) {  
249:        Zaposleni zaposleni = (Zaposleni)o;  
250:        Vector<Object> red = new Vector<>();  
251:        red.add(zaposleni.getZaposleni_id());  
252:        red.add(zaposleni.getIme());  
253:        red.add(zaposleni.getGodine());  
254:        red.add(zaposleni.getAdresa());  
255:        red.add(zaposleni.getDohodak());  
256:        podaci.add(red);  
257:      }  
258:        
259:      tabela.setModel(new DefaultTableModel(podaci,nasloviKolona));  
260:    }  
261:      
262:    private void povratakBtnActionPerformed(java.awt.event.ActionEvent evt) {                        
263:      InitialFrame frame = new InitialFrame();  
264:      frame.setSize(650,400);  
265:      frame.getContentPane().setBackground(Color.decode("#8593ae"));  
266:      frame.setLocationRelativeTo(null);  
267:      frame.setVisible(true);  
268:        
269:      this.dispose();  
270:    }                        
271:    
272:    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                       
273:      izvrsiHQL(prikazSvihZaposlenih);  
274:    }                      
275:    
276:    private void formWindowOpened(java.awt.event.WindowEvent evt) {                   
277:      izvrsiHQL(prikazSvihZaposlenih);  
278:    }                   
279:    
280:    private void imeBtnActionPerformed(java.awt.event.ActionEvent evt) {                      
281:      izvrsiHQL(  
282:      upitNaOsnovuImena + imeTf.getText() + "%'"  
283:      );  
284:      imeTf.setText("");  
285:    }                     
286:    
287:    private void godineBtnActionPerformed(java.awt.event.ActionEvent evt) {                       
288:      izvrsiHQL(  
289:      upitNaOsnovuGodina + godineTf.getText() + "%'"  
290:      );  
291:      godineTf.setText("");  
292:    }                       
293:    
294:    private void adresaBtnActionPerformed(java.awt.event.ActionEvent evt) {                       
295:      izvrsiHQL(  
296:      upitNaOsnovuAdrese + adresaTf.getText() + "%'"  
297:      );  
298:      adresaTf.setText("");  
299:    }                       
300:    
301:    private void dohodakBtnActionPerformed(java.awt.event.ActionEvent evt) {                        
302:      izvrsiHQL(  
303:      upitNaOsnovuDohotka + dohodakTf.getText() + "%'"  
304:      );  
305:      dohodakTf.setText("");  
306:    }                       
307:    
308:    /**  
309:     * @param args the command line arguments  
310:     */  
311:    public static void main(String args[]) {  
312:      /* Set the Nimbus look and feel */  
313:      //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">  
314:      /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.  
315:       * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html   
316:       */  
317:      try {  
318:        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {  
319:          if ("Nimbus".equals(info.getName())) {  
320:            javax.swing.UIManager.setLookAndFeel(info.getClassName());  
321:            break;  
322:          }  
323:        }  
324:      } catch (ClassNotFoundException ex) {  
325:        java.util.logging.Logger.getLogger(PrikazFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);  
326:      } catch (InstantiationException ex) {  
327:        java.util.logging.Logger.getLogger(PrikazFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);  
328:      } catch (IllegalAccessException ex) {  
329:        java.util.logging.Logger.getLogger(PrikazFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);  
330:      } catch (javax.swing.UnsupportedLookAndFeelException ex) {  
331:        java.util.logging.Logger.getLogger(PrikazFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);  
332:      }  
333:      //</editor-fold>  
334:    
335:      /* Create and display the form */  
336:      java.awt.EventQueue.invokeLater(new Runnable() {  
337:        public void run() {  
338:          new PrikazFrame().setVisible(true);  
339:        }  
340:      });  
341:    }  
342:    
343:    // Variables declaration - do not modify             
344:    private javax.swing.JButton adresaBtn;  
345:    private javax.swing.JTextField adresaTf;  
346:    private javax.swing.JButton dohodakBtn;  
347:    private javax.swing.JTextField dohodakTf;  
348:    private javax.swing.JButton godineBtn;  
349:    private javax.swing.JTextField godineTf;  
350:    private javax.swing.JButton imeBtn;  
351:    private javax.swing.JTextField imeTf;  
352:    private javax.swing.JButton jButton1;  
353:    private javax.swing.JLabel jLabel1;  
354:    private javax.swing.JLabel jLabel2;  
355:    private javax.swing.JLabel jLabel3;  
356:    private javax.swing.JLabel jLabel4;  
357:    private javax.swing.JScrollPane jScrollPane1;  
358:    private javax.swing.JButton povratakBtn;  
359:    private javax.swing.JTable tabela;  
360:    // End of variables declaration            
361:  }  
362:    

zaposleni.ui.IzmenaFrame.java

1:  package zaposleni.ui;  
2:    
3:  import java.awt.Color;  
4:  import java.util.List;  
5:  import java.util.Vector;  
6:  import javax.swing.table.DefaultTableModel;  
7:  import org.hibernate.HibernateException;  
8:  import org.hibernate.Query;  
9:  import org.hibernate.Session;  
10:  import zaposleni.entity.Zaposleni;  
11:  import zaposleni.util.NewHibernateUtil;  
12:    
13:  /**  
14:   *  
15:   * @author djordje  
16:   */  
17:  public class IzmenaFrame extends javax.swing.JFrame {  
18:    
19:    /**  
20:     * Creates new form IzmenaFrame  
21:     */  
22:    public IzmenaFrame() {  
23:      initComponents();  
24:    }  
25:    
26:    /**  
27:     * This method is called from within the constructor to initialize the form.  
28:     * WARNING: Do NOT modify this code. The content of this method is always  
29:     * regenerated by the Form Editor.  
30:     */  
31:    @SuppressWarnings("unchecked")  
32:    // <editor-fold defaultstate="collapsed" desc="Generated Code">               
33:    private void initComponents() {  
34:    
35:      jScrollPane1 = new javax.swing.JScrollPane();  
36:      tabela = new javax.swing.JTable();  
37:      povratakBtn = new javax.swing.JButton();  
38:      jLabel1 = new javax.swing.JLabel();  
39:      idTf = new javax.swing.JTextField();  
40:      jLabel2 = new javax.swing.JLabel();  
41:      jLabel3 = new javax.swing.JLabel();  
42:      jLabel4 = new javax.swing.JLabel();  
43:      jLabel5 = new javax.swing.JLabel();  
44:      imeTf = new javax.swing.JTextField();  
45:      godineTf = new javax.swing.JTextField();  
46:      adresaTf = new javax.swing.JTextField();  
47:      dohodakTf = new javax.swing.JTextField();  
48:      imeBtn = new javax.swing.JButton();  
49:      godineBtn = new javax.swing.JButton();  
50:      adresaBtn = new javax.swing.JButton();  
51:      dohodakBtn = new javax.swing.JButton();  
52:      prikazBtn = new javax.swing.JButton();  
53:    
54:      setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);  
55:      setTitle("Izmena podataka zaposlenih");  
56:      addWindowListener(new java.awt.event.WindowAdapter() {  
57:        public void windowOpened(java.awt.event.WindowEvent evt) {  
58:          formWindowOpened(evt);  
59:        }  
60:      });  
61:    
62:      jScrollPane1.setBackground(new java.awt.Color(0, 59, 70));  
63:      jScrollPane1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "  Zaposleni:", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Dialog", 0, 10), new java.awt.Color(196, 223, 230))); // NOI18N  
64:    
65:      tabela.setBorder(null);  
66:      tabela.setModel(new javax.swing.table.DefaultTableModel(  
67:        new Object [][] {  
68:          {null, null, null, null},  
69:          {null, null, null, null},  
70:          {null, null, null, null},  
71:          {null, null, null, null}  
72:        },  
73:        new String [] {  
74:          "Title 1", "Title 2", "Title 3", "Title 4"  
75:        }  
76:      ));  
77:      jScrollPane1.setViewportView(tabela);  
78:    
79:      povratakBtn.setBackground(new java.awt.Color(126, 103, 94));  
80:      povratakBtn.setForeground(java.awt.Color.white);  
81:      povratakBtn.setText("Povratak u Meni");  
82:      povratakBtn.addActionListener(new java.awt.event.ActionListener() {  
83:        public void actionPerformed(java.awt.event.ActionEvent evt) {  
84:          povratakBtnActionPerformed(evt);  
85:        }  
86:      });  
87:    
88:      jLabel1.setForeground(java.awt.Color.white);  
89:      jLabel1.setText("ID zaposlenog");  
90:    
91:      jLabel2.setForeground(java.awt.Color.white);  
92:      jLabel2.setText("Ime i prezime");  
93:    
94:      jLabel3.setForeground(java.awt.Color.white);  
95:      jLabel3.setText("Godine");  
96:    
97:      jLabel4.setForeground(java.awt.Color.white);  
98:      jLabel4.setText("Adresa");  
99:    
100:      jLabel5.setForeground(java.awt.Color.white);  
101:      jLabel5.setText("Dohodak");  
102:    
103:      imeBtn.setBackground(new java.awt.Color(126, 103, 94));  
104:      imeBtn.setForeground(java.awt.Color.white);  
105:      imeBtn.setText("Izmeni");  
106:      imeBtn.addActionListener(new java.awt.event.ActionListener() {  
107:        public void actionPerformed(java.awt.event.ActionEvent evt) {  
108:          imeBtnActionPerformed(evt);  
109:        }  
110:      });  
111:    
112:      godineBtn.setBackground(new java.awt.Color(126, 103, 94));  
113:      godineBtn.setForeground(java.awt.Color.white);  
114:      godineBtn.setText("Izmeni");  
115:      godineBtn.addActionListener(new java.awt.event.ActionListener() {  
116:        public void actionPerformed(java.awt.event.ActionEvent evt) {  
117:          godineBtnActionPerformed(evt);  
118:        }  
119:      });  
120:    
121:      adresaBtn.setBackground(new java.awt.Color(126, 103, 94));  
122:      adresaBtn.setForeground(java.awt.Color.white);  
123:      adresaBtn.setText("Izmeni");  
124:      adresaBtn.addActionListener(new java.awt.event.ActionListener() {  
125:        public void actionPerformed(java.awt.event.ActionEvent evt) {  
126:          adresaBtnActionPerformed(evt);  
127:        }  
128:      });  
129:    
130:      dohodakBtn.setBackground(new java.awt.Color(126, 103, 94));  
131:      dohodakBtn.setForeground(java.awt.Color.white);  
132:      dohodakBtn.setText("Izmeni");  
133:      dohodakBtn.addActionListener(new java.awt.event.ActionListener() {  
134:        public void actionPerformed(java.awt.event.ActionEvent evt) {  
135:          dohodakBtnActionPerformed(evt);  
136:        }  
137:      });  
138:    
139:      prikazBtn.setBackground(new java.awt.Color(126, 103, 94));  
140:      prikazBtn.setForeground(java.awt.Color.white);  
141:      prikazBtn.setText("Prikaz svih zaposlenih");  
142:      prikazBtn.addActionListener(new java.awt.event.ActionListener() {  
143:        public void actionPerformed(java.awt.event.ActionEvent evt) {  
144:          prikazBtnActionPerformed(evt);  
145:        }  
146:      });  
147:    
148:      javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());  
149:      getContentPane().setLayout(layout);  
150:      layout.setHorizontalGroup(  
151:        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)  
152:        .addGroup(layout.createSequentialGroup()  
153:          .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)  
154:            .addGroup(layout.createSequentialGroup()  
155:              .addContainerGap()  
156:              .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 876, javax.swing.GroupLayout.PREFERRED_SIZE))  
157:            .addGroup(layout.createSequentialGroup()  
158:              .addGap(33, 33, 33)  
159:              .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)  
160:                .addGroup(layout.createSequentialGroup()  
161:                  .addComponent(jLabel1)  
162:                  .addGap(45, 45, 45)  
163:                  .addComponent(idTf, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))  
164:                .addGroup(layout.createSequentialGroup()  
165:                  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)  
166:                    .addComponent(jLabel2)  
167:                    .addComponent(jLabel3)  
168:                    .addComponent(jLabel4)  
169:                    .addComponent(jLabel5))  
170:                  .addGap(48, 48, 48)  
171:                  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)  
172:                    .addComponent(imeTf)  
173:                    .addComponent(godineTf)  
174:                    .addComponent(adresaTf)  
175:                    .addComponent(dohodakTf, javax.swing.GroupLayout.DEFAULT_SIZE, 280, Short.MAX_VALUE))  
176:                  .addGap(30, 30, 30)  
177:                  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)  
178:                    .addComponent(imeBtn)  
179:                    .addComponent(adresaBtn)  
180:                    .addComponent(dohodakBtn)  
181:                    .addComponent(godineBtn)))  
182:                .addGroup(layout.createSequentialGroup()  
183:                  .addComponent(prikazBtn)  
184:                  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)  
185:                  .addComponent(povratakBtn)  
186:                  .addGap(11, 11, 11)))))  
187:          .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))  
188:      );  
189:      layout.setVerticalGroup(  
190:        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)  
191:        .addGroup(layout.createSequentialGroup()  
192:          .addGap(23, 23, 23)  
193:          .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)  
194:          .addGap(23, 23, 23)  
195:          .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)  
196:            .addComponent(jLabel1)  
197:            .addComponent(idTf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))  
198:          .addGap(23, 23, 23)  
199:          .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)  
200:            .addComponent(jLabel2)  
201:            .addComponent(imeTf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)  
202:            .addComponent(imeBtn))  
203:          .addGap(16, 16, 16)  
204:          .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)  
205:            .addComponent(jLabel3)  
206:            .addComponent(godineTf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)  
207:            .addComponent(godineBtn))  
208:          .addGap(16, 16, 16)  
209:          .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)  
210:            .addComponent(jLabel4)  
211:            .addComponent(adresaTf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)  
212:            .addComponent(adresaBtn))  
213:          .addGap(16, 16, 16)  
214:          .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)  
215:            .addComponent(jLabel5)  
216:            .addComponent(dohodakTf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)  
217:            .addComponent(dohodakBtn))  
218:          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 56, Short.MAX_VALUE)  
219:          .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)  
220:            .addComponent(povratakBtn)  
221:            .addComponent(prikazBtn))  
222:          .addGap(20, 20, 20))  
223:      );  
224:    
225:      pack();  
226:    }// </editor-fold>              
227:    
228:    // HQL  
229:        
230:    private void prikaziRezultat(List listaRezultata) {  
231:      Vector<String> nasloviKolona = new Vector<>();  
232:      Vector podaci = new Vector();  
233:        
234:      nasloviKolona.add("ID");  
235:      nasloviKolona.add("Ime i Prezime");  
236:      nasloviKolona.add("Godine");  
237:      nasloviKolona.add("Adresa");  
238:      nasloviKolona.add("Dohodak");  
239:        
240:      for(Object o : listaRezultata) {  
241:        Zaposleni zaposleni = (Zaposleni)o;  
242:        Vector<Object> red = new Vector<>();  
243:        red.add(zaposleni.getZaposleni_id());  
244:        red.add(zaposleni.getIme());  
245:        red.add(zaposleni.getGodine());  
246:        red.add(zaposleni.getAdresa());  
247:        red.add(zaposleni.getDohodak());  
248:        podaci.add(red);  
249:      }  
250:        
251:      tabela.setModel(new DefaultTableModel(podaci,nasloviKolona));  
252:    }  
253:        
254:    private void povratakBtnActionPerformed(java.awt.event.ActionEvent evt) {                        
255:      InitialFrame frame = new InitialFrame();  
256:      frame.setSize(650,400);  
257:      frame.getContentPane().setBackground(Color.decode("#8593ae"));  
258:      frame.setLocationRelativeTo(null);  
259:      frame.setVisible(true);  
260:        
261:      this.dispose();  
262:    }                        
263:    
264:    private void imeBtnActionPerformed(java.awt.event.ActionEvent evt) {                      
265:      try{  
266:        Session s = NewHibernateUtil.getSessionFactory().openSession();  
267:        s.beginTransaction();  
268:        Query q = s.createQuery("update Zaposleni z set z.ime='"+imeTf.getText()+"' where z.zaposleni_id='" +idTf.getText()+"'");  
269:        q.executeUpdate();  
270:        Query q2 = s.createQuery("from Zaposleni z where z.zaposleni_id='" + idTf.getText()+"'");  
271:        List listaRezultata = q2.list();  
272:        prikaziRezultat(listaRezultata);  
273:        s.getTransaction().commit();  
274:          
275:      } catch (HibernateException e) {System.out.println("GRESKA! :" + e.getMessage());}  
276:        
277:        
278:      imeTf.setText("");  
279:      idTf.setText("");  
280:    }                     
281:    
282:    private void prikazBtnActionPerformed(java.awt.event.ActionEvent evt) {                       
283:      try{  
284:        Session s = NewHibernateUtil.getSessionFactory().openSession();  
285:        s.beginTransaction();  
286:        Query q = s.createQuery("from Zaposleni");  
287:        List listaRezultata = q.list();  
288:        prikaziRezultat(listaRezultata);  
289:        s.getTransaction().commit();  
290:          
291:      } catch (HibernateException e) {System.out.println("GRESKA! :" + e.getMessage());}  
292:    }                       
293:    
294:    private void formWindowOpened(java.awt.event.WindowEvent evt) {                   
295:      try{  
296:        Session s = NewHibernateUtil.getSessionFactory().openSession();  
297:        s.beginTransaction();  
298:        Query q = s.createQuery("from Zaposleni");  
299:        List listaRezultata = q.list();  
300:        prikaziRezultat(listaRezultata);  
301:        s.getTransaction().commit();  
302:          
303:      } catch (HibernateException e) {System.out.println("GRESKA! :" + e.getMessage());}  
304:      
305:    }                   
306:    
307:    private void godineBtnActionPerformed(java.awt.event.ActionEvent evt) {                       
308:      try{  
309:        Session s = NewHibernateUtil.getSessionFactory().openSession();  
310:        s.beginTransaction();  
311:        Query q = s.createQuery("update Zaposleni z set z.godine='"+godineTf.getText()+"' where z.zaposleni_id='" +idTf.getText()+"'");  
312:        q.executeUpdate();  
313:        Query q2 = s.createQuery("from Zaposleni z where z.zaposleni_id='" + idTf.getText()+"'");  
314:        List listaRezultata = q2.list();  
315:        prikaziRezultat(listaRezultata);  
316:        s.getTransaction().commit();  
317:          
318:      } catch (HibernateException e) {System.out.println("GRESKA! :" + e.getMessage());}  
319:        
320:        
321:      godineTf.setText("");  
322:      idTf.setText("");  
323:    }                       
324:    
325:    private void adresaBtnActionPerformed(java.awt.event.ActionEvent evt) {                       
326:      try{  
327:        Session s = NewHibernateUtil.getSessionFactory().openSession();  
328:        s.beginTransaction();  
329:        Query q = s.createQuery("update Zaposleni z set z.adresa='"+adresaTf.getText()+"' where z.zaposleni_id='" +idTf.getText()+"'");  
330:        q.executeUpdate();  
331:        Query q2 = s.createQuery("from Zaposleni z where z.zaposleni_id='" + idTf.getText()+"'");  
332:        List listaRezultata = q2.list();  
333:        prikaziRezultat(listaRezultata);  
334:        s.getTransaction().commit();  
335:          
336:      } catch (HibernateException e) {System.out.println("GRESKA! :" + e.getMessage());}  
337:        
338:        
339:      adresaTf.setText("");  
340:      idTf.setText("");  
341:    }                       
342:    
343:    private void dohodakBtnActionPerformed(java.awt.event.ActionEvent evt) {                        
344:      try{  
345:        Session s = NewHibernateUtil.getSessionFactory().openSession();  
346:        s.beginTransaction();  
347:        Query q = s.createQuery("update Zaposleni z set z.dohodak='"+dohodakTf.getText()+"' where z.zaposleni_id='" +idTf.getText()+"'");  
348:        q.executeUpdate();  
349:        Query q2 = s.createQuery("from Zaposleni z where z.zaposleni_id='" + idTf.getText()+"'");  
350:        List listaRezultata = q2.list();  
351:        prikaziRezultat(listaRezultata);  
352:        s.getTransaction().commit();  
353:          
354:      } catch (HibernateException e) {System.out.println("GRESKA! :" + e.getMessage());}  
355:        
356:        
357:      dohodakTf.setText("");  
358:      idTf.setText("");  
359:    }                       
360:    
361:    /**  
362:     * @param args the command line arguments  
363:     */  
364:    public static void main(String args[]) {  
365:      /* Set the Nimbus look and feel */  
366:      //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">  
367:      /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.  
368:       * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html   
369:       */  
370:      try {  
371:        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {  
372:          if ("Nimbus".equals(info.getName())) {  
373:            javax.swing.UIManager.setLookAndFeel(info.getClassName());  
374:            break;  
375:          }  
376:        }  
377:      } catch (ClassNotFoundException ex) {  
378:        java.util.logging.Logger.getLogger(IzmenaFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);  
379:      } catch (InstantiationException ex) {  
380:        java.util.logging.Logger.getLogger(IzmenaFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);  
381:      } catch (IllegalAccessException ex) {  
382:        java.util.logging.Logger.getLogger(IzmenaFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);  
383:      } catch (javax.swing.UnsupportedLookAndFeelException ex) {  
384:        java.util.logging.Logger.getLogger(IzmenaFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);  
385:      }  
386:      //</editor-fold>  
387:    
388:      /* Create and display the form */  
389:      java.awt.EventQueue.invokeLater(new Runnable() {  
390:        public void run() {  
391:          new IzmenaFrame().setVisible(true);  
392:        }  
393:      });  
394:    }  
395:    
396:    // Variables declaration - do not modify             
397:    private javax.swing.JButton adresaBtn;  
398:    private javax.swing.JTextField adresaTf;  
399:    private javax.swing.JButton dohodakBtn;  
400:    private javax.swing.JTextField dohodakTf;  
401:    private javax.swing.JButton godineBtn;  
402:    private javax.swing.JTextField godineTf;  
403:    private javax.swing.JTextField idTf;  
404:    private javax.swing.JButton imeBtn;  
405:    private javax.swing.JTextField imeTf;  
406:    private javax.swing.JLabel jLabel1;  
407:    private javax.swing.JLabel jLabel2;  
408:    private javax.swing.JLabel jLabel3;  
409:    private javax.swing.JLabel jLabel4;  
410:    private javax.swing.JLabel jLabel5;  
411:    private javax.swing.JScrollPane jScrollPane1;  
412:    private javax.swing.JButton povratakBtn;  
413:    private javax.swing.JButton prikazBtn;  
414:    private javax.swing.JTable tabela;  
415:    // End of variables declaration            
416:  }  
417:    




Comments