Color me silly, but what do you think will happen when you try to store a floating point number in to a numeric/decimal numeric string?

 

The numeric string isn’t a floating point number. It looks like your floating point number is being round down when it’s automatically converted to a decimal string.

 

When you insert it as ’11.11’ it is a numeric string so it gets inserted correctly.

 

Does that make sense?

 

 


From: Carlos Eduardo Santin [mailto:carlos.santin@endeeper.com]
Sent: Tuesday, January 20, 2009 2:23 PM
To: derby-user@db.apache.org
Subject: Numeric Field Precision on JavaDB

 

Hi everybody,

 

Have someone had a problem like this one before?

 

If I have a simple table like:

 

create table test(id integer, percent numeric(5,2))

 

And I use a java code to insert data into this table:

 

////////////////////////////////////////////////////////////////////////////////

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.PreparedStatement;

 

public class TestJavaDB{

 

 public TestJavaDB(){
  try{
   executeTest();
  } catch (SQLException e){
   e.printStackTrace();
  }
 }

 

 public void executeTest() throws SQLException{
  Connection conn = DriverManager.getConnection("jdbc:derby:C:/Derby20090108/data/", "root", "root");
  
  Float fltValue = 11.11F;

 

  String strQuery = "INSERT INTO test VALUES (?, ?)";

 

  PreparedStatement pstmt = conn.prepareStatement(strQuery);
  pstmt.setInt(1,new Integer(2));
  pstmt.setFloat(2,fltValue);

 

  pstmt.executeUpdate();

 

  pstmt.close();
  conn.close();
 }

 

 public static void main(String[] args){
  new TestJavaDB();
 }
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

 

The value inserted into the field percent in the database is 11.10 and not 11.11 as it would be.

 

But if I insert this data directly in the database using a script:

 

INSERT INTO test VALUES (1, 11.11);

 

The value appears correctly.

 

Is there any bug related to the derby jdbc library??

 

 

Thanks in advance,

Carlos