# Jtable mit sql befehl in java füllen auf knopfdruck



## Cybertronnixxx (14. Februar 2014)

Hei ich habe ein programm in arbeit was die fehlstunden verwalten soll  ich habe dazu eine gui und eine datenbank  mein problem ist nun ich brauche die daten aus der datenbank auf knopfdruck in einer Jtable auf der Gui  wie kann ich das implementieren ? würde mich über eure hilfe freuen   Hier noch der quell code 


Gui quellcode:


```
package guifürfehlstunden;

/**
 *
 * @author Cyber
 */
public class Gui extends javax.swing.JFrame {
 private Connectiontodb con;
    /**
     * Creates new form Interface
     */
    public Gui() {
        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.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        Connectbutton = new javax.swing.JButton();
        fehlstunden = new javax.swing.JButton();
        anzahlfehlstent = new javax.swing.JButton();
        akutefehlst = new javax.swing.JButton();
        jButton7 = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jPasswordField1 = new javax.swing.JPasswordField();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        scroll1 = new javax.swing.JScrollBar();
        jPanel2 = new javax.swing.JPanel();
        entschuldigen = new javax.swing.JButton();
        jTextField2 = new javax.swing.JTextField();
        bearbeiten = new javax.swing.JButton();
        jTextField3 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane4 = new javax.swing.JScrollPane();
        jTable3 = new javax.swing.JTable();
        scroll2 = new javax.swing.JScrollBar();
        jScrollPane5 = new javax.swing.JScrollPane();
        jTable4 = new javax.swing.JTable();
        scroll3 = new javax.swing.JScrollBar();
        anzahlfehlstunent = new javax.swing.JButton();
        jScrollPane6 = new javax.swing.JScrollPane();
        jTextArea2 = new javax.swing.JTextArea();
        Login = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();

        jTable2.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"
            }
        ));
        jScrollPane1.setViewportView(jTable2);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        Connectbutton.setText("Connect");
        Connectbutton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ConnectbuttonActionPerformed(evt);
            }
        });

        fehlstunden.setText("Fehlstunden");
        fehlstunden.setToolTipText("");
        fehlstunden.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fehlstundenActionPerformed(evt);
            }
        });

        anzahlfehlstent.setText("Anzahl an Fehlstunden");
        anzahlfehlstent.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                anzahlfehlstentActionPerformed(evt);
            }
        });

        akutefehlst.setText("Akute Fehlstunden");
        akutefehlst.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                akutefehlstActionPerformed(evt);
            }
        });

        jButton7.setText("Log Out");
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });

        jTextField1.setText("ID");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null}
            },
            new String [] {
                "Name", "Fach", "Lehrer", "Datum", "Woche", "Status"
            }
        ));
        jScrollPane2.setViewportView(jTable1);

        jPasswordField1.setText("jPasswordField1");

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane3.setViewportView(jTextArea1);

        jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        entschuldigen.setText("Entschuldigen");
        entschuldigen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                entschuldigenActionPerformed(evt);
            }
        });

        jTextField2.setText("Eingabe");

        bearbeiten.setText("Bearbeiten");
        bearbeiten.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bearbeitenActionPerformed(evt);
            }
        });

        jTextField3.setText("Eingabe");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(14, 14, 14)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(bearbeiten, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(entschuldigen, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jTextField2)
                    .addComponent(jTextField3, javax.swing.GroupLayout.DEFAULT_SIZE, 261, Short.MAX_VALUE))
                .addContainerGap(49, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(entschuldigen)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(30, 30, 30)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bearbeiten)
                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(173, Short.MAX_VALUE))
        );

        jLabel1.setBackground(new java.awt.Color(153, 153, 153));
        jLabel1.setText("Bearbeitung von Fehlstunden");

        jLabel2.setText("Schülerliste");

        jTable3.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "Schüler", "Kurs", "Tutor"
            }
        ));
        jScrollPane4.setViewportView(jTable3);

        jTable4.setForeground(new java.awt.Color(255, 0, 0));
        jTable4.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "Schüler", "Datum", "Kurs", "Status", "Lehrer"
            }
        ));
        jScrollPane5.setViewportView(jTable4);

        anzahlfehlstunent.setText("Anzahl unentschuldigter Fehlstunden");

        jTextArea2.setColumns(20);
        jTextArea2.setForeground(new java.awt.Color(255, 0, 0));
        jTextArea2.setRows(5);
        jScrollPane6.setViewportView(jTextArea2);

        Login.setText("Login");

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel3.setText("                                                                                                                        Fehlstunden Verwaltung");
        jLabel3.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        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)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 291, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(75, 75, 75)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 317, Short.MAX_VALUE))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(scroll2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(94, 94, 94)
                                .addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, 281, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(116, 116, 116)
                                .addComponent(akutefehlst, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(scroll3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(40, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(fehlstunden, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(anzahlfehlstunent, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 237, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jScrollPane6, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 237, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(Connectbutton)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 193, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(Login)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jButton7))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 555, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(scroll1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addGap(41, 41, 41)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE)
                                    .addComponent(anzahlfehlstent, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                        .addGap(182, 182, 182))))
            .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(Connectbutton)
                            .addComponent(jButton7)
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(Login))
                        .addGap(42, 42, 42)
                        .addComponent(fehlstunden, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(anzahlfehlstent)
                        .addComponent(anzahlfehlstunent)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(4, 4, 4)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 207, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(scroll1, javax.swing.GroupLayout.PREFERRED_SIZE, 207, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane6, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 357, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(akutefehlst)
                            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(scroll2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 338, Short.MAX_VALUE)
                    .addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addComponent(scroll3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(85, 85, 85))
        );

        pack();
    }// </editor-fold>                        

    private void ConnectbuttonActionPerformed(java.awt.event.ActionEvent evt) {                                              
        // TODO add your handling code here:
        con = new Connectiontodb();
        con.sqlAbfrageAusfuehren();
    }                                             

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
    }                                        

    private void fehlstundenActionPerformed(java.awt.event.ActionEvent evt) {                                            
        // TODO add your handling code here:
        
        /*
         sql-abfrage von allen Daten der Tabelle fehlst der Datenbank db
         * 
         * Select* From fehlst
         */
        String query = "Select * From fehlst";
    }                                           

    private void anzahlfehlstentActionPerformed(java.awt.event.ActionEvent evt) {                                                
        // TODO add your handling code here:
        
        /*
         sql-statement in dem gezählt wird wieviele Fehlstunden der User Besitzt
         */
    }                                               

    private void akutefehlstActionPerformed(java.awt.event.ActionEvent evt) {                                            
        // TODO add your handling code here:
    }                                           

    private void entschuldigenActionPerformed(java.awt.event.ActionEvent evt) {                                              
        // TODO add your handling code here:
    }                                             

    private void bearbeitenActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:
    }                                          

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus 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 ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Gui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Gui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Gui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Gui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Gui().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                     
    private javax.swing.JButton Connectbutton;
    private javax.swing.JButton Login;
    private javax.swing.JButton akutefehlst;
    private javax.swing.JButton anzahlfehlstent;
    private javax.swing.JButton anzahlfehlstunent;
    private javax.swing.JButton bearbeiten;
    private javax.swing.JButton entschuldigen;
    private javax.swing.JButton fehlstunden;
    private javax.swing.JButton jButton7;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPasswordField jPasswordField1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JScrollPane jScrollPane6;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTable2;
    private javax.swing.JTable jTable3;
    private javax.swing.JTable jTable4;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextArea jTextArea2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JScrollBar scroll1;
    private javax.swing.JScrollBar scroll2;
    private javax.swing.JScrollBar scroll3;
    // End of variables declaration                   
}
```



