Description:
Try to get column type through ResultSetMetaData.getColumnTypeName(colIndex),
1st,create a connection and call stmt.executeQuery, getColumnTypeName,it will return DOUBLE.
2nd,close the stmt and create,call stmt,executeQuery again , it will return BIGINT.
But if you close the connection and create the connection ,run stmt.executeQuery,then, it will return DOUBLE again.
It seems like the 1st call to ResultSetMetaData.getColumnTypeName(colIndex) in the newly created connection ,will return DOUBLE. But if you close the stmt and create it again in the same connection (do not close connection itselt),it will return BIGINT.
How to repeat:
import java.sql.*;
public class MySQLJDBCTestSingle {
public static String sqlStatement="select * from (SELECT @rownum := @rownum+1 AS rownum,1_test.* FROM (SELECT @rownum:=0) r,1_test) T1639101751247 where 1=0" ;
public static void main(String args[]) {
Connection con ;
ResultSetMetaData resultSetMetaData;
ResultSet rs ;
Statement stmt;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
con = DriverManager
.getConnection("jdbc:mysql://172.16.15.128:3306/avatar?useUnicode=true&autoReconnect=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT%2B8",
"avatar4", "avatar4");
stmt=con.createStatement();;
rs = stmt.executeQuery(sqlStatement);
System.out.println("_____________________");
resultSetMetaData = rs.getMetaData();
System.out.println(resultSetMetaData.getColumnTypeName(1));
System.out.println(resultSetMetaData.getColumnName(1));
System.out.println(resultSetMetaData.getColumnClassName(1));
System.out.println(resultSetMetaData.getPrecision(1));
System.out.println(resultSetMetaData.getScale(1));
System.out.println("_____________________");
rs.close();
stmt.close();
stmt=con.createStatement();;
rs = stmt.executeQuery(sqlStatement);
resultSetMetaData = rs.getMetaData();
System.out.println(resultSetMetaData.getColumnTypeName(1));
System.out.println(resultSetMetaData.getColumnName(1));
System.out.println(resultSetMetaData.getColumnClassName(1));
System.out.println(resultSetMetaData.getPrecision(1));
System.out.println(resultSetMetaData.getScale(1));
System.out.println("_____________________");
rs.close();
stmt.close();
stmt=con.createStatement();;
rs = stmt.executeQuery(sqlStatement);
resultSetMetaData = rs.getMetaData();
System.out.println(resultSetMetaData.getColumnTypeName(1));
System.out.println(resultSetMetaData.getColumnName(1));
System.out.println(resultSetMetaData.getColumnClassName(1));
System.out.println(resultSetMetaData.getPrecision(1));
System.out.println(resultSetMetaData.getScale(1));
System.out.println("_____________________");
rs.close();
stmt.close();
System.out.println("Close connection and create it again:");
con.close();
con = DriverManager
.getConnection("jdbc:mysql://172.16.15.128:3306/avatar?useUnicode=true&autoReconnect=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT%2B8",
"avatar4", "avatar4");
stmt=con.createStatement();;
rs = stmt.executeQuery(sqlStatement);
System.out.println("_____________________");
resultSetMetaData = rs.getMetaData();
System.out.println(resultSetMetaData.getColumnTypeName(1));
System.out.println(resultSetMetaData.getColumnName(1));
System.out.println(resultSetMetaData.getColumnClassName(1));
System.out.println(resultSetMetaData.getPrecision(1));
System.out.println(resultSetMetaData.getScale(1));
System.out.println("_____________________");
rs.close();
stmt.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
System.err.println(e.getClass().getName() + ": " + e.getMessage());
}
}
}
These codes will produce the result which is hard to understand:
_____________________
DOUBLE
rownum
java.lang.Double
92
0
_____________________
BIGINT
rownum
java.lang.Long
19
0
_____________________
BIGINT
rownum
java.lang.Long
19
0
_____________________
Close connection and create it again:
_____________________
DOUBLE
rownum
java.lang.Double
92
0
_____________________
Process finished with exit code 0
Thanks in advance.