Column Type for mysql float
Posted by:
Edu Edu
Date: July 09, 2018 02:01AM
Hello,
when I use the Connector/J driver, I get a java.sql.Types code 7 (incorrect?) with calling getColumnType() for a mysql float data type. I created a simple example to reproduce this: (the interesting field is the "myFloat" field).
CREATE TABLE `myTable` (
`id` int(11) NOT NULL,
`myval` varchar(45) DEFAULT NULL,
`myFloat` float DEFAULT NULL,
`myDouble` double DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
The Java test program:
public static void main(String[] args) throws Exception {
String driver = "com.mysql.cj.jdbc.Driver";
String connString = "jdbc:mysql://localhost:3306/mydb?useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
Class.forName(driver);
Connection connection = DriverManager.getConnection(connString, "root", "myPassword");
Statement statement = connection.createStatement();
//Query
ResultSet resultSet = statement.executeQuery("select * FROM myTable");
resultSet.next();
ResultSetMetaData rsmd = resultSet.getMetaData();
//Get number of columns returned
int numOfCols = rsmd.getColumnCount();
//Print out type for each column
for(int i=1; i<=numOfCols; ++i)
{
System.out.println("Column [" + i + "] data type: " + rsmd.getColumnTypeName(i) + ", code: " + rsmd.getColumnType(i) + ", " + resultSet.getObject(i).getClass());
}
//Close DB connection
statement.close();
connection.close();
}
The output:
Column [1] data type: INT, code: 4, class java.lang.Integer
Column [2] data type: VARCHAR, code: 12, class java.lang.String
Column [3] data type: FLOAT, code: 7, class java.lang.Float
Column [4] data type: DOUBLE, code: 8, class java.lang.Double
So the FLOAT gets java.sql.Types code 7. But in java.sql.Types I see:
public final static int FLOAT = 6;
public final static int REAL = 7;
public final static int DOUBLE = 8;
Thus, the mysql DOUBLE data type seems to get the correct code (8 in java.sql.Types), but the mysql FLOAT data type should be 6, not 7. Is this a bug?
Using mysql-connector-java-8.0.11.jar and Java 8. With older versions of mysql-connector-java this also happens. Is this a bug?
Thanks,
Edu