Guten Abend allerseits,
ich habe folgendes Problem:
In der Klasse Bestellung habe ich ein Fenster (gui aus netbeans), welches per Button auf eine Datenbank zugreifen soll. Zu meiner Frage interessiert vorerst eigentlich nur die Gesamtabfrage. Es handelt sich um Bestellungen, die bis zu 4 Positionen aufweisen können (Tabelle Bestellung und Tabelle Position). In der Klasse Dball2 befindet sich die Abfrage selber, die eigentlich meines Wissens keine Fehler aufweisen dürfte. Ich denke, der Fehler liegt irgendwo bei dem Aufruf in Bestellung. Komischerweise lässt sich alles kompilieren. Wenn ich auf den Button jButton5 (Alle Anzeigen) klicke wird aber keine Tabelle angezeigt. Warum? Ich bin verzweifelt und für ejde Hilfe dankbar. Nachfolgend der Code.
gruß pazi
Abfrage-Klasse:
Fenster mit Aufruf der Abfrage (Button Zeile 86, Aufruf der Abfrage Zeile 297):
ich habe folgendes Problem:
In der Klasse Bestellung habe ich ein Fenster (gui aus netbeans), welches per Button auf eine Datenbank zugreifen soll. Zu meiner Frage interessiert vorerst eigentlich nur die Gesamtabfrage. Es handelt sich um Bestellungen, die bis zu 4 Positionen aufweisen können (Tabelle Bestellung und Tabelle Position). In der Klasse Dball2 befindet sich die Abfrage selber, die eigentlich meines Wissens keine Fehler aufweisen dürfte. Ich denke, der Fehler liegt irgendwo bei dem Aufruf in Bestellung. Komischerweise lässt sich alles kompilieren. Wenn ich auf den Button jButton5 (Alle Anzeigen) klicke wird aber keine Tabelle angezeigt. Warum? Ich bin verzweifelt und für ejde Hilfe dankbar. Nachfolgend der Code.
gruß pazi
Abfrage-Klasse:
Code:
import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.Vector;
import javax.swing.JTable;
public class Dball2 {
public JTable table;
public ResultSet rst;
/** Creates a new instance of Datenanbindung */
public void selcall(String ort,String ort2,String joinfeld) {
Vector columnNames = new Vector();
Vector data = new Vector();
try{
//Abfrage alles anzeigen
String query = "SELECT * FROM "+ort2+","+ort+" WHERE "+ort2+"."+joinfeld+" = "+ort+"."+joinfeld+";";
//Datenbankverbindung herstellen
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection( "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=D:/InMa/InmaBuchhandlung.mdb","","" );
System.out.println("Connection Successful ");
Statement stmt = con.createStatement();
ResultSet rst = stmt.executeQuery(query);
ResultSetMetaData md = rst.getMetaData();
int columns = md.getColumnCount();
// Spaltennamen ermitteln
for (int i = 1; i <= columns; i++) {
String colname=md.getColumnName(i);
columnNames.addElement( colname );
}
// Zeileninhalt ermitteln
while (rst.next()) {
Vector row = new Vector(columns);
for (int i = 1; i <= columns; i++) {
row.addElement( rst.getObject(i) );
}
data.addElement( row );
}
rst.close();
stmt.close();
} catch(Exception e) {
e.printStackTrace();
}
// Tabelle erzeugen
table = new JTable(data, columnNames);
}
public JTable rückantw()
{
return this.table;
}
public ResultSet rückrst()
{
return this.rst;
}
}
Fenster mit Aufruf der Abfrage (Button Zeile 86, Aufruf der Abfrage Zeile 297):
Code:
import java.sql.SQLException;
public class Bestellung extends javax.swing.JFrame {
/** Creates new form Bestellung */
public Bestellung() throws SQLException{
initComponents();
}
/** 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.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jPanel1 = new javax.swing.JPanel();
jButton1 = new javax.swing.JButton();
jSeparator1 = new javax.swing.JSeparator();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
jLabel7 = new javax.swing.JLabel();
jButton2 = new javax.swing.JButton();
jLabel8 = new javax.swing.JLabel();
jLabel9 = new javax.swing.JLabel();
bestellnummerF = new javax.swing.JTextField();
isbn1F = new javax.swing.JTextField();
isbn2F = new javax.swing.JTextField();
isbn3F = new javax.swing.JTextField();
isbn4F = new javax.swing.JTextField();
bestelldatumF = new javax.swing.JTextField();
kundennummerF = new javax.swing.JTextField();
anzahl1F = new javax.swing.JSpinner();
anzahl2F = new javax.swing.JSpinner();
anzahl3F = new javax.swing.JSpinner();
anzahl4F = new javax.swing.JSpinner();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
jLabel1.setText("Bestellung");
jLabel2.setText("BestellNr.:");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 456, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 142, Short.MAX_VALUE)
);
jButton1.setText("Anzeigen");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jLabel4.setText("KundenNr.:");
jLabel5.setText("ISBN");
jLabel6.setText("BestellDatum");
jLabel7.setText("BestellNr.:");
jButton2.setText("Bestellung Aufnehmen");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jLabel8.setText("Bestellungen Anzeigen");
jButton5.setText("Alle Anzeigen");
jButton5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton5ActionPerformed(evt);
}
});
jButton3.setText("Bearbeiten");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
try {
jButton3ActionPerformed(evt );
} catch (SQLException e) {
e.printStackTrace();
}
}
});
jButton4.setText("L\u00f6schen");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel2)
.addGap(30, 30, 30)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton1)
.addGap(20, 20, 20)
.addComponent(jButton5))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 456, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel5)
.addGroup(layout.createSequentialGroup()
.addGap(10, 10, 10)
.addComponent(jLabel4)))
.addGap(19, 19, 19)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(isbn2F)
.addComponent(kundennummerF, javax.swing.GroupLayout.DEFAULT_SIZE, 95, Short.MAX_VALUE)
.addComponent(isbn1F, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(isbn3F)
.addComponent(isbn4F))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(43, 43, 43)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel6)
.addComponent(jLabel7))
.addGap(12, 12, 12)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(bestellnummerF)
.addComponent(bestelldatumF, javax.swing.GroupLayout.DEFAULT_SIZE, 106, Short.MAX_VALUE)))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 0, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(180, 180, 180))))
.addGroup(layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(anzahl2F, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(71, 71, 71)
.addComponent(jButton2))
.addComponent(anzahl1F, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(anzahl3F, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(anzahl4F, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))))
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 32, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addGap(30, 30, 30)
.addComponent(jButton3)
.addGap(32, 32, 32)
.addComponent(jButton4))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(kundennummerF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel6)
.addComponent(bestelldatumF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel4))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel7)
.addComponent(bestellnummerF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel9)
.addGap(32, 32, 32)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(isbn1F, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel5)
.addComponent(anzahl1F, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(5, 5, 5)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(isbn2F, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(anzahl2F, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton2))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(isbn3F, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(anzahl3F, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(isbn4F, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(anzahl4F, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(43, 43, 43)
.addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 12, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel8)
.addGap(30, 30, 30)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(jButton1)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton5))
.addGap(14, 14, 14)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton3)
.addComponent(jButton4))
.addContainerGap(30, Short.MAX_VALUE))
);
pack();
}// </editor-fold> //GEN-END:initComponents
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
SqlDel2 del = new SqlDel2();
BestWerte com = new BestWerte();
String ort ="Bestellung";
String ort2 ="Position";
del.delete(ort,ort2,jTextField1.getText());
}//GEN-LAST:event_jButton3ActionPerformed
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) throws SQLException {//GEN-FIRST:event_jButton2ActionPerformed
new BestBe().setVisible(true);
}//GEN-LAST:event_jButton2ActionPerformed
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
DatenanbindungBest sqlInsert = new DatenanbindungBest ();
String bestellnummer = bestellnummerF.getText();
String isbn1 = isbn1F.getText();
String isbn2 = isbn2F.getText();
String isbn3 = isbn3F.getText();
String isbn4 = isbn4F.getText();
String bestelldatum = bestelldatumF.getText();
String kundennummer = kundennummerF.getText();
String positionsnummer1 = "1"+bestellnummer;
String positionsnummer2 = "2"+bestellnummer;
String positionsnummer3 = "3"+bestellnummer;
String positionsnummer4 = "4"+bestellnummer;
String anzahl1 = (String) anzahl1F.getValue();
String anzahl2 = (String) anzahl2F.getValue();
String anzahl3 = (String) anzahl3F.getValue();
String anzahl4 = (String) anzahl4F.getValue();
String ort = "Bestellung";
String ort2 = "Position";
sqlInsert.insert(ort,ort2,bestellnummer,isbn1,isbn2,isbn3,isbn4,bestelldatum,kundennummer,positionsnummer1,positionsnummer2,positionsnummer3,positionsnummer4,anzahl1,anzahl2,anzahl3,anzahl4);
}//GEN-LAST:event_jButton1ActionPerformed
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
DatenanbindungBest sqlAbfrage = new DatenanbindungBest();
BestWerte ich = new BestWerte();
System.out.println(jTextField1.getText());
//Object jcom = jComboBox1.getSelectedItem();
//String comBox = (String) jcom;
//String ort = "Bücher";
ich.setTextF (jTextField1.getText());
//ich.setTextB (jTextField9.getText());
//String textF = jTextField9.getText();
sqlAbfrage.abfrage(ich.getTextF(),ich.getOrt());
BestellungTable.setViewportView(sqlAbfrage.rückantw());
}//GEN-LAST:event_jButton5ActionPerformed
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
Dball2 zeigeAll = new Dball2();
String joinfeld="Bestellnummer";
String ort = "Bestellung";
String ort2 = "Position";
zeigeAll.selcall(ort,ort2,joinfeld);
BestellungTable.setViewportView(zeigeAll.rückantw());
}//GEN-LAST:event_jButton4ActionPerformed
public javax.swing.JScrollPane scrollTabelle;
public javax.swing.JTable Tab;
/**
* @param args the command line arguments
*/
// Variables declaration - do not modify//GEN-BEGIN:variables
public javax.swing.JScrollPane BestellungTable;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JLabel jLabel9;
private javax.swing.JTextField bestellnummerF;
private javax.swing.JTextField isbn1F;
private javax.swing.JTextField isbn2F;
private javax.swing.JTextField isbn3F;
private javax.swing.JTextField isbn4F;
private javax.swing.JTextField bestelldatumF;
private javax.swing.JTextField kundennummerF;
private javax.swing.JTextField positionsnummer1F;
private javax.swing.JTextField positionsnummer2F;
private javax.swing.JTextField positionsnummer3F;
private javax.swing.JTextField positionsnummer4F;
private javax.swing.JSpinner anzahl1F;
private javax.swing.JSpinner anzahl2F;
private javax.swing.JSpinner anzahl3F;
private javax.swing.JSpinner anzahl4F;
private javax.swing.JPanel jPanel1;
private javax.swing.JSeparator jSeparator1;
private javax.swing.JTextField jTextField1;
// End of variables declaration//GEN-END:variables
}