Big trouble storing BigDecimals in JDBC
Posted in 2000
Topics: Connectivity: ODBC / JDBC / .NET, Java & JDBC Development, Third-Party Tools & Monitoring
Hi! (this message appears also in idn.informix.com - sorry for the duplication) I'm having trouble storing BigDecimals on DECIMAL and FLOAT columns. When I INSERT a row with "0.1" what gets in the table is "1.0". I've done some testing and here are some examples: Inserting 0.1 stores 1.0 Inserting 0.2 stores 2.0 Inserting 0.5 stores 0.0 !! Inserting 0.9 stores 9.0 I've turned on PROTOCOLTRACE and apparently the JDBC driver sends the right data to the server but the server misinterprets it. I send a small program that illustrates the problem after this message. If anyone has some info on this problem or workaround please share! Thanks in advance, Fernando ----------- Program starts here --------- import com.informix.jdbc.*; import java.sql.*; import java.math.BigDecimal; /** * Title: * Description: * Copyright: Copyright (c) 2000 * Company: * @author ferdez@despodata.pt / ferdez@iapmei.pt * @version 1.0 */ public class Test3 { static Object num; static Connection connection=null; static PreparedStatement pst = null; static Statement st = null; public Test3() { } public static void main(String[] args) { Test3 test1 = new Test3(); setConnection(); runTest(); showResults(); closeConnection(); } public static void runTest() { System.out.println("Exemplo"); String deleteStr = "delete from testjdbc"; try { System.out.println("Deleting..."); pst = connection.prepareStatement(deleteStr); pst.executeUpdate(); } catch (java.sql.SQLException ex) { System.out.println("Error in delete: "+ ex.getMessage()); } String insertStr = "insert into testjdbc ( numdecimal, numfloat) values ( ?, ? )"; System.out.println("Inserting..."); try { pst = connection.prepareStatement(insertStr); } catch (java.sql.SQLException ex) { System.out.println("Error in prepare insert: "+ ex.getMessage()); } for (double i=0.0 ; i<=3 ; i=i+0.1) { num = new BigDecimal(i); System.out.println("num: " + num); try { pst.setObject(1, num); pst.setObject(2, num); pst.executeUpdate(); } catch (java.sql.SQLException ex) { System.out.println("Error in insert: "+ ex.getMessage()); } } } public static void showResults() { Object resultNum; String strSel = "select numDecimal, serialkey from testjdbc order by serialkey"; //order by a serial key so that we see what was inserted try { st = connection.createStatement(); ResultSet rs = st.executeQuery(strSel); while ( rs.next() ) { resultNum= rs.getObject("numDecimal"); System.out.println("Value: " + resultNum.toString() ); } } catch (java.sql.SQLException ex) { System.out.println("Error in select:" + ex.getMessage() ); } } public static void setConnection() { String url = "jdbc:informix-sqli://dpinid03.iapmei.pt:1526/jdbctest:INFORMIXSERVER=teste; TRACE=2;TRACEFILE=c:\\\\temp\\\\jdbctrace.log;PROTOCOLTRACE=2;PROTOCOLTRACEFILE= c:\\\\temp\\\\jdbcprot.log"; try { Class.forName("com.informix.jdbc.IfxDriver"); System.out.println("Opening db connection"); connection = DriverManager.getConnection(url, "informix", "secret"); System.out.println(connection.getMetaData().getURL()); System.out.println("Driver: " + connection.getMetaData().getDriverName() + " " + connection.getMetaData().getDriverVersion()); } catch (ClassNotFoundException ex) { System.err.println("Cannot find the database driver classes."); System.err.println(ex); System.exit(0); } catch (SQLException ex) { System.err.println("Cannot connect to this database."); System.err.println(ex); } } public static void closeConnection() { try { connection.close(); } catch (SQLException ex) { System.err.println("Cannot connect to this database."); System.err.println(ex); } } }