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;

    }

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~

This question has already been answered. Start a new discussion instead.