Announcement Announcement Module
Collapse
No announcement yet.
A problem with MapSqlParameterSource Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • A problem with MapSqlParameterSource

    So, here is the scenario:

    I have a table with three columns:
    - ID, VARCHAR2, NULLABLE = N
    - VALUE1, NUMBER, NULLABLE = Y
    - VALUE2, NUMBER, NULLABLE = Y
    - VALUE3, VARCHAR2, NULLABLE = Y


    All data is from String messages which simply concanate all column values by "||", for example:
    "id01||01||02||03" or, "id02||||02||||" .
    My mission is to split those values for each column, and save them into the database.

    I used the named parameters. My code is like this:

    Code:
    String message = "id01||01||02||03";
    String[] paras = message.split("\\|\\|");
    SqlParameterSource namedParameters = new MapSqlParameterSource().addValue("ID", paras[0])
    					.addValue("VALUE1",paras[1], Types.NUMERIC)
    					.addValue("VALUE2",paras[1], Types.NUMERIC)
    					.addValue("VALUE2",paras[1], Types.NUMERIC)
    This works fine for message like "id01||01||02||03".
    But for message like "id02||||02||||", it crashes because of casting null into an integer.

    Any ideas to solve this problem elegantly?

  • #2
    By the way, I've tried this way:

    Code:
    .addValue("VALUE1",paras[1]!=null ? paras[1] : "0", Types.NUMERIC)
    It adds 0 to the value when encountering a null.

    But this is not elegant. It should be null in the database, instead of 0.

    Comment


    • #3
      Fixed.

      juse use:

      Code:
      .addValue("VALUE1",paras[1])
      database will cast the String into number.

      Comment

      Working...
      X