2012-04-12 3 views
1

Ich versuche, mehrere Aufrufe an die Datenbank auszuführen, die erste (die Auswahl) funktioniert gut .. aber wenn ich in die zweite gehe bekomme ich diesen FehlerProgramm besagt, dass ich Operationen nach Abschluss der Transaktion nicht ausführen kann

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after statement 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:406) 
    at com.mysql.jdbc.Util.getInstance(Util.java:381) 
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:984) 
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956) 
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926) 
    at com.mysql.jdbc.StatementImpl.checkClosed(StatementImpl.java:380) 
    at com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1250) 
    at MysqlConnect.main(MysqlConnect.java:77) 
    at __SHELL13.run(__SHELL13.java:6) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
    at java.lang.reflect.Method.invoke(Method.java:597) 
    at bluej.runtime.ExecServer$3.run(ExecServer.java:724) 

Und hier ist mein Code (bitte noch entschuldigen Lernen) würde

import java.sql.*; 

public class MysqlConnect{ 

    public static void main(String[] args) { 

    Connection conn = null; 

    String url = "jdbc:mysql://localhost:3306/"; 
    String dbName = "MyBussiness"; 
    String driver = "com.mysql.jdbc.Driver"; 
    String userName = "mambo"; 
    String password = "jambo"; 

    try { 
    Class.forName(driver).newInstance(); 
    conn = DriverManager.getConnection(url+dbName,userName,password); 



    System.out.println("Connected to the database"); 
     /*SELECTING DATA */ 
     // Get a statement from the connection 
     Statement stmt = conn.createStatement() ; 
     System.out.println("--------------------------------------------."); 
     System.out.println("Retrieving items from the customers table (USING SELECT)..."); 
     System.out.println("--------------------------------------------."); 
     Thread.sleep(2000); 
     // Execute the query 
     ResultSet rs = stmt.executeQuery("SELECT * FROM customers") ; 

     // Loop through the result set 
     while(rs.next()) 
     { 
      System.out.print(rs.getInt(1)) ; 
      System.out.print(",  "); 
      System.out.print(rs.getString(2)); 
      System.out.print(",  "); 
      System.out.print(rs.getString(3)); 
      System.out.print(",  "); 
      System.out.print(rs.getString(4)); 
      System.out.print(",  "); 
      System.out.print(rs.getString(5)); 
      System.out.print(",  "); 
      System.out.print(rs.getString(6)); 
      System.out.print(",  "); 
      System.out.print(rs.getString(7)); 
      System.out.print(",  "); 
      System.out.print(rs.getString(8)); 
      System.out.print(",  "); 
      System.out.print(rs.getString(9)); 
      System.out.print(",  "); 
      System.out.println(rs.getString(10)); 
     } 

     // Close the result set, statement and the connection 
     rs.close() ; 
     stmt.close() ; 



     /*UPDATING DATA */ 
     // Get a statement from the connection 
     conn = DriverManager.getConnection(url+dbName,userName,password); 
     Statement stmt2 = conn.createStatement() ; 
     System.out.println("---------------------------------------------------------------------------."); 
     System.out.println("Updating Customers Table for Customer ID 1 Federico Gutierrez Address1 Field (USING UPDATE)..."); 
     System.out.println("---------------------------------------------------------------------------."); 
     Thread.sleep(2000); 
     // Execute the query 
     ResultSet rs2 = stmt.executeQuery("UPDATE Customers SET Address1='999 Mambo Avenue' WHERE CustomerID = 1"); 

     // Loop through the result set 
     while(rs2.next()) 
     { 
      System.out.print(rs2.getInt(1)) ; 
      System.out.print(",  "); 
      System.out.print(rs2.getString(2)); 
      System.out.print(",  "); 
      System.out.print(rs2.getString(3)); 
      System.out.print(",  "); 
      System.out.print(rs2.getString(4)); 
      System.out.print(",  "); 
      System.out.print(rs2.getString(5)); 
      System.out.print(",  "); 
      System.out.print(rs2.getString(6)); 
      System.out.print(",  "); 
      System.out.print(rs2.getString(7)); 
      System.out.print(",  "); 
      System.out.print(rs2.getString(8)); 
      System.out.print(",  "); 
      System.out.print(rs2.getString(9)); 
      System.out.print(",  "); 
      System.out.println(rs2.getString(10)); 
     } 

     // Close the result set, statement and the connection 
     rs2.close() ; 
     stmt2.close() ; 


      /*INSERTING DATA */ 
     // Get a statement from the connection 
     conn = DriverManager.getConnection(url+dbName,userName,password); 
     Statement stmt3 = conn.createStatement() ; 
     System.out.println("---------------------------------------------------------------------------."); 
     System.out.println("Inserting a new customer (Mario Villalobos) into the customers table ... (USING INSERT)"); 
     System.out.println("---------------------------------------------------------------------------."); 
     Thread.sleep(2000); 
     // Execute the query 
     ResultSet rs3 = stmt.executeQuery("INSERT INTO Customers (SSN, FirstName, LastName, Address1, Address2, State, City, Zip, PhoneNumber)"+ 
              " VALUES ('444559999','Mario','Villalobos','777 Boynton Beach','Apt 4R','FL','Boynton Beach','33436','(555)444-5555'"); 

     // Loop through the result set 
     while(rs3.next()) 
     { 
      System.out.print(rs3.getInt(1)) ; 
      System.out.print(",  "); 
      System.out.print(rs3.getString(2)); 
      System.out.print(",  "); 
      System.out.print(rs3.getString(3)); 
      System.out.print(",  "); 
      System.out.print(rs3.getString(4)); 
      System.out.print(",  "); 
      System.out.print(rs3.getString(5)); 
      System.out.print(",  "); 
      System.out.print(rs3.getString(6)); 
      System.out.print(",  "); 
      System.out.print(rs3.getString(7)); 
      System.out.print(",  "); 
      System.out.print(rs3.getString(8)); 
      System.out.print(",  "); 
      System.out.print(rs3.getString(9)); 
      System.out.print(",  "); 
      System.out.println(rs3.getString(10)); 
     } 

     // Close the result set, statement and the connection 
     rs3.close() ; 
     stmt3.close() ; 


        /*DELETING DATA */ 
     // Get a statement from the connection 
     conn = DriverManager.getConnection(url+dbName,userName,password); 
     Statement stmt4 = conn.createStatement() ; 
     System.out.println("---------------------------------------------------------------------------."); 
     System.out.println("Deleting customer where the name is George... (USING Delete)"); 
     System.out.println("---------------------------------------------------------------------------."); 
     Thread.sleep(2000); 
     // Execute the query 
     ResultSet rs4 = stmt.executeQuery("DELETE FROM Customers WHERE FirstName Like '%George%'"); 

     // Loop through the result set 
     while(rs4.next()) 
     { 
      System.out.print(rs4.getInt(1)) ; 
      System.out.print(",  "); 
      System.out.print(rs4.getString(2)); 
      System.out.print(",  "); 
      System.out.print(rs4.getString(3)); 
      System.out.print(",  "); 
      System.out.print(rs4.getString(4)); 
      System.out.print(",  "); 
      System.out.print(rs4.getString(5)); 
      System.out.print(",  "); 
      System.out.print(rs4.getString(6)); 
      System.out.print(",  "); 
      System.out.print(rs4.getString(7)); 
      System.out.print(",  "); 
      System.out.print(rs4.getString(8)); 
      System.out.print(",  "); 
      System.out.print(rs4.getString(9)); 
      System.out.print(",  "); 
      System.out.println(rs4.getString(10)); 
     } 

     // Close the result set, statement and the connection 
     rs4.close() ; 
     stmt4.close() ; 

     //Reseting data 
     conn = DriverManager.getConnection(url+dbName,userName,password); 
     Statement stmt5 = conn.createStatement() ; 
     ResultSet rs5 = stmt.executeQuery("UPDATE Customers SET Address1='555 YY Ave' WHERE CustomerID = 1"); 
     rs5.close() ; 
     stmt5.close() ; 

     conn = DriverManager.getConnection(url+dbName,userName,password); 
     Statement stmt6 = conn.createStatement() ; 
     ResultSet rs6 = stmt.executeQuery("DELETE FROM Customers WHERE FirstName Like '%Mario%'"); 
     rs6.close() ; 
     stmt6.close() ; 
     conn = DriverManager.getConnection(url+dbName,userName,password);  
     Statement stmt7 = conn.createStatement() ; 
     ResultSet rs7 = stmt.executeQuery("INSERT INTO Customers (SSN, FirstName, LastName, Address1, Address2, State, City, Zip, PhoneNumber)"+ 
              " VALUES ('923431432','George','Scott','2325 S Babcock St',' ','FL','Melbourne','32901','(321)984-4910'"); 
     rs7.close() ; 
     stmt7.close() ; 
     conn.close() ; 


    System.out.println("Disconnected from database"); 
    } catch (Exception e) { 
    e.printStackTrace(); 
    } 
    } 
} 

