Untitled

 avatar
unknown
plain_text
3 months ago
7.9 kB
5
Indexable
package evidencijaRadnika_EM;

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.awt.event.ActionEvent;
import javax.swing.JComboBox;

public class Registracija {

    private JFrame frame;
    private JTextField id_radnik;
    private JTextField ime_radnik;
    private JTextField prezime_radnik;
    private JTextField god_dat_rodenja;
    private JComboBox<String> comboBox_voditelj;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Registracija window = new Registracija();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public Registracija() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 670, 469);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JLabel lblNewLabel = new JLabel("REGISTRACIJA");
        lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 20));
        lblNewLabel.setBounds(238, 34, 153, 39);
        frame.getContentPane().add(lblNewLabel);

        JLabel lblNewLabel_1 = new JLabel("ID:");
        lblNewLabel_1.setFont(new Font("Tahoma", Font.PLAIN, 14));
        lblNewLabel_1.setBounds(59, 107, 80, 22);
        frame.getContentPane().add(lblNewLabel_1);

        id_radnik = new JTextField();
        id_radnik.setFont(new Font("Tahoma", Font.PLAIN, 14));
        id_radnik.setBounds(104, 107, 179, 27);
        frame.getContentPane().add(id_radnik);
        id_radnik.setColumns(10);

        JLabel lblNewLabel_2 = new JLabel("Ime:");
        lblNewLabel_2.setFont(new Font("Tahoma", Font.PLAIN, 14));
        lblNewLabel_2.setBounds(57, 171, 46, 14);
        frame.getContentPane().add(lblNewLabel_2);

        JLabel lblNewLabel_3 = new JLabel("Prezime:");
        lblNewLabel_3.setFont(new Font("Tahoma", Font.PLAIN, 14));
        lblNewLabel_3.setBounds(343, 167, 96, 22);
        frame.getContentPane().add(lblNewLabel_3);

        JLabel lblNewLabel_4 = new JLabel("Datum i godina rođenja (2023-12-10):");
        lblNewLabel_4.setFont(new Font("Tahoma", Font.PLAIN, 14));
        lblNewLabel_4.setBounds(59, 232, 249, 22);
        frame.getContentPane().add(lblNewLabel_4);

        ime_radnik = new JTextField();
        ime_radnik.setFont(new Font("Tahoma", Font.PLAIN, 14));
        ime_radnik.setBounds(113, 167, 195, 27);
        frame.getContentPane().add(ime_radnik);
        ime_radnik.setColumns(10);

        prezime_radnik = new JTextField();
        prezime_radnik.setFont(new Font("Tahoma", Font.PLAIN, 14));
        prezime_radnik.setBounds(420, 167, 179, 27);
        frame.getContentPane().add(prezime_radnik);
        prezime_radnik.setColumns(10);

        god_dat_rodenja = new JTextField();
        god_dat_rodenja.setFont(new Font("Tahoma", Font.PLAIN, 14));
        god_dat_rodenja.setBounds(329, 230, 270, 27);
        frame.getContentPane().add(god_dat_rodenja);
        god_dat_rodenja.setColumns(10);

        JLabel lblNewLabel_5 = new JLabel("ID voditelj:");
        lblNewLabel_5.setFont(new Font("Tahoma", Font.PLAIN, 14));
        lblNewLabel_5.setBounds(59, 309, 80, 22);
        frame.getContentPane().add(lblNewLabel_5);

        comboBox_voditelj = new JComboBox<>();
        comboBox_voditelj.setFont(new Font("Tahoma", Font.PLAIN, 14));
        comboBox_voditelj.setBounds(155, 306, 236, 27);
        frame.getContentPane().add(comboBox_voditelj);

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://student.veleri.hr/mbukovic?serverTimezone=UTC", "mbukovic", "11");
            String upit = "SELECT * FROM EM_Voditelj";
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery(upit);

            while (rs.next()) {
                String podatak = rs.getString(1);
                comboBox_voditelj.addItem(podatak);
            }
        } catch (Exception e2) {
            JOptionPane.showMessageDialog(null, e2);
        }

        JButton gumb_registriraj_se = new JButton("Registriraj se");
        gumb_registriraj_se.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String id_radniks = id_radnik.getText();
                String ime_radniks = ime_radnik.getText();
                String prezime_radniks = prezime_radnik.getText();
                String god_dat_rodenjas = god_dat_rodenja.getText();
                String id_voditeljs = (String) comboBox_voditelj.getSelectedItem();

                String url = "jdbc:mysql://student.veleri.hr/mbukovic";
                String user = "mbukovic";
                String password = "11";

                try (Connection con = DriverManager.getConnection(url, user, password)) {
                    String upit = "INSERT INTO EM_Radnik(id_radnik, ime_radnik, prezime_radnik, god_dat_rodenja, id_voditelj) VALUES(?,?,?,?,?)";
                    PreparedStatement ps = con.prepareStatement(upit);
                    ps.setString(1, id_radniks);
                    ps.setString(2, ime_radniks);
                    ps.setString(3, prezime_radniks);
                    ps.setString(4, god_dat_rodenjas);
                    ps.setString(5, id_voditeljs);

                    int redakaUbaceno = ps.executeUpdate();
                    if (redakaUbaceno == 1) {
                        JOptionPane.showMessageDialog(null, "Unos je uspješan.");
                    } else {
                        JOptionPane.showMessageDialog(null, "Unos podataka nije uspio. Pokušajte ponovno.");
                    }

                    String upitVoditelj = "SELECT id_voditelj FROM EM_Voditelj WHERE id_voditelj='" + id_voditeljs + "'";
                    Statement stmtVoditelj = con.createStatement();
                    ResultSet rsVoditelj = stmtVoditelj.executeQuery(upitVoditelj);

                    int idVod;
                    if (rsVoditelj.next()) {
                        idVod = rsVoditelj.getInt(1);
                    }
                } catch (Exception e1) {
                    JOptionPane.showMessageDialog(null, e1);
                }
            }
        });
        gumb_registriraj_se.setForeground(Color.BLUE);
        gumb_registriraj_se.setFont(new Font("Tahoma", Font.BOLD, 14));
        gumb_registriraj_se.setBounds(480, 376, 137, 27);
        frame.getContentPane().add(gumb_registriraj_se);

        JButton gumb_natrag = new JButton("Natrag");
        gumb_natrag.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Izbornik.main(null);
                frame.dispose();
            }
        });
        gumb_natrag.setFont(new Font("Tahoma", Font.BOLD, 14));
        gumb_natrag.setForeground(Color.RED);
        gumb_natrag.setBounds(29, 376, 110, 27);
        frame.getContentPane().add(gumb_natrag);
    }
}
Editor is loading...
Leave a Comment