Hi. Below is a simple JDBC program to insert and extract
a numerical value. When ResultSet.getDouble() is called,
the same program produces different output on solaris
than it does on Linux. I would be grateful for any
discussion of this!
thanks,
Joe Weinstein at BEA Systems
Results on Linux Box
-----------------------------------------------------------------------
-bash-2.05b$ java db2
08.02.0001
Database version is 08.02.0000
Testing value 1.001
native driver object is a class java.math.BigDecimal
value is 1.001
as getDouble it's 1.0010000000000001 <<<<<<<<<< SEE THIS!
native driver object is a class java.math.BigDecimal
value is 1.001
as getDouble it's 1.0010000000000001 <<<<<<<<<< SEE THIS!
double d = 1.001d gives 1.001
new Double("1.001").doubleValue() gives 1.001
they are equal.
new BigDecimal(1.001d) gives 1.000999999999999889865875957184471189975738525390 6
25
new BigDecimal( new Double("1.001").doubleValue() ) gives 1.000999999999999889865875957184471189975738525390 625
Results on Solaris Box
---------------------------------------------------------------------------
bash-3.00$java db2
08.02.0002
Database version is 08.02.0000
Testing value 1.001
native driver object is a class java.math.BigDecimal
value is 1.001
as getDouble it's 1.001 <<<<<<<<<< DIFFERENT!
native driver object is a class java.math.BigDecimal
value is 1.001 <<<<<<<<<< DIFFERENT!
as getDouble it's 1.001
double d = 1.001d gives 1.001
new Double("1.001").doubleValue() gives 1.001
they are equal.
new BigDecimal(1.001d) gives 1.000999999999999889865875957184471189975738525390 625
new BigDecimal( new Double("1.001").doubleValue() ) gives 1.000999999999999889865875957184471189975738525390 625
The program:
------------------------------------------------------------------------------------------
import java.io.*;
import java.util.*;
import java.net.*;
import java.sql.*;
import weblogic.common.*;
public class db2
{
public static void main(String argv[])
throws Exception
{
Connection c = null;
try
{
java.util.Properties props = new java.util.Properties();
Driver d = (Driver)Class.forName("COM.ibm.db2.jdbc.app.DB2Dri ver").newInstance();
props.put("user", "wls");
props.put("password", "wls");
props.put("DatabaseName", "wls");
String URL = "jdbc:db2:wls1";
c = d.connect(URL, props);
DatabaseMetaData dm = c.getMetaData();
System.out.println(dm.getDriverVersion());
System.out.println("Database version is " + dm.getDatabaseProductVersion() );
Statement s = c.createStatement();
try{s.executeUpdate("drop table joe");}catch (Exception ignore){}
s.executeUpdate("create table joe (bar decimal(4,3))");
s.executeUpdate("insert into joe values(1.001)");
PreparedStatement p = c.prepareStatement("insert into joe values(?)");
p.setDouble(1, new Double("1.001").doubleValue());
p.executeUpdate();
System.out.println("Testing value " + new Double("1.001").doubleValue() );
ResultSet r = s.executeQuery("select * from joe");
while (r.next())
{
System.out.println("native driver object is a " + r.getObject(1).getClass() );
System.out.println(" value is " + r.getObject(1) );
System.out.println("as getDouble it's " + r.getDouble(1) );
}
double d = 1.001d;
double dd = new Double("1.001").doubleValue();
System.out.println("double d = 1.001d gives " + d );
System.out.println("new Double(\"1.001\").doubleValue() gives " + dd );
if (d == dd)
System.out.println("they are equal." );
else
System.out.println("they are not equal." );
System.out.println("new BigDecimal(1.001d) gives " + new BigDecimal(1.001d) );
System.out.println("new BigDecimal( new Double(\"1.001\").doubleValue() ) gives "
+ new BigDecimal( new Double("1.001").doubleValue() ) );
}
catch (Exception e)
{ e.printStackTrace(); }
finally
{ try {c.close();}catch (Exception e){} }
}
}