1

Whenever I run my prepared statement with the following SQL:

SELECT SUM(TEST_UNITS) FROM TESTDB.TEST 
WHERE ((S_TS >= ? AND S_TS <= ?) or (E_TS >= ? AND E_TS <= ?) OR (S_TS < ? AND E_TS > ?))

I get very random data together with the following error message:

com.ibm.db2.jcc.am.SqlSyntaxErrorException: [jcc][10177][11615][3.61.75] Invalid data conversion: Requested conversion would result in a loss of precision of 2579055604. ERRORCODE=-4461, SQLSTATE=42815

  • On the DB2 db TEST_UNITS is a BIGINT
  • and S_TS and E_TS are both from the datatype TIMESTAMP

However since I fire a prepared statement I already tried with:

  1. Calendar.getTime()
  2. With a simple Date Format sdf.format(Calendar.getTime())
  3. new java.sql.Timestamp(Calendar..getTimeInMillis())

Nothing seems to work correctly without conversion errors. Probably it is not even the time which is causing the problem. Does anybody have an idea what the problem could be?

Edit:

The Requested code snippet:

public ChartDataSetMSU msuWOMachine(Connection con, Date date){
    PreparedStatement prepStmt = null;
    ResultSet rs = null;
    ChartDataSetMSU chartDataMsu = new ChartDataSetMSU();
    int[] msuSaver  = chartDataMsu.getHouresMsu();
    SimpleDateFormat sdf =  new SimpleDateFormat("yyyy-MM-dd-HH.mm.ss.SSSSSS");


    Calendar vonCal = Calendar.getInstance();
    Calendar bisCal = Calendar.getInstance();

    vonCal.setTime(date);
    vonCal.set(Calendar.HOUR_OF_DAY, 0);
    vonCal.set(Calendar.MINUTE, 0);
    vonCal.set(Calendar.SECOND, 0);
    vonCal.set(Calendar.MILLISECOND, 0);

    bisCal.setTime(date);
    bisCal.set(Calendar.HOUR_OF_DAY, 1);
    bisCal.set(Calendar.MINUTE, 0);
    bisCal.set(Calendar.SECOND, 0);
    bisCal.set(Calendar.MILLISECOND, 0);

    for(int i = 0; i < 24; i++){

    try {                                           
        prepStmt = con.prepareStatement("SELECT SUM(TEST_UNITS) FROM TESTDB.TEST WHERE ((S_TS >= ? AND S_TS <= ?) or (E_TS >= ? AND E_TS <= ?) OR (S_TS < ? AND E_TS > ?))"); 

        prepStmt.setString(1, sdf.format(vonCal.getTime()));
        prepStmt.setString(2, sdf.format(bisCal.getTime()));
        prepStmt.setString(3, sdf.format(vonCal.getTime()));
        prepStmt.setString(4, sdf.format(bisCal.getTime()));
        prepStmt.setString(5, sdf.format(vonCal.getTime()));
        prepStmt.setString(6, sdf.format(bisCal.getTime()));

        rs = prepStmt.executeQuery();

        if(rs.next())
        {
        msuSaver[i] = rs.getInt(1); 
        systemLog.debug("Ammount this hour:  " +  i  +"  --> " + msuSaver[i]);
        }


    } catch (SQLException e) {
        e.printStackTrace();
    }

    vonCal.add(Calendar.HOUR_OF_DAY, 1);
    bisCal.add(Calendar.HOUR_OF_DAY, 1);

    }
    chartDataMsu.setHouresMsu(msuSaver);
    return chartDataMsu;
}

And the TIMESTAMP in DB2 is showing data like this

2015-07-31-13.37.01.000000

5
  • What exactly are you storing in the database? seconds since the epoch? milliseconds since the epoch? Something else?
    – Mureinik
    Jul 30, 2015 at 13:59
  • Please show the code fragment where you bind the parameters in Java.
    – mustaccio
    Jul 30, 2015 at 15:16
  • @mustaccio why voted down ?
    – santiago92
    Jul 30, 2015 at 15:18
  • @mustaccio Ok thx code fragment is added.
    – LoremIpsum
    Jul 31, 2015 at 6:25
  • @Mureinik , In the DB2 DB it is a Default TIMESTAMP if i view it with a db2 programm it shows the following data as a timestamp for example: 2015-07-31-13.37.01.000000
    – LoremIpsum
    Jul 31, 2015 at 6:27

1 Answer 1

3

Because TEST_UNITS is a BigInt in DB2, you need to use the JDBC API getLong() to fetch it. getInt() fetches an Integer and hence this would result in a possible data conversion issue and loss of precision/data.

The line

msuSaver[i] = rs.getInt(1); 

would need to be

msuSaver[i] = rs.getLong(1); 

I didn't look over the entire code whether you would need to change msuSaver and related formatting.

2
  • Thank you verry much that fixed it ! :)
    – LoremIpsum
    Jul 31, 2015 at 8:53
  • 1
    @WarGodNT - you'd also want to use prepStmt.setTimestamp() instead of prepStmt.setString() to avoid type conversion problems there.
    – mustaccio
    Jul 31, 2015 at 14:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.