Jede Hilfe sehr geschätzt werden.

HINWEIS: Es sagt mir, dass die Verbindung nach dem ersten Durchgang geschlossen ist, aber ich habe kein conn.Close(); bis zum Ende, wie du sehen kannst.

+0

Grund für die Abstimmung unten, damit ich es verbessern kann ?. – jedgard

Antwort

3
ResultSet rs2 = stmt.executeQuery(...) 
      // ^^^^ 

Sie bedeutete stmt2 dort. Bitte überprüfen Sie Ihren gesamten Code, um sicherzustellen, dass Sie die Anweisungen nicht erneut verwenden.

+0

das macht sehr viel Sinn, ich danke Ihnen so viel wird überprüfen – jedgard

+0

richtige Antwort, Sie bereits geschlossen Stmt nach der ersten Abfrage (Warum gibt es keine Zeilennummern auf Code-Schnipsel, wäre viel einfacher!) – hcpl

1

Falls Sie es noch nicht bemerkt haben, verwenden Sie dasselbe SQlStatement, das Sie bereits geschlossen haben. Die Linie:

ResultSet rs2 = stmt.executeQuery("UPDATE Customers SET Address1='999 Mambo Avenue' WHERE CustomerID = 1"); 

sollte sein:

ResultSet rs2 = stmt2.executeQuery("UPDATE Customers SET Address1='999 Mambo Avenue' WHERE CustomerID = 1"); 
0

