Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import javax.swing.*;
- import javax.swing.event.TableModelEvent;
- import javax.swing.table.DefaultTableModel;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.sql.*;
- import java.text.DateFormat;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.time.LocalDate;
- import java.time.format.DateTimeFormatter;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.SimpleTimeZone;
- import java.util.Vector;
- import java.sql.Statement;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.ResultSetMetaData;
- public class Speler extends JFrame {
- //Connect to database and database statements
- Connection connection = DriverManager.getConnection("jdbc:mysql://meru.hhs.nl/18106986", "18106986", "hahghie7bi");
- //Statements
- private PreparedStatement veiligeUpdate = null;
- private Statement st;
- private ResultSet rs = null;
- private String s;
- //Table requirements
- private ResultSetMetaData rsmt;
- private int c;
- private Vector column;
- private Vector row;
- private Vector data;
- //private frame settings
- private JPanel panelLabel;
- private JPanel panelTextField;
- private JPanel panelButton;
- private JPanel panelTable;
- private JPanel border;
- private JPanel panelDelete;
- //Labels
- private JLabel labelNaam;
- private JLabel labelAdres;
- private JLabel labelPostcode;
- private JLabel labelWoonplaats;
- private JLabel labelTel;
- private JLabel labelMail;
- private JLabel labelGeboortedatum;
- private JLabel labelGeslacht;
- private JLabel labelSpelerCode;
- private JLabel labelverplicht;
- private JLabel labelZoek;
- //Textfield
- private JTextField textFieldNaam;
- private JTextField textFieldAdres;
- private JTextField textFieldPostcode;
- private JTextField textFieldWoonplaats;
- private JTextField textFieldTel;
- private JTextField textFieldMail;
- private JTextField textFieldGeboortedatum;
- private JTextField textFieldGeslacht;
- private JTextField textFieldSpelerCode;
- private JTextField textFieldZoek;
- //Buttons
- private JButton buttonTerug;
- private JButton button1Delete;
- private JButton buttonOpslaan;
- private JButton buttonUpdate;
- private JButton refresh;
- private JButton buttonZoek;
- //ScrollPane
- private JScrollPane jsp;
- //Table
- private JTable table;
- //Constructor
- public Speler () throws SQLException {
- //Frame settings
- setExtendedState(JFrame.MAXIMIZED_BOTH);
- setTitle("Speler");
- setDefaultCloseOperation(EXIT_ON_CLOSE);
- createComponents();
- createTable();
- opslaan();
- wijzig();
- verwijder();
- Refresh();
- Terug();
- Zoek();
- setVisible(true);
- }
- public void createComponents () {
- //Panel create
- panelLabel = new JPanel();
- panelTextField = new JPanel();
- panelButton = new JPanel();
- panelTable = new JPanel();
- panelDelete = new JPanel();
- border = new JPanel();
- //PanelLabel
- labelNaam = new JLabel("Naam");
- labelAdres = new JLabel("Adres");
- labelPostcode = new JLabel("Postcode");
- labelWoonplaats = new JLabel("Woonplaats");
- labelTel = new JLabel("Telefoonnummer");
- labelMail = new JLabel("eMail");
- labelGeboortedatum = new JLabel("Geboortedatum");
- labelGeslacht = new JLabel("Geslacht");
- labelSpelerCode = new JLabel("Speler code");
- labelverplicht = new JLabel("**Alle velden invullen is verplicht**");
- labelZoek = new JLabel("Zoek");
- //label is added to label panel
- panelLabel.setLayout(new GridLayout(11, 1));
- panelLabel.add(labelNaam);
- panelLabel.add(labelAdres);
- panelLabel.add(labelPostcode);
- panelLabel.add(labelWoonplaats);
- panelLabel.add(labelTel);
- panelLabel.add(labelMail);
- panelLabel.add(labelGeboortedatum);
- panelLabel.add(labelGeslacht);
- panelLabel.add(labelZoek);
- panelLabel.add(labelverplicht);
- //TextField creation
- textFieldNaam = new JTextField();
- textFieldAdres = new JTextField();
- textFieldPostcode = new JTextField();
- textFieldWoonplaats = new JTextField();
- textFieldTel = new JTextField();
- textFieldMail = new JTextField();
- textFieldGeboortedatum = new JTextField();
- textFieldGeslacht = new JTextField();
- textFieldSpelerCode = new JTextField();
- textFieldZoek = new JTextField();
- //paneltextfield toevoegen
- panelTextField.add(textFieldNaam);
- panelTextField.add(textFieldAdres);
- panelTextField.add(textFieldPostcode);
- panelTextField.add(textFieldWoonplaats);
- panelTextField.add(textFieldTel);
- panelTextField.add(textFieldMail);
- panelTextField.add(textFieldGeboortedatum);
- panelTextField.add(textFieldGeslacht);
- panelTextField.add(textFieldZoek);
- //textfieldlayout is set
- panelTextField.setLayout(new GridLayout(11, 1));
- //button creation
- buttonTerug = new JButton("Terug");
- button1Delete = new JButton("Delete");
- buttonUpdate = new JButton("Wijzig");
- buttonOpslaan = new JButton("Opslaan");
- refresh = new JButton("Refresh");
- buttonZoek = new JButton("Zoek");
- //Panel button adding the buttons
- panelButton.add(buttonTerug);
- panelButton.add(buttonOpslaan);
- panelButton.add(buttonUpdate);
- panelButton.add(refresh);
- panelButton.add(buttonZoek);
- //button layout set
- panelButton.setLayout(new GridLayout(1, 4));
- //panel delete
- panelDelete.add(button1Delete);
- panelDelete.add(textFieldSpelerCode);
- //panelDelete.add(button1Delete);
- //layout panel delete
- panelDelete.setLayout(new GridLayout(3, 1));
- //Borders adding the panels
- border.setLayout(new BorderLayout());
- border.add(panelLabel, BorderLayout.WEST);
- border.add(panelTextField, BorderLayout.CENTER);
- border.add(panelButton, BorderLayout.EAST);
- border.add(panelTable, BorderLayout.SOUTH);
- border.add(panelDelete, BorderLayout.NORTH);
- //Add border to frame
- add(border);
- }
- public void Refresh(){
- refresh.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- setVisible(false);
- try {
- new Speler();
- } catch (SQLException ex) {
- ex.printStackTrace();
- }
- }
- });
- }
- public void Zoek(){
- buttonZoek.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- try{
- veiligeUpdate = connection.prepareStatement("select * from speler where spelerCode=?");
- veiligeUpdate.setString(1,textFieldSpelerCode.getText());
- rs=veiligeUpdate.executeQuery();
- if(rs.next()){
- String add1 = rs.getString("naam");
- textFieldNaam.setText(add1);
- String add2 = rs.getString("adres");
- textFieldAdres.setText(add2);
- String add3 = rs.getString("postcode");
- textFieldPostcode.setText(add3);
- String add4 = rs.getString("woonplaats");
- textFieldWoonplaats.setText(add4);
- String add5 = rs.getString("telefoonnummer");
- textFieldTel.setText(add5);
- String add6 = rs.getString("email");
- textFieldMail.setText(add6);
- String add7 = rs.getString("geboortedatum");
- textFieldGeboortedatum.setText(add7);
- String add8 = rs.getString("geslacht");
- textFieldGeslacht.setText(add8);
- }
- }
- catch(Exception e1){
- JOptionPane.showMessageDialog(null,e1);
- }
- }
- });
- }
- public void Terug(){
- buttonTerug.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- setVisible(false);
- new Keuzebord();
- }
- });
- }
- // public int datumControle () throws SQLException, ParseException {
- // String pattern = "yyyy/MM/dd";
- // SimpleDateFormat format = new SimpleDateFormat(pattern);
- // Date date = format.parse(textFieldGeboortedatum.getText());
- // try {
- // if (date >) {
- // veiligeUpdate.setString(7, textFieldGeboortedatum.getText());
- // }
- // } catch (Exception e) {
- // JOptionPane.showMessageDialog(null, "De speler is niet 18 jaar oud");
- // }
- // return datum;
- // }
- LocalDate lc = new LocalDate();
- public void opslaan() throws SQLException {
- buttonOpslaan.addActionListener(new ActionListener() {
- int id = autoIncrementable();
- // int date = datumControle();
- public void actionPerformed(ActionEvent e) {
- try {
- veiligeUpdate = connection.prepareStatement(
- "INSERT INTO speler (spelercode, naam, adres, postcode, woonplaats, telefoonnummer, email, geboortedatum, geslacht) " +
- " VALUES (" + id + ", ?, ?, ?, ?, ?, ?, ?, ?);");
- } catch (SQLException ex) {
- ex.printStackTrace();
- }
- try {
- veiligeUpdate.setString(1, textFieldNaam.getText());
- veiligeUpdate.setString(2, textFieldAdres.getText());
- veiligeUpdate.setString(3, textFieldPostcode.getText());
- veiligeUpdate.setString(4, textFieldWoonplaats.getText());
- veiligeUpdate.setString(5, textFieldTel.getText());
- veiligeUpdate.setString(6, textFieldMail.getText());
- veiligeUpdate.setString(8, textFieldGeslacht.getText());
- } catch (SQLException ex) {
- ex.printStackTrace();
- }
- try {
- veiligeUpdate.executeUpdate();
- } catch (SQLException ex) {
- ex.printStackTrace();
- }
- }
- });
- }
- public void verwijder() {
- button1Delete.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- try {
- veiligeUpdate = connection.prepareStatement("DELETE FROM speler WHERE spelerCode = ?");
- } catch (SQLException ex) {
- ex.printStackTrace();
- }
- try {
- veiligeUpdate.setString(1, textFieldSpelerCode.getText());
- } catch (SQLException ex) {
- ex.printStackTrace();
- }
- try {
- veiligeUpdate.executeUpdate();
- } catch (SQLException ex) {
- ex.printStackTrace();
- }
- }
- });
- }
- public void wijzig() {
- buttonUpdate.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- try {
- veiligeUpdate = connection.prepareStatement(
- "UPDATE speler set naam=?, adres=?, postcode=?, woonplaats=?, telefoonnummer=?, email=?, geboortedatum=?, geslacht=? WHERE spelercode=?");
- } catch (SQLException ex) {
- ex.printStackTrace();
- }
- try {
- veiligeUpdate.setString(1, textFieldNaam.getText());
- veiligeUpdate.setString(2, textFieldAdres.getText());
- veiligeUpdate.setString(3, textFieldPostcode.getText());
- veiligeUpdate.setString(4, textFieldWoonplaats.getText());
- veiligeUpdate.setString(5, textFieldTel.getText());
- veiligeUpdate.setString(6, textFieldMail.getText());
- veiligeUpdate.setString(7, textFieldGeboortedatum.getText());
- veiligeUpdate.setString(8, textFieldGeslacht.getText());
- veiligeUpdate.setString(9, textFieldSpelerCode.getText());
- } catch (SQLException ex) {
- ex.printStackTrace();
- }
- try {
- veiligeUpdate.executeUpdate();
- } catch (SQLException ex) {
- ex.printStackTrace();
- }
- }
- });
- }
- public int autoIncrementable () throws SQLException {
- int id = 1;
- try {
- veiligeUpdate = connection.prepareStatement("select max(spelercode) from speler");
- rs = veiligeUpdate.executeQuery();
- while (rs.next()) {
- id = rs.getInt(1)+1;
- }
- } catch (Exception ex) {
- JOptionPane.showMessageDialog(null, "ERROR");
- }
- finally {
- try {
- } catch (Exception ex) {
- veiligeUpdate.close();
- rs.close();
- connection.close();
- }
- }
- return id;
- }
- public void createTable() {
- try {
- st = connection.createStatement();
- s = "select * from speler";
- rs = st.executeQuery(s);
- rsmt = rs.getMetaData();
- c = rsmt.getColumnCount();
- column = new Vector(c);
- for (int i = 1; i <= c; i++) {
- column.add(rsmt.getColumnName(i));
- }
- data = new Vector();
- row = new Vector();
- while (rs.next()) {
- row = new Vector(c);
- for (int i = 1; i <= c; i++) {
- row.add(rs.getString(i));
- }
- data.add(row);
- }
- table = new JTable(data, column);
- jsp = new JScrollPane(table);
- panelTable.setLayout(new BorderLayout());
- panelTable.add(jsp, BorderLayout.SOUTH);
- } catch (Exception e) {
- JOptionPane.showMessageDialog(null, "ERROR");
- } finally {
- try {
- rs.close();
- } catch (Exception e) {
- JOptionPane.showMessageDialog(null, "ERROR CLOSE");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement