Tri Nguyen Tri Nguyen - 25 days ago 6
Java Question

Java,MySQL - NULL resultset

I got a method to get an integer from a MySQL table

public int getAddressID(String postcode) throws SQLException {
String q = "SELECT PK_ADDRESS_ID FROM tbl_addresses WHERE postcode =" + "\"" + postcode + "\"";
System.out.println(q);
ResultSet rs = executeSearch(q);
int pc = 0;
while (rs.next()) {
String str = rs.getString("postcode");
pc = Integer.parseInt(str);
}
System.out.println(pc);
return pc;
}


The query seems fine but somehow when I initialize some variable and use this method, I get the error
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: ""
. Am I missing anything? Thanks for any help!

Answer

Your SQL is SELECT PK_ADDRESS_ID ...

but then you try to get

rs.getString("postcode");

change to

rs.getString("PK_ADDRESS_ID");