Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //DB
- //DatabaseConnection
- public class DatabaseConnection {
- private final Connection connection;
- private static DatabaseConnection instance;
- public DatabaseConnection() throws Exception {
- DatabaseRecources dbr = new DatabaseRecources();
- connection = DriverManager.getConnection(dbr.getUrl(), dbr.getUsername(), dbr.getPassword());
- System.out.println("Uspostavljena konekcija");
- }
- public Connection getConnection() {
- return connection;
- }
- public static DatabaseConnection getInstance() throws Exception{
- if(instance == null){
- instance = new DatabaseConnection();
- }
- return instance;
- }
- }
- //DatabaseRecources
- public class DatabaseRecources {
- Properties properties;
- public DatabaseRecources() throws Exception {
- properties = new Properties();
- FileInputStream fis = new FileInputStream("C:\\Users\\Ana Milekic\\Documents\\NetBeansProjects\\PS-Kolokvijum\\resources\\database.config");
- properties.load(fis);
- }
- public String getUrl(){
- return properties.getProperty(Constants.URL);
- }
- public String getUsername(){
- return properties.getProperty("username");
- }
- public String getPassword(){
- return properties.getProperty("password");
- }
- }
- //DatabaseRepository
- public class DatabaseRepository {
- public Predmet savePredmet(Predmet predmet) throws Exception {
- Connection connection = DatabaseConnection.getInstance().getConnection();
- connection.setAutoCommit(false);
- String queryBill = "INSERT INTO predmet(Naziv,Problem,Datum,Advokat,KlijentID,VrstaPostupkaId) values(?,?,?,?,?,?)";
- PreparedStatement ps = connection.prepareStatement(queryBill,Statement.RETURN_GENERATED_KEYS);
- ps.setString(1, predmet.getNaziv());
- ps.setString(2, predmet.getProblem());
- ps.setDate(3, Date.valueOf(LocalDate.now()));
- ps.setString(4, predmet.getAdvokat().getIme());
- ps.setLong(5, predmet.getKlijent().getKlijentID());
- ps.setLong(6, predmet.getVrstaPostupka().getVrstaPostupkaID());
- System.out.println(ps);
- ps.executeUpdate();
- ResultSet rs = ps.getGeneratedKeys();
- if (rs.next()) {
- Long predmetId = rs.getLong(1);
- System.out.println(predmetId);
- connection.commit();
- ps.close();
- }
- return predmet;
- }
- public List<Klijent> findAllKlijent() throws Exception {
- List<Klijent> clients = new ArrayList<>();
- Connection connection = DatabaseConnection.getInstance().getConnection();
- String query = "SELECT * FROM klijent ORDER BY Prezime ASC";
- Statement s = connection.createStatement();
- ResultSet rs = s.executeQuery(query);
- while (rs.next()) {
- Long klijentID = rs.getLong("KlijentID");
- String ime = rs.getString("Ime");
- String prezime = rs.getString("Prezime");
- String telefon = rs.getString("Telefon");
- String elPosta = rs.getString("ElPosta");
- String adresa = rs.getString("Adresa");
- Klijent client = new Klijent(klijentID, ime, prezime, telefon, elPosta, adresa);
- clients.add(client);
- }
- rs.close();
- s.close();
- return clients;
- }
- public List<VrstaPostupka> findAllVrstaPostupka() throws Exception {
- List<VrstaPostupka> vrste = new ArrayList<>();
- Connection connection = DatabaseConnection.getInstance().getConnection();
- String query = "SELECT * FROM vrstapostupka ORDER BY naziv DESC";
- Statement s = connection.createStatement();
- ResultSet rs = s.executeQuery(query);
- while (rs.next()) {
- Long vrstaPostupkaID = rs.getLong("VrstaPostupkaID");
- String naziv = rs.getString("Naziv");
- VrstaPostupka vrsta = new VrstaPostupka(vrstaPostupkaID, naziv);
- vrste.add(vrsta);
- }
- rs.close();
- s.close();
- return vrste;
- }
- }
- //Constants
- public class Constants {
- public static final String URL = "url";
- }
- //GUI
- //Controller
- public class Controller {
- public static List<Klijent> getAllClients() throws Exception{
- DatabaseRepository dbr = new DatabaseRepository();
- return dbr.findAllKlijent();
- }
- public static List<VrstaPostupka> getAllVrstaPostupka() throws Exception{
- DatabaseRepository dbr = new DatabaseRepository();
- return dbr.findAllVrstaPostupka();
- }
- public static Predmet savePredmet(Predmet predmet) throws Exception{
- DatabaseRepository dbr = new DatabaseRepository();
- return dbr.savePredmet(predmet);
- }
- }
- //Table - Model
- public class PredmetTableModel extends AbstractTableModel{
- private final List<Predmet> predmeti;
- private String[] columnNames = new String[]{"Advokat","Klijent","Datum","Naziv predmeta"};
- public PredmetTableModel(List<Predmet> predmeti) {
- this.predmeti = predmeti;
- }
- @Override
- public int getRowCount() {
- if(predmeti == null || predmeti.size() == 0){
- return 0;
- }else{
- return predmeti.size();
- }
- }
- @Override
- public int getColumnCount() {
- return columnNames.length;
- }
- @Override
- public Object getValueAt(int rowIndex, int columnIndex) {
- Predmet predmet = predmeti.get(rowIndex);
- switch (columnIndex){
- case 0:
- return predmet.getAdvokat().getIme() + " " + predmet.getAdvokat().getPrezime();
- case 1:
- return predmet.getKlijent().getIme() + " " + predmet.getKlijent().getPrezime();
- case 2:
- return predmet.getDatum();
- case 3:
- return predmet.getNaziv();
- default:
- return "N/A";
- }
- }
- @Override
- public String getColumnName(int column) {
- return columnNames[column];
- }
- }
- //Forma
- //Login
- private void jButtonPrijavaActionPerformed(java.awt.event.ActionEvent evt) {
- String username = jTextFieldUsername.getText();
- String password = jTextFieldPassword.getText();
- Advokat a = new Advokat(username, password);
- for (Advokat advokat : advokati) {
- if(advokat.getUsername().equals(a.getUsername()) && advokat.getPassword().equals(a.getPassword())){
- FPredmet forma = new FPredmet(advokat);
- forma.setVisible(true);
- this.dispose();
- return;
- }
- }
- JOptionPane.showMessageDialog(this, "Pogresno uneto korisnicko ime ili lozinka!");
- }
- //populate table
- private void populateTablePredmeti() {
- try {
- List<Predmet> predmeti = this.predmeti;
- TableModel tm = new PredmetTableModel(predmeti);
- jTable1.setModel(tm);
- } catch (Exception e) {
- Logger.getLogger(FPredmet.class.getName()).log(Level.SEVERE, null, e);
- }
- }
- //brisanje iz tabele
- private void jButtonObrisiActionPerformed(java.awt.event.ActionEvent evt) {
- int row = jTable1.getSelectedRow();
- if(row >= 0){
- predmeti.remove(row);
- populateTablePredmeti();
- }else{
- JOptionPane.showMessageDialog(null, "Molimo vas izaberite red!");
- }
- }
- //populate combobox
- private void populateComboBox() {
- try {
- jComboBoxKlijent.removeAllItems();
- jComboBoxVrstaPostupka.removeAllItems();
- List<Klijent> klijenti = Controller.getAllClients();
- List<VrstaPostupka> vrste = Controller.getAllVrstaPostupka();
- for (VrstaPostupka vrstaPostupka : vrste) {
- jComboBoxVrstaPostupka.addItem(vrstaPostupka);
- }
- for (Klijent klijent : klijenti) {
- jComboBoxKlijent.addItem(klijent);
- }
- } catch (Exception ex) {
- Logger.getLogger(FPredmet.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- //center form
- private void centerForm() {
- setLocationRelativeTo(null);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement