schemagenerator.DBbeheer.java Source code

Java tutorial

Introduction

Here is the source code for schemagenerator.DBbeheer.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package schemagenerator;

import java.awt.event.KeyEvent;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;
import net.proteanit.sql.DbUtils;
import org.apache.commons.lang.WordUtils;

public class DBbeheer extends javax.swing.JFrame {

    DBconnect db = new DBconnect();

    private String conURL = "jdbc:sqlite:ShapersDatabase.sqlite";
    private String conUN = "";
    private String conPW = "";

    Connection con = null;
    ResultSet rs = null;
    PreparedStatement pst = null;

    /**
     * Creates new form DBbeheer
     */
    public DBbeheer() {
        try {
            initComponents();
            DBconnect db = new DBconnect();
            con = DriverManager.getConnection(conURL, conUN, conPW);
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, ex);
        }
        UpdateTable();
        FillComboBox();
        ClearAllTextFields();
    }

    private void UpdateTable() {
        try {
            String query = "SELECT spiergroep, oefening, categorie, plaatje FROM oefeningen ORDER BY spiergroep ASC";
            pst = con.prepareStatement(query);
            rs = pst.executeQuery();
            Table_Oefeningen.setModel(DbUtils.resultSetToTableModel(rs));
            pst.close();
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(this, ex);
        }
    }

    private void FillComboBox() {
        cbOefeningen.removeAllItems();
        cbOefeningen.addItem("Oefeningen");
        try {
            String sql = "SELECT * FROM oefeningen ORDER BY oefening";
            pst = con.prepareStatement(sql);
            rs = pst.executeQuery();

            while (rs.next()) {
                String oefening = rs.getString("oefening");
                cbOefeningen.addItem(oefening);
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, ex);
        }
    }

    private void ClearAllTextFields() {
        jtfID.setText(null);
        jtfCategorie.setText(null);
        jtfOefening.setText(null);
        jtfSpiergroep.setText(null);
        jlPlaatje.setIcon(null);
        jtfPad.setText(null);
    }

    /**
     * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The content of this method is always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        Table_Oefeningen = new javax.swing.JTable();
        cbOefeningen = new javax.swing.JComboBox();
        jPanel1 = new javax.swing.JPanel();
        jlSpiergroep = new javax.swing.JLabel();
        jlOefening = new javax.swing.JLabel();
        jlCategorie = new javax.swing.JLabel();
        jtfOefening = new javax.swing.JTextField();
        jtfCategorie = new javax.swing.JTextField();
        jlID = new javax.swing.JLabel();
        jtfID = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jbAddImage = new javax.swing.JButton();
        jtfPad = new javax.swing.JTextField();
        jtfSpiergroep = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jbVerwijderen = new javax.swing.JButton();
        jbRefresh = new javax.swing.JButton();
        jbBewerken = new javax.swing.JButton();
        jbToevoegen = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jtfZoeken = new javax.swing.JTextField();
        jDesktopPane1 = new javax.swing.JDesktopPane();
        jlPlaatje = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        Table_Oefeningen
                .setModel(new javax.swing.table.DefaultTableModel(
                        new Object[][] { { null, null, null, null }, { null, null, null, null },
                                { null, null, null, null }, { null, null, null, null } },
                        new String[] { "Title 1", "Title 2", "Title 3", "Title 4" }));
        Table_Oefeningen.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                Table_OefeningenMouseClicked(evt);
            }
        });
        Table_Oefeningen.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                Table_OefeningenKeyReleased(evt);
            }
        });
        jScrollPane1.setViewportView(Table_Oefeningen);

        cbOefeningen.setModel(
                new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cbOefeningen.addPopupMenuListener(new javax.swing.event.PopupMenuListener() {
            public void popupMenuCanceled(javax.swing.event.PopupMenuEvent evt) {
            }

            public void popupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {
                cbOefeningenPopupMenuWillBecomeInvisible(evt);
            }

            public void popupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt) {
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Oefening gegevens"));

        jlSpiergroep.setText("Spiergroep");

        jlOefening.setText("Oefening");

        jlCategorie.setText("Categorie");

        jlID.setText("ID");

        jtfID.setEditable(false);

        jLabel2.setText("Plaatje");

        jbAddImage.setText("Plaatje toevoegen");
        jbAddImage.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbAddImageActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(jPanel1Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap().addGroup(jPanel1Layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jtfPad)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(
                                        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(jlSpiergroep).addComponent(jlOefening)
                                                .addComponent(jlCategorie).addComponent(jlID).addComponent(jLabel2))
                                .addGap(18, 18, 18)
                                .addGroup(jPanel1Layout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jtfOefening).addComponent(jtfCategorie).addComponent(jtfID)
                                        .addComponent(jbAddImage, javax.swing.GroupLayout.Alignment.TRAILING,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(jtfSpiergroep))))));
        jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jlID).addComponent(jtfID, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jlSpiergroep).addComponent(jtfSpiergroep,
                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jlOefening).addComponent(jtfOefening,
                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jlCategorie)
                                .addComponent(jtfCategorie, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel2).addComponent(jbAddImage))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jtfPad, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)));

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Bediening"));

        jbVerwijderen.setText("Verwijderen");
        jbVerwijderen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbVerwijderenActionPerformed(evt);
            }
        });

        jbRefresh.setText("Refresh het hele frame");
        jbRefresh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbRefreshActionPerformed(evt);
            }
        });

        jbBewerken.setText("Bewerken");
        jbBewerken.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbBewerkenActionPerformed(evt);
            }
        });

        jbToevoegen.setText("Toevoegen");
        jbToevoegen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbToevoegenActionPerformed(evt);
            }
        });

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Zoeken in de database"));

        jtfZoeken.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jtfZoekenKeyReleased(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
                jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(jPanel3Layout
                        .createSequentialGroup().addContainerGap().addComponent(jtfZoeken).addContainerGap()));
        jPanel3Layout.setVerticalGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel3Layout.createSequentialGroup().addContainerGap()
                        .addComponent(jtfZoeken, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(jPanel2Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup().addContainerGap()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jbBewerken, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jbVerwijderen, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jbRefresh, javax.swing.GroupLayout.DEFAULT_SIZE, 225, Short.MAX_VALUE)
                                .addComponent(jbToevoegen, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addContainerGap()));
        jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup().addContainerGap().addComponent(jbToevoegen)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbBewerken)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbVerwijderen)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jbRefresh)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)));

        javax.swing.GroupLayout jDesktopPane1Layout = new javax.swing.GroupLayout(jDesktopPane1);
        jDesktopPane1.setLayout(jDesktopPane1Layout);
        jDesktopPane1Layout.setHorizontalGroup(
                jDesktopPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jDesktopPane1Layout.createSequentialGroup().addContainerGap()
                                .addComponent(jlPlaatje, javax.swing.GroupLayout.DEFAULT_SIZE, 253, Short.MAX_VALUE)
                                .addContainerGap()));
        jDesktopPane1Layout
                .setVerticalGroup(jDesktopPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jDesktopPane1Layout.createSequentialGroup().addContainerGap()
                                .addComponent(jlPlaatje, javax.swing.GroupLayout.DEFAULT_SIZE, 251, Short.MAX_VALUE)
                                .addContainerGap()));
        jDesktopPane1.setLayer(jlPlaatje, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jScrollPane1)
                                .addGroup(layout.createSequentialGroup().addGroup(
                                        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(cbOefeningen, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        260, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jDesktopPane1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addContainerGap()));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 200,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addGroup(layout.createSequentialGroup()
                                                .addComponent(cbOefeningen, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addComponent(jDesktopPane1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(22, Short.MAX_VALUE)));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void Table_OefeningenMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_Table_OefeningenMouseClicked
        try {
            int row = Table_Oefeningen.getSelectedRow();
            String Table_click = (Table_Oefeningen.getModel().getValueAt(row, 0).toString());
            String sql = "SELECT * FROM oefeningen WHERE spiergroep='" + Table_click + "'";
            pst = con.prepareStatement(sql);
            rs = pst.executeQuery();
            if (rs.next()) {
                String add1 = rs.getString("ID");
                jtfID.setText(add1);
                String add2 = rs.getString("spiergroep");
                jtfSpiergroep.setText(add2);
                String add3 = rs.getString("oefening");
                jtfOefening.setText(add3);
                String add4 = rs.getString("categorie");
                jtfCategorie.setText(add4);
                byte[] imagedata = rs.getBytes("plaatje");
                format = new ImageIcon(imagedata);
                jlPlaatje.setIcon(format);
            }
        } catch (Exception ex) {
            try {
                int row = Table_Oefeningen.getSelectedRow();
                String Table_click = (Table_Oefeningen.getModel().getValueAt(row, 0).toString());
                String sql = "SELECT * FROM oefeningen WHERE spiergroep='" + Table_click + "'";
                pst = con.prepareStatement(sql);
                rs = pst.executeQuery();
                if (rs.next()) {
                    String add1 = rs.getString("ID");
                    jtfID.setText(add1);
                    String add2 = rs.getString("spiergroep");
                    jtfSpiergroep.setText(add2);
                    String add3 = rs.getString("oefening");
                    jtfOefening.setText(add3);
                    String add4 = rs.getString("categorie");
                    jtfCategorie.setText(add4);
                    jlPlaatje.setIcon(null);
                }
            } catch (SQLException SQLE) {
                Logger.getLogger(DBbeheer.class.getName()).log(Level.SEVERE, null, SQLE);
            }
        }
    }//GEN-LAST:event_Table_OefeningenMouseClicked

    private void cbOefeningenPopupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {//GEN-FIRST:event_cbOefeningenPopupMenuWillBecomeInvisible
        String tmp = (String) cbOefeningen.getSelectedItem();
        String sql = "SELECT * FROM oefeningen WHERE oefening = ? ORDER BY oefening ASC";
        try {
            pst = con.prepareStatement(sql);
            pst.setString(1, tmp);
            rs = pst.executeQuery();
            if (rs.next()) {
                String add1 = rs.getString("ID");
                jtfID.setText(add1);
                String add2 = rs.getString("spiergroep");
                jtfSpiergroep.setText(add2);
                String add3 = rs.getString("oefening");
                jtfOefening.setText(add3);
                String add4 = rs.getString("categorie");
                jtfCategorie.setText(add4);
                byte[] imagedata = rs.getBytes("plaatje");
                format = new ImageIcon(imagedata);
                jlPlaatje.setIcon(format);
            }
        } catch (Exception ex) {
            try {
                pst = con.prepareStatement(sql);
                pst.setString(1, tmp);
                rs = pst.executeQuery();
                if (rs.next()) {
                    try {
                        String add1 = rs.getString("ID");
                        jtfID.setText(add1);
                        String add2 = rs.getString("spiergroep");
                        jtfSpiergroep.setText(add2);
                        String add3 = rs.getString("oefening");
                        jtfOefening.setText(add3);
                        String add4 = rs.getString("categorie");
                        jtfCategorie.setText(add4);
                        jlPlaatje.setIcon(null);
                    } catch (SQLException SQLE) {
                        JOptionPane.showMessageDialog(null, SQLE);
                    }
                }
            } catch (SQLException SQLEx) {
                JOptionPane.showMessageDialog(null, SQLEx);
            }
        }

    }//GEN-LAST:event_cbOefeningenPopupMenuWillBecomeInvisible

    private void jbToevoegenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbToevoegenActionPerformed
        if (jtfSpiergroep.getText().isEmpty() || jtfOefening.getText().isEmpty()) {
            JOptionPane.showMessageDialog(null, "Check of er een spiergroep of oefening ingevuld", "Insert Error",
                    JOptionPane.ERROR_MESSAGE);
        } else {
            try {
                //eerste letter van spiergroep, oefening en categorie textboxen
                //worden omgezet naar hoofdletter, rest naar kleine letters
                String sql = "INSERT INTO oefeningen (spiergroep, oefening, categorie, plaatje) VALUES (?,?,?,?)";
                pst = con.prepareStatement(sql);

                pst.setString(1, WordUtils.capitalize(jtfSpiergroep.getText()));
                pst.setString(2, WordUtils.capitalize(jtfOefening.getText()));
                pst.setString(3, WordUtils.capitalize(jtfCategorie.getText()));
                pst.setBytes(4, oefening_image);
                pst.executeUpdate();

                ClearAllTextFields();
                FillComboBox();
                UpdateTable();
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(null, ex);
            }
        }
    }//GEN-LAST:event_jbToevoegenActionPerformed

    private void jbBewerkenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbBewerkenActionPerformed
        if (jtfSpiergroep.getText().isEmpty() || jtfOefening.getText().isEmpty()) {
            JOptionPane.showMessageDialog(null, "Check of er een spiergroep of oefening ingevuld", "Update Error",
                    JOptionPane.ERROR_MESSAGE);
        } else {
            try {
                String ID = jtfID.getText();
                String spiergroep = jtfSpiergroep.getText();
                String oefening = jtfOefening.getText();
                String categorie = jtfCategorie.getText();
                String sql = "UPDATE oefeningen SET ID = '" + ID + "', `spiergroep`= '" + spiergroep
                        + "', `oefening`= '" + oefening + "', `categorie`= '" + categorie + "' WHERE `ID`= '" + ID
                        + "'";

                pst = con.prepareStatement(sql);
                pst.execute();

                ClearAllTextFields();
                FillComboBox();
                UpdateTable();
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(null, ex);
            }
        }
    }//GEN-LAST:event_jbBewerkenActionPerformed

    private void jbVerwijderenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbVerwijderenActionPerformed
        String sql = "DELETE FROM oefeningen WHERE ID = ?";
        String ID = jtfID.getText();
        try {
            pst = con.prepareStatement(sql);
            pst.setString(1, ID);
            pst.executeUpdate();

            ClearAllTextFields();
            FillComboBox();
            UpdateTable();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, ex);
        }
    }//GEN-LAST:event_jbVerwijderenActionPerformed

    private void jbRefreshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbRefreshActionPerformed
        ClearAllTextFields();
        FillComboBox();
        UpdateTable();
    }//GEN-LAST:event_jbRefreshActionPerformed

    private void jtfZoekenKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtfZoekenKeyReleased
        String query = "SELECT * FROM oefeningen WHERE ID = ? OR spiergroep = ? OR oefening = ? OR categorie = ? OR plaatje = ?";
        try {
            pst = con.prepareStatement(query);
            pst.setString(1, jtfZoeken.getText());
            pst.setString(2, jtfZoeken.getText());
            pst.setString(3, jtfZoeken.getText());
            pst.setString(4, jtfZoeken.getText());

            rs = pst.executeQuery();
            if (rs.next()) {
                String add1 = rs.getString("ID");
                jtfID.setText(add1);
                String add2 = rs.getString("spiergroep");
                jtfSpiergroep.setText(add2);
                String add3 = rs.getString("oefening");
                jtfOefening.setText(add3);
                String add4 = rs.getString("categorie");
                jtfCategorie.setText(add4);
                byte[] imagedata = rs.getBytes("plaatje");
                format = new ImageIcon(imagedata);
                jlPlaatje.setIcon(format);
            }
        } catch (Exception ex) {
            try {
                pst = con.prepareStatement(query);
                pst.setString(1, jtfZoeken.getText());
                pst.setString(2, jtfZoeken.getText());
                pst.setString(3, jtfZoeken.getText());
                pst.setString(4, jtfZoeken.getText());

                rs = pst.executeQuery();
                if (rs.next()) {
                    String add1 = rs.getString("ID");
                    jtfID.setText(add1);
                    String add2 = rs.getString("spiergroep");
                    jtfSpiergroep.setText(add2);
                    String add3 = rs.getString("oefening");
                    jtfOefening.setText(add3);
                    String add4 = rs.getString("categorie");
                    jtfCategorie.setText(add4);
                    byte[] imagedata = rs.getBytes("plaatje");
                    format = new ImageIcon(imagedata);
                    jlPlaatje.setIcon(null);
                }
            } catch (SQLException ex1) {
                JOptionPane.showMessageDialog(null, ex1);
            }
        }
    }//GEN-LAST:event_jtfZoekenKeyReleased

    private void Table_OefeningenKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_Table_OefeningenKeyReleased
        if (evt.getKeyCode() == KeyEvent.VK_DOWN || evt.getKeyCode() == KeyEvent.VK_UP) {
            try {
                int row = Table_Oefeningen.getSelectedRow();
                String tableClick = (Table_Oefeningen.getModel().getValueAt(row, 0).toString());
                String sql = "SELECT * FROM oefeningen WHERE spiergroep='" + tableClick + "'";
                pst = con.prepareStatement(sql);
                rs = pst.executeQuery();
                if (rs.next()) {
                    String add1 = rs.getString("ID");
                    jtfID.setText(add1);
                    String add2 = rs.getString("spiergroep");
                    jtfSpiergroep.setText(add2);
                    String add3 = rs.getString("oefening");
                    jtfOefening.setText(add3);
                    String add4 = rs.getString("categorie");
                    jtfCategorie.setText(add4);
                    byte[] imagedata = rs.getBytes("plaatje");
                    format = new ImageIcon(imagedata);
                    jlPlaatje.setIcon(format);
                }
            } catch (Exception ex) {
                try {
                    int row = Table_Oefeningen.getSelectedRow();
                    String tableClick = (Table_Oefeningen.getModel().getValueAt(row, 0).toString());
                    String sql = "SELECT * FROM oefeningen WHERE spiergroep='" + tableClick + "'";
                    pst = con.prepareStatement(sql);
                    rs = pst.executeQuery();
                    if (rs.next()) {
                        String add1 = rs.getString("ID");
                        jtfID.setText(add1);
                        String add2 = rs.getString("spiergroep");
                        jtfSpiergroep.setText(add2);
                        String add3 = rs.getString("oefening");
                        jtfOefening.setText(add3);
                        String add4 = rs.getString("categorie");
                        jtfCategorie.setText(add4);
                        jlPlaatje.setIcon(null);
                    }
                } catch (Exception ex1) {
                    JOptionPane.showMessageDialog(null, ex1);
                }
            }
        }
    }//GEN-LAST:event_Table_OefeningenKeyReleased

    private void jbAddImageActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbAddImageActionPerformed

        JFileChooser chooser = new JFileChooser();
        FileNameExtensionFilter filterPNG = new FileNameExtensionFilter("PNG bestanden", "png");
        chooser.setFileFilter(filterPNG);
        chooser.showOpenDialog(null);
        File f = chooser.getSelectedFile();
        filename = f.getAbsolutePath();
        jtfPad.setText(filename);

        try {
            File image = new File(filename);
            FileInputStream fis = new FileInputStream(image);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] buf = new byte[1024];
            for (int readNum; (readNum = fis.read(buf)) != -1;) {
                bos.write(buf, 0, readNum);
            }
            oefening_image = bos.toByteArray();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, ex);
        }

    }//GEN-LAST:event_jbAddImageActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Metal look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Metal".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException
                | javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(DBbeheer.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        }
        //</editor-fold>
        //</editor-fold>

        //</editor-fold>
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new DBbeheer().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable Table_Oefeningen;
    private javax.swing.JComboBox cbOefeningen;
    private javax.swing.JDesktopPane jDesktopPane1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbAddImage;
    private javax.swing.JButton jbBewerken;
    private javax.swing.JButton jbRefresh;
    private javax.swing.JButton jbToevoegen;
    private javax.swing.JButton jbVerwijderen;
    private javax.swing.JLabel jlCategorie;
    private javax.swing.JLabel jlID;
    private javax.swing.JLabel jlOefening;
    private javax.swing.JLabel jlPlaatje;
    private javax.swing.JLabel jlSpiergroep;
    private javax.swing.JTextField jtfCategorie;
    private javax.swing.JTextField jtfID;
    private javax.swing.JTextField jtfOefening;
    private javax.swing.JTextField jtfPad;
    private javax.swing.JTextField jtfSpiergroep;
    private javax.swing.JTextField jtfZoeken;
    // End of variables declaration//GEN-END:variables

    private ImageIcon format = null;
    String filename = null;
    int s = 0;
    byte[] oefening_image = null;
}