Connectiontodb quellcode :


```
package guifürfehlstunden;

/**
 *
 * @author Cyber
 */
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import abiturklassen.listenklassen.List;

public class Connectiontodb {
    
    int attributAnzahl;//Anzahl der Spalten
    int datensatzAnzahl;//Anzahl der Zeilen
    List attributNamen;//Liste der Spaltenueberschriften
    List attributWerte;//Achtung: Liste von Listen, die die Attributwerte
                       //         fuer jeweils einen Datensatz enthalten.
    String query;//SQL-Abfrage
    
    /**
     * Initialisiert die Datenspeicher fuer den Inhalt 
     * einer Tabelle einer Datenbank.
     */
    public Connectiontodb()
    {
        attributAnzahl=0;
        datensatzAnzahl=0;
        attributNamen=new List();
        attributWerte=new List();
        //Abfrage definieren - Hier kann beliebiger SQL-Code stehen,
        //der auch über das Abfrage-Fenster von Netbeans funktioniert.
        query = "SELECT DISTINCT * FROM fehlst ORDER BY Username";
    }
    
    public void setzeSQLAbfrage(String sql)
    {
        query=sql;
    }
    
    /**
     * Fuellt die Liste attributNamen mit dem Spaltenueberschriften
     * der Tabelle table der DB db und fuellt die Liste attributWerte
     * mit den entsprechenden Werten aller Datensaetze der Tabelle,
     * wobei jede Tabellenzeile wieder in einer eigenen Liste steht.
     * @param db Name der DB
     * @param table Name der Tabelle
     */
    public void sqlAbfrageAusfuehren() {
             
        try{
                
            //Datenbankverbindung herstellen
                        
            String driver = "org.apache.derby.jdbc.EmbeddedDriver";
            //Für MS-Access: (dazu muss noch eine JdbcOdbc-Datenquelle 
            //über die Systemsteuerung eingerichtet werden)
            //String driver="sun.jdbc.odbc.JdbcOdbcDriver";
            String connectionURL = "jdbc:derby:C:/Users/pc/Desktop/ProjektFehlstunden/db";
            Class.forName(driver);
            Connection con=DriverManager.getConnection(connectionURL,null,null );
            System.out.println("Connected to Database...");
            
            //SQL-Statement-Objekt wird vorbereitet
            Statement stmt = con.createStatement();
            //Ein ResultSet-Objekt (ErgebnisMenge) nimmt das Ergebnis der
            //SQL-Abfrage aus dem String query auf:
            ResultSet rst = stmt.executeQuery(query);
            //Ein ResultSetMetaData-Objekt nimmt die "Rahmendaten" der Tabelle
            //z.B. hier Spaltenüberschriften auf:
            ResultSetMetaData md = rst.getMetaData();
            
            attributAnzahl = md.getColumnCount();
            //  Spaltennamen ermitteln
            for (int i = 1; i <=attributAnzahl; i++) {
                String spaltenueberschrift=md.getColumnName(i);
                attributNamen.append( spaltenueberschrift );
            }
            //  Zeileninhalt ermitteln
            while (rst.next()) {
                datensatzAnzahl++;
                List datensatz = new List();
                for (int i = 1; i<=attributAnzahl; i++) {
                    datensatz.append( rst.getObject(i) );
                }
                attributWerte.append( datensatz );
            }
            rst.close();
            stmt.close();
            con.close();
            
            //Testausgabe
            String tmp;
            attributNamen.toFirst();
            tmp="";
            while (attributNamen.hasAccess()) 
            {
                tmp+=" "+((String) attributNamen.getObject());
                attributNamen.next();
            }
            //System.out.println(tmp);
            attributWerte.toFirst();
            while (attributWerte.hasAccess()) {
               
                tmp="";
                List datensatz = (List) attributWerte.getObject();
                datensatz.toFirst();
                while (datensatz.hasAccess()) 
                {
                    tmp+=" "+((String)datensatz.getObject());
                    datensatz.next();
                }
                //System.out.println(tmp);
                attributWerte.next();
                
            }
        } catch(Exception e) {
            e.printStackTrace();
        }    
    }
    
    public String gibSpaltenüberschriften()
    {
        String returnString="";
        attributNamen.toFirst();
        while (attributNamen.hasAccess()) 
        {
            returnString= returnString + " " +(String) attributNamen.getObject();
            attributNamen.next();
        }
        //Testausgabe
        System.out.println(returnString);
        return returnString;
    }
    
    public String gibNaechstenDatensatz()
    {
        String returnString="";
        List datensatz;
        if (attributWerte.hasAccess()) 
            datensatz = (List) attributWerte.getObject();
        else return returnString;
        
        datensatz.toFirst();
        while (datensatz.hasAccess())
        {
            returnString= returnString + " " + (String)datensatz.getObject();
            datensatz.next();
            //Testausgabe
            System.out.println(returnString);
        }
        attributWerte.next();
        return returnString;
    }
    
    public void setzeAufErstenDatensatz()
    {
        attributWerte.toFirst();
    }

    
}


Vielen dank im voraus :) Cyber :)

edit: das ganze ist in netbeans implementiert worden :)
```