Einige Korrekturen gemacht, aber Code ist immer noch nicht schön. Sollte arbeiten.

import java.sql.*; 

public class MysqlConnect{ 

    public static void main(String[] args) { 

    Connection conn = null; 

    String url = "jdbc:mysql://localhost:3306/"; 
    String dbName = "MyBussiness"; 
    String driver = "com.mysql.jdbc.Driver"; 
    String userName = "mambo"; 
    String password = "jambo"; 

    try { 
    Class.forName(driver).newInstance(); 
    conn = DriverManager.getConnection(url+dbName,userName,password); 



    System.out.println("Connected to the database"); 
     /*SELECTING DATA */ 
     // Get a statement from the connection 
     Statement stmt = conn.createStatement() ; 
     System.out.println("--------------------------------------------."); 
     System.out.println("Retrieving items from the customers table (USING SELECT)..."); 
     System.out.println("--------------------------------------------."); 
     Thread.sleep(2000); 
     // Execute the query 
     ResultSet rs = stmt.executeQuery("SELECT * FROM customers") ; 

     // Loop through the result set 
     while(rs.next()) 
     { 
      System.out.print(rs.getInt(1)) ; 
      System.out.print(",  "); 
      System.out.print(rs.getString(2)); 
      System.out.print(",  "); 
      System.out.print(rs.getString(3)); 
      System.out.print(",  "); 
      System.out.print(rs.getString(4)); 
      System.out.print(",  "); 
      System.out.print(rs.getString(5)); 
      System.out.print(",  "); 
      System.out.print(rs.getString(6)); 
      System.out.print(",  "); 
      System.out.print(rs.getString(7)); 
      System.out.print(",  "); 
      System.out.print(rs.getString(8)); 
      System.out.print(",  "); 
      System.out.print(rs.getString(9)); 
      System.out.print(",  "); 
      System.out.println(rs.getString(10)); 
     } 

     // DO NOT Close the result set, statement and the connection 



     /*UPDATING DATA */ 
     // Get a statement from the connection 
     System.out.println("---------------------------------------------------------------------------."); 
     System.out.println("Updating Customers Table for Customer ID 1 Federico Gutierrez Address1 Field (USING UPDATE)..."); 
     System.out.println("---------------------------------------------------------------------------."); 
     Thread.sleep(2000); 
     // Execute the UPDATE 
     stmt.executeUpdate("UPDATE Customers SET Address1='999 Mambo Avenue' WHERE CustomerID = 1"); 

     // Loop through the result set 
     //NO ResultSet for UPDATE 

     // DON'T Close the result set, statement and the connection 



      /*INSERTING DATA */ 
     // Get a statement from the connection 

     System.out.println("---------------------------------------------------------------------------."); 
     System.out.println("Inserting a new customer (Mario Villalobos) into the customers table ... (USING INSERT)"); 
     System.out.println("---------------------------------------------------------------------------."); 
     Thread.sleep(2000); 
     // Execute the UPDATE 
    stmt.executeUpdate("INSERT INTO Customers (SSN, FirstName, LastName, Address1, Address2, State, City, Zip, PhoneNumber)"+ 
              " VALUES ('444559999','Mario','Villalobos','777 Boynton Beach','Apt 4R','FL','Boynton Beach','33436','(555)444-5555'"); 

     // Loop through the result set INSERT HAS NO RESULTSET 

     // Close the result set, statement and the connection 


        /*DELETING DATA */ 
     // Get a statement from the connection 
     System.out.println("---------------------------------------------------------------------------."); 
     System.out.println("Deleting customer where the name is George... (USING Delete)"); 
     System.out.println("---------------------------------------------------------------------------."); 
     Thread.sleep(2000); 
     // Execute the query, NO UPDATE 
     stmt.executeUpdate("DELETE FROM Customers WHERE FirstName Like '%George%'"); 

     // Loop through the result set 


     // Close the result set, statement and the connection 

     //Reseting data 
     stmt.executeUpdate("UPDATE Customers SET Address1='555 YY Ave' WHERE CustomerID = 1"); 

     stmt.executeUpdate("DELETE FROM Customers WHERE FirstName Like '%Mario%'"); 

     stmt.executeUpdate("INSERT INTO Customers (SSN, FirstName, LastName, Address1, Address2, State, City, Zip, PhoneNumber)"+ 
              " VALUES ('923431432','George','Scott','2325 S Babcock St',' ','FL','Melbourne','32901','(321)984-4910'"); 

    // NOW CLOSE DB RS, STATEMENT, CONN 
     rs.close() ; 


     stmt.close() ; 
     conn.close() ; 


    System.out.println("Disconnected from database"); 
    } catch (Exception e) { 
    e.printStackTrace(); 
    } 
    } 
} 
Verwandte Themen