当前位置: 代码迷 >> J2SE >> DB2 JDBC failed on sum a DOUBLE TYPE 202601.84 + 249494.82=452096.660000003解决办法
  详细解决方案

DB2 JDBC failed on sum a DOUBLE TYPE 202601.84 + 249494.82=452096.660000003解决办法

热度:515   发布时间:2016-04-24 02:19:24.0
DB2 JDBC failed on sum a DOUBLE TYPE 202601.84 + 249494.82=452096.660000003
Hi there,
 My DB2 version is 9.5, and I use db2 JDBC (v9.5fp5 or v9.7fp2) for developing.
 Both the two JDBC have a problem while SUM DOUBLE TYPE number.
 For example, 202601.84 + 249494.82, in DB2 client or Quest Cental for DB2 I got the right answer 452096.66,
 but in JAVA or DbVisualizer 6.5.7(also using JDBC driver) I got 452096.66000000003!

Who can explain why?


My following code is here:
CREATE
  TABLE TEST_
  (
  ID INTEGER NOT NULL,
  PRICE DOUBLE,
  PRIMARY KEY (ID)
  );
insert into TBMDEV.TEST_ (ID, PRICE) values (1, 202601.84);
insert into TBMDEV.TEST_ (ID, PRICE) values (2, 249494.82);



public class DB2JDBCConnection {
static {
try {
Class.forName("com.ibm.db2.jcc.DB2Driver");
} catch (ClassNotFoundException e) {
System.err.println(e);
System.exit(1);
}
}

public static void main(String args[]) {
String url = "jdbc:db2://192.168.0.1:50000/dev";
String uname = "dev", psswrd = "dev";

String query = " select sum(price) from dev.test_ ";

try {

Connection conn = DriverManager.getConnection(url, uname, psswrd);

Statement stmt = conn.createStatement();

ResultSet rs = stmt.executeQuery(query);

// Loop through the result and print it out
System.out.println(query);
System.out.println("----------------------------");

while (rs.next()) {
System.out.print(rs.getString(1) + " \t");
}

rs.close();
stmt.close();
conn.close();
} catch (SQLException e) {
System.out.println("SQL Exception: ");
System.err.println(e.getMessage());
}
}// main
} // class

------解决方案--------------------
Java code
rs.getDouble(1)
  相关解决方案