# Daten aus MySql Datenbank lesen???



## patrick_e (31. Mrz 2010)

Hallo,
also die Verbindung wird wunderbar hergestellt aber das Problem ist, das ich nix abrufen kann.Kann mir vielleicht jemand ne Hilfestellung geben?


```
public static void main(String args[]) {
        final bdesqlabfrage g = new bdesqlabfrage();
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                g.setVisible(true);
            }});
        String[] tag ={"1","2","3","4","5","6","7","8","9","10","11","12","13","14",
        "15","16","17","18","19","20","21","23","24","25","26","24","28","29","30","31"};
        String[] monat ={"1","2","3","4","5","6","7","8","9","10","11","12"};
        String[] Jahr ={"2010","2011","2012","2013","2014","2015"};
        
        String[] stunden ={"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15",
        "16","17","18","19","20","21","23","24","25","26","24","28","29","30","31","32","33",
        "34","35","36","37","38","39","40","41","42","43","44","45","46","47","48","49","50",
        "51","52","53","54","55","56","57","58","59"};
        String[] minuten ={"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15",
        "16","17","18","19","20","21","23","24","25","26","24","28","29","30","31","32","33",
        "34","35","36","37","38","39","40","41","42","43","44","45","46","47","48","49","50",
        "51","52","53","54","55","56","57","58","59"};
        String[] sekunden ={"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15",
        "16","17","18","19","20","21","23","24","25","26","24","28","29","30","31","32","33",
        "34","35","36","37","38","39","40","41","42","43","44","45","46","47","48","49","50",
        "51","52","53","54","55","56","57","58","59"};
        
        Connection con=null;
        int stu=0,stu1=0,j=0;
        int min=0,min1=0,m=0;
        int sek=0,sek1=0,t=0;



        try{
        System.out.println("Treiber laden");

            Class.forName("com.mysql.jdbc.Driver");
            System.out.println("Treiber geladen");
            DriverManager.setLogWriter(new PrintWriter(System.out));
            System.out.println("Verbindung wird aufgebaut");
            con = (Connection) DriverManager.getConnection("jdbc:mysql://192.168.****:3306/bde", "root", "*****");
            System.out.println("Verbindung aufgebaut");

           
            stu = g.jComboBox4.getSelectedIndex();
            min=g.jComboBox5.getSelectedIndex();
            sek=g.jComboBox6.getSelectedIndex();
            
            stu1=g.jComboBox4.getSelectedIndex();
            min1=g.jComboBox4.getSelectedIndex();
            sek1=g.jComboBox4.getSelectedIndex();
            
            j=g.jComboBox1.getSelectedIndex();
            m=g.jComboBox2.getSelectedIndex();
            t=g.jComboBox3.getSelectedIndex();
            while(true){
            if (g.jCheckBox1.isSelected()) {
            //String query= "select linie,date,sum(differenz) from bde.bdetable where time between "+stunden[stu]+minuten[min]+sekunden[sek]+
            //" and "+stunden[stu1]+minuten[min1]+sekunden[sek1]+" and date ="+Jahr[j]+monat[m]+tag[t]+" group by linie;";
            String query="Select count(*) from bde.bdetable";
            java.sql.Statement st = con.createStatement();
            java.sql.ResultSet rs = st.executeQuery(query);

            while(rs.next()){
                System.out.println(rs.getString(0));
            rs.close();
            st.close();
            }
            
                
            }  con.close();
            }  
        }catch(Exception e){
        e.printStackTrace();
        }
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new bdesqlabfrage().setVisible(true);
            }
        });
    }
```
Fehlercode: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed.
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
        at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
        at com.mysql.jdbc.Util.getInstance(Util.java:384)
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1015)
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989)
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:984)
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:929)
        at com.mysql.jdbc.ConnectionImpl.throwConnectionClosedException(ConnectionImpl.java:1204)
        at com.mysql.jdbc.ConnectionImpl.checkClosed(ConnectionImpl.java:1191)
        at com.mysql.jdbc.ConnectionImpl.createStatement(ConnectionImpl.java:2534)
        at com.mysql.jdbc.ConnectionImpl.createStatement(ConnectionImpl.java:2516)
        at bdemitsqlabfrage.bdesqlabfrage.main(bdesqlabfrage.java:296)


----------



## Michael... (31. Mrz 2010)

Du rufst während des Auslesens des ResultSets close() an ihm und dem Statemtent auf.


patrick_e hat gesagt.:


> [JAVA=64]         while(rs.next()){
> System.out.println(rs.getString(0));
> rs.close();
> st.close();
> ...


----------



## patrick_e (31. Mrz 2010)

das ist aber nicht das Problem es kommt immer noch nichts


----------



## Michael... (31. Mrz 2010)

Naja, auf jeden Fall scheint die Verbindung zwischendurch beendet worden zu sein.
Ausserdem stimmt da was mit Deiner Klammersetzung nicht, das könnte man ja nicht mal kompilieren.

Als Tipp: Fang einfach mal klein an ohne GUI und Co.
-Verbinden, Abfragen, auf Konsole ausgeben.

und nicht alles in die public static void main(...) reinschreiben, die benutzt man nur zum Starten der Anwendung.


----------



## patrick_e (31. Mrz 2010)

ohne GUI!!!!!

```
public class ohnegui {

    public static void main(String args[]) throws SQLException {
        Connection con = null;
        try{
            System.out.println("Treiber laden");
            Class.forName("com.mysql.jdbc.Driver");
            System.out.println("Treiber geladen");
            DriverManager.setLogWriter(new PrintWriter(System.out));
            System.out.println("Verbindung wird aufgebaut");
            con = (Connection) DriverManager.getConnection("jdbc:mysql://192.168.*****:3306/bde", "root", "*****");
            System.out.println("Verbindung aufgebaut");
        }
        catch(Exception e){
            e.printStackTrace();
        }
        String query= "select linie,date,sum(differenz) from bde.bdetable where time between 070000 and 143000 and date =100331 group by linie;";

        java.sql.Statement st = con.createStatement();
        java.sql.ResultSet rs = st.executeQuery(query);

            System.out.println(rs.getString(0)+"-->"+rs.getString(1));
        
     rs.close();
     st.close();
     con.close();
    }

}
```

Fahlercode:Verbindung aufgebaut
SQLState(S1000) vendor code(0)
Exception in thread "main" java.sql.SQLException: Before start of result set
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1075)
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989)
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:984)
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:929)
        at com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:842)
        at com.mysql.jdbc.ResultSetImpl.getStringInternal(ResultSetImpl.java:5657)
        at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5577)
        at ohnegui.main(ohnegui.java:35)
Java Result: 1


----------



## Michael... (31. Mrz 2010)

Der Zeiger des ResultSets ist noch nicht positioniert, ausserdem könntest Du so auf einen Fehler laufen, wenn der ResultSet kein Ergebnis liefert --> while(rs.next()) vor Zeile 22


----------



## patrick_e (31. Mrz 2010)

Danke hat funktioniert!!!!


----------