----------



## sheel (15. Februar 2014)

Hi

ohne die ~600 Zeilen Code angeschaut zu haben:
Wo genau liegt das Problem?
Keine Ahnung von der Vorgehensweise (DB-Verbindung, Db-Abfrage in Java, SQL, Tablemodel...?)
oder irgendwelche Fehlermeldungen (welche) oder...?


----------



## Cybertronnixxx (15. Februar 2014)

mein Problem besteht darin das ich keine ahnung habe wie ich auf knopfdruck die Jtable mit den daten füllen kann  ich bin soweit gekommen schon das ich  es geschafft habe wie gesagt die datensätze in einen query zu speichern  Problem jetzt nur wie kriege ich die da per knopfdruck raus und in eine jtable auf der gui


----------



## youza (17. Februar 2014)

Hallo Cypertronnixxx,

hab mir deinen Code mal so ein bißchen angeschaut. Ohne die genaue Implementierung der von:

```
abiturklassen.listenklassen.List;
```

Kann ich nur raten.
In der Database Connection Klasse brauchst du auf jeden fall für die Attribute einen Getter:


```
public int getAttributAnzahl() {
		return attributAnzahl;
	}

	public List getAttributNamen() {
		return attributNamen;
	}

	public List getAttributWerte() {
		return attributWerte;
	}
```

