import javax.swing.*;
import javax.swing.JTable.PrintMode;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class KundeSuchen extends JPanel
implements ActionListener
{
private DvdGui parent;
private JTextField tf1 = new JTextField ();
private JTextField tf2 = new JTextField ();
private JLabel l1 = new JLabel("Nachname eingeben: ");
private JLabel l2 = new JLabel("Vorname eingeben: ");
private JLabel l7 = new JLabel(" Kunden - Suche ");
private JButton btnSU = new JButton(" Suchen ");
private JButton btnNEW = new JButton(" Neu ");
private JButton btnABBRECHEN = new JButton(" Schließen ");
private String s1 = "";
private String s2 = "";
private JButton btschließen = new JButton(" Schließen ");
private JButton btsuche_neu = new JButton(" Neue Suche ");
private JButton btsuche = new JButton(" Suchen ");
private JTextField suche = new JTextField("Bitte Suche eingeben...");
private JLabel überschrift = new JLabel("Kunde suchen...");
private JLabel Fehler = new JLabel("");
private String ssuche = "";
private String kunde[][];
Font f = new Font("Comic Sans MS", Font.ITALIC, 16);
Font f1 = new Font("Comic Sans MS", Font.ITALIC, 22);
String columnNames[] ={"Kunden-ID", "Name", "Vorname", "Straße", "Hausnummer","PLZ","Ort","Telefonnummer","e-Mailadresse","Geburtsdatum","Personalausweis-ID"};
DefaultTableModel model = new DefaultTableModel(kunde, columnNames)
{
public boolean isCellEditable(int row, int col)
{
return(true);
}
};
private JTable tablesuche = new JTable( model );
public KundeSuchen(DvdGui parent)
{
this.parent = parent;
setLayout(null);
//tablesuche.setBounds (new Rectangle(100,300,800,300));
tf1.setBounds (new Rectangle(500,180,250,21));
tf2.setBounds (new Rectangle(500,200,250,21));
l7.setBounds (new Rectangle(375,80,250,25));
Fehler.setBounds (new Rectangle(20,550,700,21));
l1.setBounds (new Rectangle(250,180,210,21));
l2.setBounds (new Rectangle(250,200,210,21));
btnSU.setBounds (new Rectangle(300,250,120,21));
btnNEW.setBounds (new Rectangle(450,250,120,21));
btnABBRECHEN.setBounds (new Rectangle(600,250,120,21));
Font f = new Font("Comic Sans MS", Font.ITALIC, 16);
l1.setFont(f);
l2.setFont(f);
Font f1 = new Font("Comic Sans MS", Font.ITALIC, 22);
l7.setFont(f1);
add(l1);
add(l2);
add(l7);
add(tf1);
add(tf2);
add(btnSU);
add(btnNEW);
add(btnABBRECHEN);
add(Fehler);
btnSU.addActionListener(this);
btnNEW.addActionListener(this);
btnABBRECHEN.addActionListener(this);
tablesuche.getTableHeader().setReorderingAllowed(false);
tablesuche.getTableHeader().setResizingAllowed(false);
JScrollPane scrollbar = new JScrollPane(tablesuche);
scrollbar.setViewportView(tablesuche);
tablesuche.setModel(model);
scrollbar.setVisible(true);
scrollbar.setBounds(50,300,900,300);
add(scrollbar);
// add(tablesuche);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()== btnABBRECHEN)
{
parent.jtp.remove(this);
parent.pack();
parent.setExtendedState(JFrame.MAXIMIZED_BOTH);
}
if(e.getSource()== btnNEW)
{
tf1.setText("");
tf2.setText("");
}
if(e.getSource()== btnSU){
Connection conn = null; //Verbindung zum Server
Statement stmt = null; //die abzusetzende SQL-Anweisung
ResultSet rs = null; //die empfangenen Daten
int index1 = 0;
//ResultSet rs2 = null; //die empfangenen Daten
try{
s1 = tf1.getText();
s2 = tf2.getText();
}
catch (Exception ex)
{
Fehler.setText("Fehler: " + ex.toString());
}
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
Fehler.setText("Treiber OK.");
conn = DriverManager.getConnection("jdbc:mysql://10.30.0.201/dvdverleih?user=software&password=verleih");
Fehler.setText("Verbindung erhalten: " + conn.toString());
}
catch (Exception exe)
{
Fehler.setText("Fehler beim Verbindungsaufbau!" + exe.toString());
}
try
{
stmt = conn.createStatement();
if(s1==null){ s1 = "%";}
if(s2==null){ s2 = "%";}
rs = stmt.executeQuery("SELECT * FROM kunde WHERE name LIKE '%"+s1+"%' AND vorname LIKE '%"+s2+"%';");
rs.last();
int rows = rs.getRow();
rs.beforeFirst();
kunde = new String[rows][11];
//System.out.println(rows);
while (rs.next()){
// Alle Kundenangaben laufen hier durch
for(int i=0;i<11;i++)
{
kunde[index1][i] = rs.getString(i+1);
System.out.println(kunde[index1][i]);
}
index1++;
}
model.setDataVector(kunde, columnNames);
model.fireTableDataChanged();
}
catch(Exception exep)
{
Fehler.setText("Fehler beim Suchen: " + exep.toString());
}
}
}
}