Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

java.sql.SQLException: Before start of result set"

807607Jan 19 2007 — edited Jan 19 2007
I'm having a problem with getting some data from a database. The problem is that I am getting an exception, "java.sql.SQLException: Before start of result set".
I think the it might be coded right. Perhaps it's a problem with MySQL. Can any one give me some help here? Thanks in advance....

I've trimmed down the code to just the method.
rs, st and conn are declared global variables in the class.
/* This method queries the database and sets the
       variables that we'll "get" from the jsp form.*/
    public int setInputElementValues(String query)
    {

      try {
         Class.forName("com.mysql.jdbc.Driver").newInstance();
 	 conn = DriverManager.getConnection("jdbc:mysql://francis:3306/addressbook?autoReconnect=true",
                                             "root",
                                             "password"
                                           );
     Calendar rightNow = Calendar.getInstance();
     //System.out.println("Got a DB Connection" + rightNow.toString());
	 st = conn.createStatement();
	 //System.out.println("Getting ready to execute this query: " + query);

	 rs = st.executeQuery(query);
	 rs.beforeFirst(); //set result set to first line
	 /*set the field values*/
   	 firstName = rs.getString("FirstName");
/*
      lastName = rs.getString("LastName");
      addressLn1 = rs.getString("AddressLn1");
      addressLn2 = rs.getString("AddressLn2");
      city= rs.getString("City");
      state = rs.getString("State");
      zip = rs.getString("Zip");
      country=rs.getString("Country");
*/
      }
      catch (Exception ex)
      {
		  ex.printStackTrace();
      }
      finally
      {
		  try{
			  if (rs != null) { rs.close(); }
			  if (st != null) { st.close(); }
		      if (conn != null) { conn.close(); }
		  }
		  catch (Exception ex)
		  {
			  ex.printStackTrace();
			  System.out.println("could not close rs, st or conn");
		  }
      }
      return 1;
    }

Comments

Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Feb 16 2007
Added on Jan 19 2007
1 comment
608 views