Und jetzt ein so ungefähr implementierung deiner fehlstundenActionPerform:


```
private void fehlstundenActionPerformed(java.awt.event.ActionEvent evt) {                                            
        // TODO add your handling code here:
        
        /*
         sql-abfrage von allen Daten der Tabelle fehlst der Datenbank db
         * 
         * Select* From fehlst
         */
        String query = "Select * From fehlst";
        con.setzeSQLAbfrage(query);
        List attributes =  con.getAttributWerte();
        String columnNames[] = new String [] { "Name", "Fach", "Lehrer", "Datum", "Woche", "Status"  };
        String data[][] = new String[attributes.size()][columnNames.length];
        for (int i = 0; i < attributes.size(); i++) {
        	String datensatz = attributes.get(i);
			for (int j = 0; j < columnNames.length; j++) {
				data[i][j] = datensatz.split(";")[j];
			}
		}
        jTable1.setModel(new javax.swing.table.DefaultTableModel(
               data,columnNames
            ));
        
    }
```

Also besonderst folgender Teil ist komplett geraten:

```
String datensatz = attributes.get(i);
			for (int j = 0; j < columnNames.length; j++) {
				data[i][j] = datensatz.split(";")[j];
			}
```

Aber ich denke mit dem Ansatz kannst du weiterarbeiten.

Viele Grüße
Youza


----------



## Cybertronnixxx (17. Februar 2014)

vielen dank youza werd mich da mal dran orientieren vielen dank für deine hilfe


----------

