I am trying to update with this code

Result.updateInt("bank_ac_no",Integer.parseInt(acnotxtfield.getText().trim()));
//this is the line I'm getting error.

Here is the Trace

java.lang.NumberFormatException: For input string: "2737383928291819"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:495)
    at java.lang.Integer.parseInt(Integer.java:527)

What's wrong?

Dani AI

Generated

You are getting the exception before JDBC ever touches the DB. The parse of a 16-digit string into an int overflows, so Integer.parseInt(...) throws. is right that a long would hold this specific value, but there is a bigger design point: bank account (or card) numbers are identifiers, not quantities. Treat them as text to preserve leading zeros, avoid overflow now or in the future, and sidestep locale/format issues. In the database, prefer a VARCHAR (or similar) column, not an INT/BIGINT. That also makes input validation and masking simpler.

is right to ask about catching NumberFormatException, but do not rely on exceptions for normal validation. Pre-validate the text and fail fast with a clear message. For example, strip whitespace, check that it is only digits, and optionally enforce an allowed length range that matches your business rules.

String acct = acnotxtfield.getText().replaceAll("\\s+", "");
if (!acct.matches("\\d{6,20}")) { // adjust bounds to your rules
    // show a friendly validation error and return
    return;
}
// store as text so you keep leading zeros and avoid overflow
result.updateString("bank_ac_no", acct);

If you must keep the column numeric, then change the DB type to BIGINT and parse with Long.parseLong(...) into a long variable first; then pass that variable to your update call. Simply switching to updateLong while still using Integer.parseInt (as you tried) will keep failing because the parse happens with the smaller type. Also consider adding server-side constraints (CHECK/length) so invalid values never reach the table.

Recommended Answers

All 4 Replies

Did you try catching the NumberFormatException?

Yes

 catch (SQLException | NumberFormatException e) 
          {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

Looks to me like that value is a valid integer, but its not a valid int because it's too big, >= 2^32. You could use a long instead of an int.

Do I have to change the Data type also for the column?Because

Result.updateLong("bank_ac_no",Integer.parseInt(acnotxtfield.getText().trim()));

updateLong instead of Int did not worked ,gave me the same error.

EDIT:I think I have also to change Long.parseLong :) It will work then .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.