问题描述
您好。下面是一个简单的JDBC程序,用于插入和提取
的数值。当调用ResultSet.getDouble()时,
同一程序在solaris上产生的输出不同于Linux上的输出。我很感激
对此的讨论!
谢谢,
BEA Systems的Joe Weinstein
Linux Box上的结果
----------------------------------- ------------------------------------
-bash-2.05b $ java db2
08.02.0001
数据库版本是08.02.0000
测试值1.001
本机驱动程序对象是一个类java.math.BigDecimal
的值是1.001
作为getDouble它是1.0010000000000001<<<<<<<<<< ;<看到这个!
本机驱动程序对象是一个类java.math.BigDecimal
值是1.001
作为getDouble它是1.0010000000000001< <<<<<<<<<看到这个!
double d = 1.001d给出1.001
new Double(1.001)。doubleValue()给出1.001
他们是等于。
新BigDecimal(1.001d)给出1.000999999999999889865875957184471189975738525390 6
25
新BigDecimal(新的Double(" 1.001"))。 doubleValue())给出1.000999999999999889865875957184471189975738525390 625
Solaris Box结果
----------------------- -------------------------------------------------- -
bash-3.00 $ java db2
08.02.0002
数据库版本是08.02.0000
测试值1.001
本机驱动程序对象是类java.math.BigDecimal
值是1.001
作为getDouble它是1.001<< ;<<<<<<<<不同!
本机驱动程序对象是类java.math.BigDecimal
值是1.001<<<<<<<<<<< <不同!
as getDouble它是1.001
double d = 1.001d给出1.001
新Double(1.001)。 doubleValue()给出1.001
它们是相等的。
新BigDecimal(1.001d)给出1.000999999999999889865875957184471189975738525390 625
新BigDecimal(new Double(" 1.001"。。doubleValue())给出1.000999999999999889865875957184471189975738525390 625
程序:
----------- -------------------------------------------------- -----------------------------
import java.io. *;
import java.util。*;
import java.net。*;
import java.sql。*;
import weblogic.common。*;
公共类db2
{
public static void main(String argv [])
抛出异常
{
连接c = null;
尝试
{
java.util.Properties props = new java.util.Properties();
驱动程序d =(驱动程序)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());
语句s = c.createStatement();
try {s.executeUpdate(" drop table joe") ;} catch(异常忽略){}
s.executeUpdate(" create table joe(bar decimal(4,3))");
s.executeUpdate (插入乔值(1.001));
PreparedStatement p = c.prepareStatement(" insert into joe values(?)");
p.setDouble(1,new Double(" 1.001")。doubleValue());
p.executeUpdate();
System.out .println("测试值" + new Double(" 1.001")。doubleValue());
ResultSet r = s.executeQuery(" select * from joe");
while(r.next())
{
System.out.println(本机驱动程序对象是+ r。 getObject(1).getClass());
System.out.println(" value is" + r.getObject(1));
System.out .println(" as getDouble it''s" + r.getDouble(1) ); b / b $
double d = 1.001d;
double dd = new Double(1.001)。 doubleValue();
System.out.println(" double d = 1.001d给出" + d);
System.out.println(" new Double(\" 1.001 \")。doubleValue()给出" + dd);
if(d == dd)
System.out.println(它们相等。);
其他
System.out.println(它们不相等。);
System.out.println(" new BigDecimal(1.001d)给出+ + new BigDecimal( 1.001d));
System.out.println(" new BigDecimal(new Double(\" 1.001 \")。doubleValue())给出
+ new BigDecimal(new Double(" 1.001")。doubleValue()));
}
catch(例外e)
{e.printStackTrace(); }
终于
{try {c.close();} catch(例外e){}}
}
}
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){} }
}
}
推荐答案
这篇关于IBM JDBC驱动程序在Linux上的行为与在Solaris上的行为不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!