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;
}