Column Type for mysql float
Posted by: Edu Edu
Date: July 09, 2018 02:01AM


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,

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";

Connection connection = DriverManager.getConnection(connString, "root", "myPassword");

Statement statement = connection.createStatement();

ResultSet resultSet = statement.executeQuery("select * FROM myTable");;

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


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?


Options: ReplyQuote

Written By
Column Type for mysql float
July 09, 2018 02:01AM

Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.