- Elipse : java : MySQL query, menubar , internal frame, form klanten ,DefaultTableModel
- MySQL - tabel :
-- phpMyAdmin SQL Dump
-- version 2.6.1-pl3
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generatie Tijd: 15 Jul 2007 om 11:24
-- Server versie: 5.0.24
-- PHP Versie: 5.0.3
--
-- Database: `voorraad`
--
-- --------------------------------------------------------
--
-- Tabel structuur voor tabel `klanten`
--
DROP TABLE IF EXISTS `klanten`;
CREATE TABLE `klanten` (
`id` int(11) NOT NULL auto_increment,
`geslacht` char(1) NOT NULL,
`achternaam` varchar(30) NOT NULL,
`voornaam` varchar(30) NOT NULL,
`straat` varchar(50) NOT NULL,
`huisnr` char(10) NOT NULL,
`postnr` char(4) NOT NULL,
`gemeente` varchar(30) NOT NULL,
`telefoon` varchar(12) NOT NULL,
`GSM` varchar(20) NOT NULL,
`fax` varchar(12) NOT NULL,
`email` varchar(50) NOT NULL,
`korting` int(2) NOT NULL,
`nieuws` char(1) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
--
-- Gegevens worden uitgevoerd voor tabel `klanten`
--
INSERT INTO `klanten` VALUES (1, 'm', 'de
gieter', 'jefke', 'mollestraat ', '362', '8640', 'vleteren', '057400017', '',
'057400017', 'degieter@gegoten.com', 0, 'n');
INSERT INTO `klanten` VALUES (2, 'm', 'de wachter', 'jozef', 'diepestraat',
'31', '8641', 'izenberge', '057425218', 'geen', '057555223', 'dewachter@wachteenbeke.be',
2, 'j');
INSERT INTO `klanten` VALUES (3, 'm', 'de gieter', 'jaak', 'mollestraat ',
'362', '8640', 'vleteren', '057425217', '448 155515', '057555222', 'bler@blobbers.com',
5, 'n');
INSERT INTO `klanten` VALUES (4, 'm', 'maresco', 'stefaan', 'diepebeekstraat ',
'36', '8640', 'vleteren', '057425217', '448 145915', '057555222', 'maresco@blobbers.com',
5, 'n');
INSERT INTO `klanten` VALUES (7, 'v', 'smolders', 'jenny', 'vennestraat', '62',
'4525', 'izenberge', '0245678925', 'geen', 'geen', 'jenny@column.com', 0, 'j');
INSERT INTO `klanten` VALUES (8, 'v', 'van santen', 'mary', 'wipstraat', '47',
'2010', 'vleteren', '0358946545', 'geen', 'geen', 'mary@strijders.com', 0, 'n');
-
- Eclips project
- - src
- + AdminKlanten.java
- +OpzoekenKlant.java
- +VoorraadBeheer.java
1.Code :: AdminKlanten.java
package src;
import java.awt.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableModel;
import java.awt.event.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class AdminKlanten extends JInternalFrame
{
final String[] columnNames = {"id","Familienaam","Voornaam","Straat","Nr","Postnr","Gemeente"};
Object[][] record = new Object[1000][7];
java.util.List<Klant> klanten = new ArrayList<Klant> ();
final DefaultTableModel myModel = new DefaultTableModel(record,columnNames )
{
public Class getColumnClass(int c)
{
return getValueAt(0, c).getClass();
}
};
JTable table = new JTable(myModel);
JButton knp_nieuw = new JButton("Nieuw");
JButton knp_toevoegen = new JButton("Toevoegen");
JButton knp_wijzigen = new JButton("wijzigen");
JButton knp_delete = new JButton("Verwijderen");
JLabel lab01 = new JLabel("Klantnummer");
JLabel lab02 = new JLabel("Geslacht");
JLabel lab03 = new JLabel("Achternaam");
JLabel lab04 = new JLabel("Voornaam");
JLabel lab05 = new JLabel("Straat");
JLabel lab06 = new JLabel("Huisnr");
JLabel lab07 = new JLabel("Postnummer");
JLabel lab08 = new JLabel("Gemeente");
JLabel lab09 = new JLabel("Telefoon");
JLabel lab10 = new JLabel("GSM");
JLabel lab11 = new JLabel("Fax");
JLabel lab12 = new JLabel("E-post");
JLabel lab13 = new JLabel("Korting %");
JLabel lab14 = new JLabel("Niewsbrief");
JTextField rs01 = new JTextField("auto");//klnt_id :: disable!
JTextField rs02 = new JTextField("");//klnt_geslacht
JTextField rs03 = new JTextField("");//klnt_voornaam
JTextField rs04 = new JTextField("");//klnt_achternaam
JTextField rs05 = new JTextField("");//klnt_straat
JTextField rs06 = new JTextField("");//klnt_huisnr
JTextField rs07 = new JTextField("");//klnt_postnr
JTextField rs08 = new JTextField("");//klnt_gemeente
JTextField rs09 = new JTextField("");//klnt_telefoon
JTextField rs10 = new JTextField("");//klnt_gsm
JTextField rs11 = new JTextField("");//klnt_fax
JTextField rs12 = new JTextField("");//klnt_email
JTextField rs13 = new JTextField("");//klnt_korting
JTextField rs14 = new JTextField("");//klnt_nieuws
public AdminKlanten()
{
super("Klanten",true,true,true,true);
try {
} catch (Exception exception) {
exception.printStackTrace();
}
knp_nieuw.addActionListener(new KnpnieHandler());knp_nieuw.setBounds(550, 190,
140, 20);add(knp_nieuw);
knp_toevoegen.addActionListener(new KnptoeHandler());knp_toevoegen.setBounds(550,
220, 140, 20);add(knp_toevoegen);
knp_wijzigen.addActionListener(new KnpwijHandler());knp_wijzigen.setBounds(550,
250, 140, 20);add(knp_wijzigen);
knp_delete.addActionListener(new KnpdelHandler());knp_delete.setBounds(550, 280,
140, 20);add(knp_delete);
add(lab01);lab01.setForeground(Color.black);lab01.setBounds(190, 20, 150,
20);lab01.setHorizontalAlignment(SwingConstants.RIGHT);
add(lab02);lab02.setForeground(Color.black);lab02.setBounds(190, 40, 150,
20);lab02.setHorizontalAlignment(SwingConstants.RIGHT);
add(lab03);lab03.setForeground(Color.black);lab03.setBounds(190, 60, 150,
20);lab03.setHorizontalAlignment(SwingConstants.RIGHT);
add(lab04);lab04.setForeground(Color.black);lab04.setBounds(190, 80, 150,
20);lab04.setHorizontalAlignment(SwingConstants.RIGHT);
add(lab05);lab05.setForeground(Color.black);lab05.setBounds(190, 100, 150,
20);lab05.setHorizontalAlignment(SwingConstants.RIGHT);
add(lab06);lab06.setForeground(Color.black);lab06.setBounds(190, 120, 150,
20);lab06.setHorizontalAlignment(SwingConstants.RIGHT);
add(lab07);lab07.setForeground(Color.black);lab07.setBounds(190, 140, 150,
20);lab07.setHorizontalAlignment(SwingConstants.RIGHT);
add(lab08);lab08.setForeground(Color.black);lab08.setBounds(190, 160, 150,
20);lab08.setHorizontalAlignment(SwingConstants.RIGHT);
add(lab09);lab09.setForeground(Color.black);lab09.setBounds(190, 180, 150,
20);lab09.setHorizontalAlignment(SwingConstants.RIGHT);
add(lab10);lab10.setForeground(Color.black);lab10.setBounds(190, 200, 150,
20);lab10.setHorizontalAlignment(SwingConstants.RIGHT);
add(lab11);lab11.setForeground(Color.black);lab11.setBounds(190, 220, 150,
20);lab11.setHorizontalAlignment(SwingConstants.RIGHT);
add(lab12);lab12.setForeground(Color.black);lab12.setBounds(190, 240, 150,
20);lab12.setHorizontalAlignment(SwingConstants.RIGHT);
add(lab13);lab13.setForeground(Color.black);lab13.setBounds(190, 260, 150,
20);lab13.setHorizontalAlignment(SwingConstants.RIGHT);
add(lab14);lab14.setForeground(Color.black);lab14.setBounds(190, 280, 150,
20);lab14.setHorizontalAlignment(SwingConstants.RIGHT);
add(rs01);rs01.setBounds(350, 20, 150, 20);
add(rs02);rs02.setBounds(350, 40, 150, 20);
add(rs03);rs03.setBounds(350, 60, 150, 20);
add(rs04);rs04.setBounds(350, 80, 150, 20);
add(rs05);rs05.setBounds(350, 100, 150, 20);
add(rs06);rs06.setBounds(350, 120, 150, 20);
add(rs07);rs07.setBounds(350, 140, 150, 20);
add(rs08);rs08.setBounds(350, 160, 150, 20);
add(rs09);rs09.setBounds(350, 180, 150, 20);
add(rs10);rs10.setBounds(350, 200, 150, 20);
add(rs11);rs11.setBounds(350, 220, 150, 20);
add(rs12);rs12.setBounds(350, 240, 150, 20);
add(rs13);rs13.setBounds(350, 260, 150, 20);
add(rs14);rs14.setBounds(350, 280, 150, 20);
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
vulTabel();
table.setSurrendersFocusOnKeystroke(true);
table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
class pwLeesToets extends KeyAdapter{
public void keyReleased(KeyEvent e)
{
int key = e.getKeyCode();
if (( key == 13)||(key == 10))
{
int r = table.getSelectedRow();
rs03.setText(table.getValueAt(r,1).toString());//achternaam
rs04.setText(table.getValueAt(r,2).toString());//voornaam
rs05.setText(table.getValueAt(r,3).toString());//straat table edit
rs06.setText(table.getValueAt(r,4).toString());//huisnr table edit
rs07.setText(table.getValueAt(r,5).toString());//postnr table edit
rs08.setText(table.getValueAt(r,6).toString());//gemeente table edit
//table.nextFocus();
}
}
}
table.addKeyListener(new pwLeesToets());
table.addMouseListener( new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
int r = table.rowAtPoint(e.getPoint());
String s = table.getValueAt(r,0).toString();
int i01 = Integer.parseInt(s);// bij delete veranderen listitem n recorditem!
actieTabelselectie(i01);
}
});
JScrollPane jScrollPane = new JScrollPane();
jScrollPane.setViewportView(table);
Font font01 = new Font("Arial", Font.BOLD, 12);
Font font02 = new Font("Arial", Font.BOLD, 18);
//Font font03 = new Font("Arial", Font.BOLD, 14);
JLabel lab01 = new JLabel("Criteria");
JLabel lab02 = new JLabel("Zoekitem");
JLabel lab03 = new JLabel("Administratie Klanten");
add(lab01);lab01.setFont(font02);lab01.setForeground(Color.GRAY);lab01.setBounds(-20,
360, 100, 20);lab01.setHorizontalAlignment(SwingConstants.RIGHT);
add(lab02);lab02.setFont(font02);lab02.setForeground(Color.GRAY);lab02.setBounds(190,
360, 150, 20);lab02.setHorizontalAlignment(SwingConstants.RIGHT);
add(lab03);lab03.setFont(font02);lab03.setForeground(Color.darkGray);lab03.setBounds(-45,
15, 250, 30);lab03.setHorizontalAlignment(SwingConstants.RIGHT);
final JTextField rs01 = new JTextField("");//zoek item
add(rs01);rs01.setBounds(350, 360, 150, 20);rs01.setFont(font01);
JButton knopZoek = new JButton("Zoek");
knopZoek.setBounds(550, 360, 140, 20);
knopZoek.setFont(font01);
add(knopZoek);
final JComboBox comboBox = new JComboBox();
comboBox.setName("JComboBox");
comboBox.setBounds(90,360,150,20);
comboBox.setPreferredSize(new java.awt.Dimension(100,22));
comboBox.addItem("");
comboBox.addItem("Id");
comboBox.addItem("achternaam");
comboBox.addItem("Voornaam");
comboBox.addItem("Straat");
comboBox.addItem("Huisnummer");
comboBox.addItem("Postnummer");
comboBox.addItem("Gemeente");
comboBox.setFont(font01);
add(comboBox);
for(int x = 0 ; x < 26 ; x++)
{
panel.add(new JLabel(" "));
}
knopZoek.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
int y = myModel.getRowCount();
for(int a = 0; a < y; a++)
{
myModel.removeRow(0);
}
System.out.println(comboBox.getSelectedItem());
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection ("jdbc:mysql://localhost/voorraad",
"user", "passwoord");
Statement statement = conn.createStatement();
ResultSet rs = statement.executeQuery("SELECT * FROM klanten WHERE "+comboBox.getSelectedItem()+"
= '"+rs01.getText()+"' LIMIT 0 , 1000");
int x;x=0;
while (rs.next()) {
record[x][0]=String.valueOf(rs.getInt("id"));
record[x][1]=rs.getString("achternaam");
record[x][2]=rs.getString("voornaam");
record[x][3]=rs.getString("straat");
record[x][4]=rs.getString("huisnr");
record[x][5]=rs.getString("postnr");
record[x][6]=rs.getString("gemeente");
myModel.addRow(record[x]);
x++;
}
conn.close();
}
catch (Exception ex)
{
ex.getStackTrace();
}
}
}
);
panel.add(jScrollPane);
getContentPane().add(panel);
Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
setSize(new Dimension(800, 600));
setLocation((screenSize.width-800)/2,(screenSize.height-600)/2);
}
public void actieTabelselectie(int selectie){
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection ("jdbc:mysql://localhost/voorraad",
"user", "passwoord");
Statement statement = conn.createStatement();
ResultSet rs = statement.executeQuery("SELECT * FROM klanten WHERE id="+(selectie));
while (rs.next()) {
rs01.setText(rs.getString("id"));//omzetten naar string
rs02.setText(rs.getString("geslacht"));
rs03.setText(rs.getString("achternaam"));
rs04.setText(rs.getString("voornaam"));
rs05.setText(rs.getString("straat"));
rs06.setText(rs.getString("huisnr"));
rs07.setText(rs.getString("postnr"));
rs08.setText(rs.getString("gemeente"));
rs09.setText(rs.getString("telefoon"));
rs10.setText(rs.getString("GSM"));
rs11.setText(rs.getString("fax"));
rs10.setText(rs.getString("email"));
rs13.setText(rs.getString("korting"));
rs14.setText(rs.getString("nieuws"));
rs01.disable();
}
conn.close();
}
catch (Exception ex)
{
ex.getStackTrace();
}
}
@SuppressWarnings("test")
public void vulTabel(){
int y = myModel.getRowCount();
for(int a = 0; a < y; a++)
{
myModel.removeRow(0);
}
try
{
System.out.println("start");
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection ("jdbc:mysql://localhost/voorraad",
"user", "passwoord");
Statement statement = conn.createStatement();
ResultSet rs = statement.executeQuery("SELECT * FROM `klanten`LIMIT 0 , 1000");
int x;x=0;
while (rs.next()) {
record[x][0]=String.valueOf(rs.getInt("id"));
record[x][1]=rs.getString("achternaam");
record[x][2]=rs.getString("voornaam");
record[x][3]=rs.getString("straat");
record[x][4]=rs.getString("huisnr");
record[x][5]=rs.getString("postnr");
record[x][6]=rs.getString("gemeente");
myModel.addRow(record[x]);
x++;
}
conn.close();
}
catch (Exception ex)
{
ex.getStackTrace();
}
}
public void nieuwKlantenForm(){
rs01.setText("Auto");
rs02.setText("");
rs03.setText("");
rs04.setText("");
rs05.setText("");
rs06.setText("");
rs07.setText("");
rs08.setText("");
rs09.setText("");
rs10.setText("");
rs11.setText("");
rs10.setText("");
rs13.setText("");
rs14.setText("");
rs01.disable();
}
class KnpnieHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
try
{
nieuwKlantenForm();
}
catch (Exception ex)
{
ex.getStackTrace();
}
System.out.println("nieuw");
knp_delete.setEnabled( false );
knp_wijzigen.setEnabled( false );
}
}
class KnptoeHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection ("jdbc:mysql://localhost/voorraad",
"user", "passwoord");
Statement statement = conn.createStatement();
String s = rs13.getText();
int i13;
if (s == "") {i13 = 0;} else {i13 = Integer.parseInt(s);}
statement.executeUpdate("INSERT INTO klanten ( id , geslacht , achternaam ,
voornaam , straat , huisnr , postnr , gemeente , telefoon , GSM , fax , email ,
korting , nieuws ) VALUES (0, '"+rs02.getText()+"', '"+rs03.getText()+"',
'"+rs04.getText()+"', '"+rs05.getText()+"', '"+rs06.getText()+"',
'"+rs07.getText()+"', '"+rs08.getText()+"', '"+rs09.getText()+"',
'"+rs10.getText()+"', '"+rs11.getText()+"', '"+rs12.getText()+"', "+i13+",
'"+rs14.getText()+"')");
conn.close();
}
catch (Exception ex)
{
ex.getStackTrace();
System.out.println(ex);
}
System.out.println("INSERT"+rs13.getText());
vulTabel();
knp_delete.setEnabled(true);
knp_wijzigen.setEnabled(true);
}
}
class KnpwijHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection ("jdbc:mysql://localhost/voorraad",
"user", "passwoord");
Statement statement = conn.createStatement();
String s = rs01.getText();
int i01 = Integer.parseInt(s);
System.out.println(i01);
System.out.println(rs02.getText());
statement.executeUpdate("UPDATE klanten SET geslacht =
'"+rs02.getText()+"',achternaam = '"+rs03.getText()+"',voornaam =
'"+rs04.getText()+"',straat = '"+rs05.getText()+"',huisnr =
'"+rs06.getText()+"',postnr = '"+rs07.getText()+"',gemeente =
'"+rs08.getText()+"',telefoon = '"+rs09.getText()+"',GSM =
'"+rs10.getText()+"',fax = '"+rs11.getText()+"',email =
'"+rs12.getText()+"',korting = '"+rs13.getText()+"',nieuws =
'"+rs14.getText()+"' WHERE id = "+ i01 +" LIMIT 1");
conn.close();
}
catch (Exception ex)
{
ex.getStackTrace();
}
System.out.println("vullen");
vulTabel();
}
}
class KnpdelHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection ("jdbc:mysql://localhost/voorraad",
"user", "passwoord");
Statement statement = conn.createStatement();
String s = rs01.getText();
int i01 = Integer.parseInt(s);
statement.executeUpdate("DELETE FROM klanten WHERE id = "+i01);
System.out.println(i01);
conn.close();
}
catch (Exception ex)
{
ex.getStackTrace();
}
System.out.println("delete");
vulTabel();
nieuwKlantenForm();
}
}
public static void main(String args[])
{
new AdminKlanten().setVisible(true);
}
}
2.Code OpzoekenKlant.java
package src;
import javax.swing.*;
import java.awt.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.BoxLayout;
import javax.swing.JComboBox;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.event.*;
import java.awt.event.*; //import org.eclipse.swt.SWT;
//import org.eclipse.swt.widgets.Listener;
public class OpzoekenKlant extends JInternalFrame
{
public OpzoekenKlant()
{
super("Klant Opzoeken",true,true,true,true);
try {
//setDefaultCloseOperation(EXIT_ON_CLOSE);
//jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
final String[] columnNames = {"id","Familienaam","Voornaam","Straat","Nr","Postnr","Gemeente"};
final Object[][] record= new Object[1000][7];
try
{
System.out.println("start");
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection ("jdbc:mysql://localhost/voorraad",
"user", "passwoord");
Statement statement = conn.createStatement();
//ResultSet rs = statement.executeQuery("SELECT 'id','achternaam','voornaam','straat`,'huisnr','postnr'
FROM 'klanten' WHERE 'gemeente' = 'vleteren' LIMIT 0 , 30");
ResultSet rs = statement.executeQuery("SELECT * FROM `klanten`LIMIT 0 , 30");
int x;x=0;
while (rs.next()) {
record[x][0]=String.valueOf(rs.getInt("id"));
record[x][1]=rs.getString("achternaam");
record[x][2]=rs.getString("voornaam");
record[x][3]=rs.getString("straat");
record[x][4]=rs.getString("huisnr");
record[x][5]=rs.getString("postnr");
record[x][6]=rs.getString("gemeente");
x++;
}
conn.close();
}
catch (Exception ex)
{
ex.getStackTrace();
}
final DefaultTableModel myModel = new DefaultTableModel(record,columnNames )
{ //Gezien er geen foutenroutine voorzien is in dit voorbeeld zal de code hier vastlopen
//indien er geen database "voorraad" en tabel "klanten" bestaat
public Class getColumnClass(int c)
{
return getValueAt(0, c).getClass();
}
};
final JTable table = new JTable(myModel);
table.setSurrendersFocusOnKeystroke(true);
table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
JScrollPane jScrollPane = new JScrollPane();
jScrollPane.setViewportView(table);
Font font01 = new Font("Arial", Font.BOLD, 18);
Font font02 = new Font("Arial", Font.BOLD, 24);
JLabel lab01 = new JLabel("Criteria");
JLabel lab02 = new JLabel("Zoekitem");
JLabel lab03 = new JLabel("Info Klant opzoeken");
add(lab01);lab01.setFont(font01);lab01.setForeground(Color.blue);lab01.setBounds(18,
60, 100, 20);lab01.setHorizontalAlignment(SwingConstants.RIGHT);
add(lab02);lab02.setFont(font01);lab02.setForeground(Color.blue);lab02.setBounds(200,
60, 150, 20);lab02.setHorizontalAlignment(SwingConstants.RIGHT);
add(lab03);lab03.setFont(font02);lab03.setForeground(Color.darkGray);lab03.setBounds(36,
20, 250, 30);lab03.setHorizontalAlignment(SwingConstants.RIGHT);
final JTextField rs01 = new JTextField("");//zoek item
add(rs01);rs01.setBounds(270, 85, 150, 22);rs01.setFont(font01);
JButton knopZoek = new JButton("Zoek");
knopZoek.setBounds(440, 62, 80, 44);
knopZoek.setFont(font01);
add(knopZoek);
final JComboBox comboBox = new JComboBox();
comboBox.setName("JComboBox");
comboBox.setBounds(55,85,150,22);
comboBox.setPreferredSize(new java.awt.Dimension(100,22));
comboBox.addItem("");
comboBox.addItem("Id");
comboBox.addItem("achternaam");
comboBox.addItem("Voornaam");
comboBox.addItem("Straat");
comboBox.addItem("Huisnummer");
comboBox.addItem("Postnummer");
comboBox.addItem("Gemeente");
comboBox.setFont(font01);
add(comboBox);
for(int x = 0 ; x < 10 ; x++)
{
panel.add(new JLabel(" "));
}
knopZoek.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
int y = myModel.getRowCount();
for(int a = 0; a < y; a++)
{
myModel.removeRow(0);
}
String[] achter; achter = new String[7];
System.out.println(comboBox.getSelectedItem());
try
{
System.out.println("start");
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection ("jdbc:mysql://localhost/voorraad",
"user", "password");
Statement statement = conn.createStatement();
//ResultSet rs = statement.executeQuery("SELECT 'id','achternaam','voornaam','straat`,'huisnr','postnr'
FROM 'klanten' WHERE 'gemeente' = 'vleteren' LIMIT 0 , 30");
ResultSet rs = statement.executeQuery("SELECT * FROM klanten WHERE "+comboBox.getSelectedItem()+"
= '"+rs01.getText()+"' LIMIT 0 , 1000");
int x;x=0;
while (rs.next()) {
record[x][0]=String.valueOf(rs.getInt("id"));
record[x][1]=rs.getString("achternaam");
record[x][2]=rs.getString("voornaam");
record[x][3]=rs.getString("straat");
record[x][4]=rs.getString("huisnr");
record[x][5]=rs.getString("postnr");
record[x][6]=rs.getString("gemeente");
myModel.addRow(record[x]);
x++;
}
conn.close();
}
catch (Exception ex)
{
ex.getStackTrace();
}
}
}
);
panel.add(jScrollPane);
getContentPane().add(panel);
Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
setSize(new Dimension(800, 600));
setLocation((screenSize.width-800)/2,(screenSize.height-600)/2);
}
public void knopZoekActie(String val, Object crit, DefaultTableModel dtm){
Object[][] record= new Object[1000][7];
try
{
System.out.println("start");
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection ("jdbc:mysql://localhost/voorraad",
"user", "password");
Statement statement = conn.createStatement();
//ResultSet rs = statement.executeQuery("SELECT 'id','achternaam','voornaam','straat`,'huisnr','postnr'
FROM 'klanten' WHERE 'gemeente' = 'vleteren' LIMIT 0 , 30");
ResultSet rs = statement.executeQuery("SELECT * FROM `klanten`LIMIT 0 , 30");
int x;x=0;int y;y=0;
while (rs.next()) {
record[x][0]=String.valueOf(rs.getInt("id"));
record[x][1]=rs.getString("achternaam");
record[x][2]=rs.getString("voornaam");
record[x][3]=rs.getString("straat");
record[x][4]=rs.getString("huisnr");
record[x][5]=rs.getString("postnr");
record[x][6]=rs.getString("gemeente");
x++;
}
conn.close();
for (y=0;y<x;y++){dtm.addRow(record);}
}
catch (Exception ex)
{
ex.getStackTrace();
}
}
public static void main(String args[])
{
new OpzoekenKlant().setVisible(true);
}
}
3.Code VoorraadBeheer.java
package src;
import javax.swing.JDesktopPane; import javax.swing.JMenu; import javax.swing.JMenuItem; import javax.swing.JMenuBar; import javax.swing.JFrame; import javax.swing.KeyStroke; import java.awt.event.*; import java.awt.*;
public class VoorraadBeheer extends JFrame implements ActionListener {
JDesktopPane desktop;
public VoorraadBeheer() { super("Voorraadbeheer");
//50 pixels van rand scherm
int inset = 50; Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); setBounds(inset, inset, screenSize.width - inset*2, screenSize.height - inset*2);
desktop = new JDesktopPane(); //a specialized layered pane setContentPane(desktop); setJMenuBar(createMenuBar()); desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE); }
protected JMenuBar createMenuBar() { JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("Administratie"); menu.setMnemonic(KeyEvent.VK_A); menuBar.add(menu);
JMenuItem menuItem01 = new JMenuItem("Producten"); menuItem01.setMnemonic(KeyEvent.VK_P); menuItem01.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P, ActionEvent.ALT_MASK)); menuItem01.setActionCommand("adminpro"); menuItem01.addActionListener(this); menu.add(menuItem01);
JMenuItem menuItem02 = new JMenuItem("Klanten"); menuItem02.setMnemonic(KeyEvent.VK_K); menuItem02.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_K, ActionEvent.ALT_MASK)); menuItem02.setActionCommand("adminkla"); menuItem02.addActionListener(this); menu.add(menuItem02); JMenuItem menuItem03 = new JMenuItem("Leveranciers"); menuItem03.setMnemonic(KeyEvent.VK_L); menuItem03.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L, ActionEvent.ALT_MASK)); menuItem03.setActionCommand("adminlev"); menuItem03.addActionListener(this); menu.add(menuItem03); JMenuItem menuItem04 = new JMenuItem("Klant opzoeken"); menuItem04.setMnemonic(KeyEvent.VK_A); menuItem04.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, ActionEvent.ALT_MASK)); menuItem04.setActionCommand("adminzoek"); menuItem04.addActionListener(this); menu.add(menuItem04); JMenuItem menuItem05 = new JMenuItem("Printbeheer"); menuItem05.setMnemonic(KeyEvent.VK_P); menuItem05.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P, ActionEvent.ALT_MASK)); menuItem05.setActionCommand("adminprint"); menuItem05.addActionListener(this); menu.add(menuItem05); JMenu menu10 = new JMenu("Voorraad"); menu.setMnemonic(KeyEvent.VK_V); menuBar.add(menu10);
JMenuItem menuItem11 = new JMenuItem("Producten IN"); menuItem11.setMnemonic(KeyEvent.VK_I); menuItem11.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_I, ActionEvent.ALT_MASK)); menuItem11.setActionCommand("Prodin"); menuItem11.addActionListener(this); menu10.add(menuItem11);
JMenuItem menuItem12 = new JMenuItem("Producten UIT"); menuItem12.setMnemonic(KeyEvent.VK_U); menuItem12.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_U, ActionEvent.ALT_MASK)); menuItem12.setActionCommand("produit"); menuItem12.addActionListener(this); menu10.add(menuItem12); JMenu menu20 = new JMenu("Fakturatie"); menu.setMnemonic(KeyEvent.VK_F); menuBar.add(menu20);
JMenuItem menuItem21 = new JMenuItem("Kostprijs"); menuItem21.setMnemonic(KeyEvent.VK_K); menuItem21.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_K, ActionEvent.ALT_MASK)); menuItem21.setActionCommand("kost"); menuItem21.addActionListener(this); menu20.add(menuItem21);
JMenuItem menuItem22 = new JMenuItem("Rekening"); menuItem22.setMnemonic(KeyEvent.VK_R); menuItem22.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, ActionEvent.ALT_MASK)); menuItem22.setActionCommand("rek"); menuItem22.addActionListener(this); menu20.add(menuItem22); JMenu menu50 = new JMenu("Systeem"); menu.setMnemonic(KeyEvent.VK_S); menuBar.add(menu50); JMenuItem menuItem51 = new JMenuItem("Quit"); menuItem51.setMnemonic(KeyEvent.VK_Q); menuItem51.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, ActionEvent.ALT_MASK)); menuItem51.setActionCommand("quit"); menuItem51.addActionListener(this); menu50.add(menuItem51);
return menuBar; }
public void actionPerformed(ActionEvent e) { if ("adminpro".equals(e.getActionCommand())) { //new maakProductAdmin(); } else if ("adminkla".equals(e.getActionCommand())) { maakKlantenAdmin(); } else if ("adminlev".equals(e.getActionCommand())) { maakLeveranciersAdmin(); } else if ("adminzoek".equals(e.getActionCommand())) { maakKlantOpzoeken(); } else { //quit quit(); } }
protected void maakProductAdmin() { //AdminProducten frame01 = new AdminProducten(); // frame01.setVisible(true); //necessary as of 1.3 // desktop.add(frame01); // try { // frame01.setSelected(true); // } catch (java.beans.PropertyVetoException e) {} } protected void maakKlantenAdmin() { AdminKlanten frame02 = new AdminKlanten(); frame02.setVisible(true); desktop.add(frame02); try { frame02.setSelected(true); } catch (java.beans.PropertyVetoException e) {} } protected void maakLeveranciersAdmin() { //AdminLeveranciers frame03 = new AdminLeveranciers(); //frame03.setVisible(true); //desktop.add(frame03); //try { // frame03.setSelected(true); //} catch (java.beans.PropertyVetoException e) {} }
protected void maakKlantOpzoeken() { OpzoekenKlant frame04 = new OpzoekenKlant(); frame04.setVisible(true); desktop.add(frame04); try { frame04.setSelected(true); } catch (java.beans.PropertyVetoException e) {} } //Beêindig toepessing. protected void quit() { System.exit(0); }
/** * Maak hoofdraam */ private static void createAndShowGUI() {
JFrame.setDefaultLookAndFeelDecorated(true); VoorraadBeheer frame = new VoorraadBeheer(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } }
Run
VoorraadBeheer.java als
java application
- dit project is slechts een basis voorbeeld
gezien het de uiteindelijke bedoeling is om met hibernate 3 te werken.
- De volgende stap in dit voolbeeld die we
volgen is : het object klant maken
- Ook is het aan te raden om in een definitief
project met meer duidelijke benamingen te weken
- bvb ipv lab01 » labelKlantID te gebruiken
enz...
zie : Aanmaken object klant : Klant;java
|