Hello all,

I'm sorry if this is an easy fix, but I've searched everywhere and spent 3 days on this and would like my life back. I'm trying to Select data from a database using a PreparedStatement and it isn't working for me. No errors, just no data. I can Insert, Update, Delete data and I can create and drop tables. It seems the only thing I cannot do is Select from a database. I've created several new databases to make sure I didn't screw it up and still the same result. My code is quite basic, but I'll post it in case I'm missing something. I changed my query to the most basic select I could think of and still got nothing. Thanks for any help guys.

public static Connection connectToMYSQLDBMS () throws SQLException
    {
        try
        {
	   Class.forName("com.mysql.jdbc.Driver").newInstance();
        }
        catch(Exception x)
        {
	   System.out.println( "Unable to load the driver class!" );
        }


       Connection conn = null;

       try
       {
          conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?" +
                                             "user=user&password=password");
       }
       catch(SQLException e)
       {
           System.out.println("Unable to establish a connection..." + e);
       }

       return conn;
    }

   public static ResultSet getSongByUser(int inUserID, Connection con)
    {
       ResultSet srs = null;

       try
       {
          String sqlQuery = "select * from audiotracks ";

           PreparedStatement ps = con.prepareStatement(sqlQuery);       
           srs = ps.executeQuery();

           System.out.println(srs.getFetchSize());


          ps.close();
       }

       catch (SQLException e)
       {
           System.out.println("Unable to execute query... " + e );
       }

          return srs;

    }

Dani AI

Generated

Two separate points caused the confusion. As pointed out, closing the Statement (or PreparedStatement) closes its ResultSet, so the Statement must remain open while the ResultSet is being processed. The actual logic bug was treating ResultSet.getFetchSize() as the number of rows. getFetchSize() is only an advisory hint to the driver (it defaults to 0) and does not report how many rows the query returned — see the ResultSet.getFetchSize documentation (ResultSet.getFetchSize).

The correct approach is to iterate the ResultSet with while (rs.next()) and read columns inside that loop. If a row count is needed up front, run SELECT COUNT(*) ... or create a scrollable ResultSet and use rs.last() followed by rs.getRow() (but that requires a scrollable ResultSet and can be expensive because it may materialize all rows). For large result sets, avoid counting by moving the cursor to last.

MySQL-specific note: Connector/J does not treat fetch size the same way as some other drivers. To control streaming or server-side cursors consult the Connector/J documentation (server-side cursor options and useCursorFetch), since driver-specific settings are required to change default fetch behavior.

Practical checklist (what fixed in this thread):

  • Do not use getFetchSize() to detect "no rows".
  • Iterate with while (rs.next()) and process columns there.
  • Keep the Statement open until the ResultSet is fully processed (or use try-with-resources to scope them correctly).
  • Use SELECT COUNT(*) or a scrollable ResultSet when an upfront row count is truly needed.

Credit: good catch by on the fetchSize issue and thanks to for the Statement/ResultSet reminder.

Recommended Answers

All 3 Replies

You are closing the Statement object which in turn closes the ResultSet which was obtained from it. Read this.

Close the Statement only if you are done with the ResultSet and the Statement object, close the Connection only if you are done with the ResultSet, Statement and Connection objects.

I removed the call to close the PreparedStatement but still fail to get any data from the database. When I try to loop through the ResultSet, I can call next() and it returns true, doesn't that mean there is a next row for the cursor to be moved to? I think the problem may be in my handling of the data. Thank you for the tip about closing the statement though, I was unaware it would close my result set too.

I got it figured out. I was processing the data incorrectly. Since I hadn't set the fetchSize, it was returning 0 and never going into that loop. Thanks for the help ~s.o.s~

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.