I encountered a strange JDBC case when I use float values with the connection property useCursorFetch. Setting useCursorFetch to true/false makes getFloat() returning different values with different precision.
Reproduce:
1. Create a table:
CREATE TABLE data_type(
col_float float
);
2. Insert data: (I know float is a single precision type, but in our case, double values are inserted)
INSERT INTO data_type(col_float) VALUES(358294.648941);
INSERT INTO data_type(col_float) VALUES(120263.815023);
3. JAVA code:
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useCursorFetch=false", "user", "password");
PreparedStatement statement = con.prepareStatement("select col_float from data_type;");
System.out.println("Executing query...");
ResultSet rs = statement.executeQuery();
while (rs.next()) {
System.out.println(rs.getDouble(1) );
}
rs.close();
statement.close();
con.close();
When useCursorFetch=false, I got the following output in the console:
358295.0
120264.0
But when I set useCursorFetch=true, I got different values in the console:
358294.66
120263.81
Could somebody please explain why useCursorFetch impacts the values?
Thank you very much.
Reproduce:
1. Create a table:
CREATE TABLE data_type(
col_float float
);
2. Insert data: (I know float is a single precision type, but in our case, double values are inserted)
INSERT INTO data_type(col_float) VALUES(358294.648941);
INSERT INTO data_type(col_float) VALUES(120263.815023);
3. JAVA code:
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useCursorFetch=false", "user", "password");
PreparedStatement statement = con.prepareStatement("select col_float from data_type;");
System.out.println("Executing query...");
ResultSet rs = statement.executeQuery();
while (rs.next()) {
System.out.println(rs.getDouble(1) );
}
rs.close();
statement.close();
con.close();
When useCursorFetch=false, I got the following output in the console:
358295.0
120264.0
But when I set useCursorFetch=true, I got different values in the console:
358294.66
120263.81
Could somebody please explain why useCursorFetch impacts the values?
Thank you very much.