简体   繁体   English

Java,MySQL-NULL结果集

[英]Java,MySQL - NULL resultset

I got a method to get an integer from a MySQL table 我有一种从MySQL表中获取整数的方法

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: "" . 该查询似乎很好,但是以某种方式初始化变量并使用此方法时, Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: ""得到错误Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "" Am I missing anything? 我有什么想念的吗? Thanks for any help! 谢谢你的帮助!

Your SQL is SELECT PK_ADDRESS_ID ... 您的SQL是SELECT PK_ADDRESS_ID ...

but then you try to get 但后来你试图得到

rs.getString("postcode");

change to 改成

rs.getString("PK_ADDRESS_ID");

It seems you are selecting PK_ADDRESS_ID column from the database and the value returned is either "" (empty) or NULL . 似乎您正在从数据库中选择PK_ADDRESS_ID列,并且返回的值为“” (empty)NULL So it shows NumberFormatException which cannot be converted as a Number. 因此它显示了NumberFormatException ,它无法转换为数字。

So please check the value you get from the database. 因此,请检查您从数据库获得的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM