Tri Nguyen
Tri Nguyen

Reputation: 1808

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!

Upvotes: 0

Views: 126

Answers (2)

Anands23
Anands23

Reputation: 780

It seems you are selecting PK_ADDRESS_ID column from the database and the value returned is either "" (empty) or NULL. So it shows NumberFormatException which cannot be converted as a Number.

So please check the value you get from the database.

Upvotes: -1

Scary Wombat
Scary Wombat

Reputation: 44813

Your SQL is SELECT PK_ADDRESS_ID ...

but then you try to get

rs.getString("postcode");

change to

rs.getString("PK_ADDRESS_ID");

Upvotes: 3

Related Questions