0

My Code is :

String sql="insert into L_EC(AMOUNT)values(?)";

    pst=con.prepareStatement(sql);


   for(int i=0;i<dtm.getRowCount();i++){
     BigDecimal nbr= parsing(dtm.getValueAt(i, 1).toString());
     prst.setBigDecimal(1, nbr);
     prst.addBatch();
}
prst.executeBatch();

info: Column is of type Currency

private static BigDecimal parsing(String str) {
    String normalized="";
    if(str==null){
       normalized="0";
    }else{
     normalized = str.replaceAll("\\s", "").replace(',', '.');
    }
    return new BigDecimal(normalized);

problem is that when a cell from table contain NULL ,I get this message:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException
    at java.math.BigDecimal.<init>(BigDecimal.java:545)
    at java.math.BigDecimal.<init>(BigDecimal.java:739)
    at Vista.TCRCG.parse(TCRCG.java:130)

I think that the problem is on parsing method when the parser find a cell without a any text or value.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Squero27
  • 21
  • 1
  • 7
  • 1
    Please include the full exception stacktrace – Mark Rotteveel Oct 19 '16 at 12:08
  • I have include this function to my code : private static BigDecimal parse(String str) { String normalized=""; if(str==null){ normalized="0"; }else{ normalized = str.replaceAll("\\s", "").replace(',', '.'); } return new BigDecimal(normalized); – Squero27 Oct 19 '16 at 13:08
  • Don't use comments, **edit** your question. And post the stacktrace with the full exception message. – Mark Rotteveel Oct 19 '16 at 13:10

1 Answers1

1

Your parsing method will throw that exception if str is an empty string ("") or a space (" "). You may need to do some extra checking, e.g.,

if (str == null || str.trim().length() == 0) {
    normalized = "0";
Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
  • After edit code with your answer ,it work perfectly,but when i want to insert data on database I got this message: Exception in thread thread-0 java.lang.arrayindexoutofboundsexception: 10 – Squero27 Oct 19 '16 at 14:20
  • Finally, it works well, I have forgot a column name on query ,Thank you @Gord Thompson to your help – Squero27 Oct 19 '16 at 14:44