Announcement

Collapse
No announcement yet.

Fehlermeldung durch getString

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Fehlermeldung durch getString

    Hallo,

    ich bekomm hier eine Fehlermeldung durch den Zusatz getString(1):

    Code:
    String query = "SELECT * FROM gratulation";
    			ResultSet res = stat.executeQuery(query);
    			while(res.next());
    				String resstring = res.getString(1);
    				System.out.println(resstring);
    			res.close();
    So erhalte ich folgende Fehlermeldung:

    java.sql.SQLException: After end of result set
    at com.mysql.jdbc.SQLError.createSQLException(SQLErro r.java:1073)
    at com.mysql.jdbc.SQLError.createSQLException(SQLErro r.java:987)
    at com.mysql.jdbc.SQLError.createSQLException(SQLErro r.java:982)
    at com.mysql.jdbc.SQLError.createSQLException(SQLErro r.java:927)
    at com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSet Impl.java:843)
    at com.mysql.jdbc.ResultSetImpl.getStringInternal(Res ultSetImpl.java:5649)
    at com.mysql.jdbc.ResultSetImpl.getString(ResultSetIm pl.java:5569)
    at components.QueryDBtest.runTest(QueryDBtest.java:45 )
    at components.QueryDBtest.main(QueryDBtest.java:146)
    Wenn ich das jetzt ohne String nehme bleibt die Fehlermeldung aus:
    Code:
    	String query = "SELECT * FROM gratulation";
    			ResultSet res = stat.executeQuery(query);
    			while(res.next());
    				System.out.println(res);
    			res.close();
    So erhalte ich die Ausgabe in der Console:
    com.mysql.jdbc.JDBC4ResultSet@120f0be
    Jetzt ist die Frage woran es liegt - die Angabe " After end of result set" sagt ja aus, dass irgendwo der aktuelle Datensatz verändert wird... oder nicht?

    besten Dank

  • #2
    Die Fehlermeldung besagt, dass das Resultset zu Ende ist (next hat ein false geliefert), aber weiterhin darauf zugegriffen wird

    Im Stacktrace sollte irgendwo die Zeilennummer deines Programmes (components.QueryDBtest.runTest??) auftauchen, wo der Fehler passiert. Ggf. debugge das und schau dir das ResultSet an
    Christian

    Comment


    • #3
      Bei der Verwendung des Debuggers bzw beim Anklicken der Fehlerurl kommt ein Hinweis aus einem Class File Editor, dass eine bestimmte Datei nicht verfügbar ist mit den Informationen zu dem Fehler! Sieht so aus als ob in der mysql Connector Datei nach Infos gesucht wird aber keine vorhanden sind für die Fehlermeldungen.......!

      in eine MessageBox kommt dann "XXXX ist keine gültige Zeilennummer in com.mysql.jdbc.ResultSetImpl" - auf Englisch halt...!

      Comment


      • #4
        Es wäre extrem unwahrscheinlich, das der JDBC Connector für Mysql

        http://www.mysql.com/products/connector/

        fehlerhaft wäre. Zeige mal den kompletten Code
        Christian

        Comment


        • #5
          Code:
          package components;
          
          /*
           * SimpleTableDemo.java requires no other files.
           */
          
          import javax.swing.JFrame;
          import javax.swing.JPanel;
          import javax.swing.JScrollPane;
          import javax.swing.JTable;
          import java.awt.Dimension;
          import java.awt.GridLayout;
          import java.awt.event.MouseAdapter;
          import java.awt.event.MouseEvent;
          import java.io.IOException;
          import java.sql.*;
          
          
          public class QueryDBtest extends JPanel {
              private boolean DEBUG = false;
          
              
              /**
           	* Runs a test by creating a table, adding a value, showing the table contents, and
           	* removing the table. 
           	**/
              public static void runTest() throws SQLException, IOException
          	{
          		Connection conn = getConnection();
          	
          		try
          		{
          			Statement stat = conn.createStatement();
          		
          			ResultSet result = stat.executeQuery("SHOW TABLES");
          			int count = 0;
          			while (result.next())
          				count++;	
          			System.out.println("Es gibt " + count + " Tabellen.");
          			result.close();
          			
          			String query = "SELECT * FROM gratulation";
          			ResultSet res = stat.executeQuery(query);
          			while(res.next());
          				System.out.println(res.getString(1));
          			res.close();
          
          		}
          		finally
          		{
          			conn.close();
          		}
          	}
              
              public QueryDBtest() {
                  super(new GridLayout(1,0));
          
                  String[] columnNames = {"First Name",
                                          "Last Name",
                                          "Sport",
                                          "# of Years",
                                          "Vegetarian"};
          
                  Object[][] data = {
          	    {"Kathy", "Smith",
          	     "Snowboarding", new Integer(5), new Boolean(false)},
          	    {"John", "Doe",
          	     "Rowing", new Integer(3), new Boolean(true)},
          	    {"Sue", "Black",
          	     "Knitting", new Integer(2), new Boolean(false)},
          	    {"Jane", "White",
          	     "Speed reading", new Integer(20), new Boolean(true)},
          	    {"Joe", "Brown",
          	     "Pool", new Integer(10), new Boolean(false)}
                  };
                  
                    
                  final JTable table = new JTable(data, columnNames);
                  table.setPreferredScrollableViewportSize(new Dimension(500, 150));
                  table.setFillsViewportHeight(true);
          
                  if (DEBUG) {
                      table.addMouseListener(new MouseAdapter() {
                          public void mouseClicked(MouseEvent e) {
                              printDebugData(table);
                          }
                      });
                  }
          
                  //Create the scroll pane and add the table to it.
                  JScrollPane scrollPane = new JScrollPane(table);
          
                  //Add the scroll pane to this panel.
                  add(scrollPane);
              }
          
              private void printDebugData(JTable table) {
                  int numRows = table.getRowCount();
                  int numCols = table.getColumnCount();
                  javax.swing.table.TableModel model = table.getModel();
          
                  System.out.println("Value of data: ");
                  for (int i=0; i < numRows; i++) {
                      System.out.print("    row " + i + ":");
                      for (int j=0; j < numCols; j++) {
                          System.out.print("  " + model.getValueAt(i, j));
                      }
                      System.out.println();
                  }
                  System.out.println("--------------------------");
              }
          
              /**
               * Create the GUI and show it.  For thread safety,
               * this method should be invoked from the
               * event-dispatching thread.
               */
              private static void createAndShowGUI() {
                  //Create and set up the window.
                  JFrame frame = new JFrame("SimpleTableDemo");
                  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          
                  //Create and set up the content pane.
                  QueryDBtest newContentPane = new QueryDBtest();
                  newContentPane.setOpaque(true); //content panes must be opaque
                  frame.setContentPane(newContentPane);
          
                  //Display the window.
                  frame.pack();
                  frame.setVisible(true);
              }
          
                  
              public static void main(String[] args) {
                  //Schedule a job for the event-dispatching thread:
                  //creating and showing this application's GUI.
                  javax.swing.SwingUtilities.invokeLater(new Runnable() {
                      public void run() {
                          createAndShowGUI();
                      }
                  });
                  
                  try
          		{
          			runTest();
          		}
          		catch (SQLException ex)
          		{
          			for (Throwable t: ex)
          				t.printStackTrace();
          		}
          		catch (IOException ex)
          		{
          			ex.printStackTrace();
          		}
                  
              }
              
              
              
          
          	/**
          	 * Gets a connection from the properties specified in the file database properties
          	 * @return the database connection
          	 **/
          
          	public static Connection getConnection() throws SQLException, IOException
          	{
          	
          		String drivers = "com.mysql.jdbc.Driver";
          		if(drivers != null) System.setProperty("jdbc.drivers", drivers);
          		String url = "jdbc:mysql://localhost:3306/datenbank";
          		String username = "benutzer";
          		String password = "password";
          	
          		return DriverManager.getConnection(url, username, password);
          	
          	}
              
          }

          Comment


          • #6
            Die Connection würde ich mit

            Class.forName( "com.mysql.jdbc.Driver" );
            cn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/myDatabaseName", user, passwort);

            if(cn!=null)
            return cn;

            aufbauen

            Des Weiteren drückst du alle Excetions weg ohne die wirklich irgendwo aus zugeben. So weisst du nicht, wo tatsächlich der Fehler auftritt
            Christian

            Comment


            • #7
              Ja tut mir leid versteh ich nicht wirklich wie ich das umbauen sollte - bin froh dass ich das so hinbekommen habe! ;-)
              Da wüsste ich jetzt nicht, wo ich was einfügen und was ich löschen müsste...!

              Die Fehlermeldungen würde aber wohl weiterhin kommen...?

              Comment


              • #8
                [highlight=java]

                public static Connection getConnection() {
                Connection cn = null;
                try {
                Class.forName("com.mysql.jdbc.Driver");
                cn = DriverManager.getConnection("jdbc:mysql://localhost:3306/myDatabaseName", "user", "passwort");
                } catch (SQLException e) {
                System.out.println("SQL-Exception:" + e.getMessage());
                } catch (ClassNotFoundException e) {
                System.out.println("ClassNotFoundException:" + e.getMessage());
                }
                if (cn != null)
                return cn;
                System.out.println("Connection ist null");
                return null;
                }
                [/highlight]

                Beim prüfen jetzt gesehen:

                while(res.next());

                while (res.next()) {

                System.out.println(res.getString(1));
                res.close();
                }


                Die Schleife hatte keine Schleifenkörper

                Christian

                Comment


                • #9
                  Ah ok stimmt super - das ist ein Punkt.

                  Ich habs gerade probiert und leider ist es noch nicht die ganze Wahrheit! :-)

                  Folgende Ausgabe erhalte ich:

                  Code:
                  Es gibt 2 Tabellen.
                  Geburtstag
                  java.sql.SQLException: Operation not allowed after ResultSet closed
                  	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
                  	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987)
                  	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982)
                  	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927)
                  	at com.mysql.jdbc.ResultSetImpl.checkClosed(ResultSetImpl.java:796)
                  	at com.mysql.jdbc.ResultSetImpl.next(ResultSetImpl.java:6857)
                  	at components.QueryDBtest.runTest(QueryDBtest.java:44)
                  	at components.QueryDBtest.main(QueryDBtest.java:146)
                  Also es wird nur einmal die While-Schleife durchgangen, ich habe per Eingabezeile in die Tabelle 7 Einträge gemacht... nur der erste Eintrag erscheint!

                  Comment


                  • #10
                    Habs selber gemerkt - res.close(); gehört außerhalb des SChleifenkörper! :-)
                    Sowas kann man dann auch ohne große Java-Kenntnisse merken, dass die Schleife nicht schon beim ersten Durchgang geschlossen wird bzw die Verbindung gekappt wird! :-)

                    Super Hilfe, besten Dank!

                    Jetzt probier ich wieder selbst aus!

                    Comment

                    Working...
                    X