Hello,
I was testing some code of a colleague who used ResultSetMetaData which was retrieved with Statement.getMetaData() from a select statement. ResultSetMetaData.getColumnType(i) returned 2 (Numeric) for the SQL type Number. My test code used DatabaseMetaData retrieved from Connection.getMetaData(). DatabaseMetaData.getColumns returned 3 (Decimal) for SQL type Number.
Here is the a code snippet to illustrate the issue:
// The database should contain a table MY_TABLE with a column of type 'Number'
String user = "user";
String password = "password";
String host = "host"
String port = "port";
String sid = "sid";
DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
Connection connection =
DriverManager.getConnection("jdbc:oracle:thin:@" + host + ":" + port + ":" + sid, user, password);
// Get SQL type with DatabaseMetaData
DatabaseMetaData metaData = connection.getMetaData();
ResultSet tables = metaData.getTables(null, user, null, new String[] { "TABLE" });
// MY_Table should be the first table
if (tables.next()) {
String tableName = tables.getString(3);
log.info(tableName);
ResultSet columns = metaData.getColumns(null, user, tableName, null);
while (columns.next()) {
String columnName = columns.getString(4);
String columnType = columns.getString(5);
System.out.println(columnName + " " + columnType);
// Column type 3
}
columns.close();
}
tables.close();
// Get SQL type with ResultSetMetaData
Statement statement = connection.createStatement();
String sql = "SELECT * FROM MY_TABLE";
statement.execute(sql);
ResultSet resultSet = statement.getResultSet();
ResultSetMetaData metaData2 = resultSet.getMetaData();
for (int i = 1; i <= metaData2.getColumnCount(); i++) {
System.out.println(metaData2.getColumnName(i) + " " + metaData2.getColumnType(i));
// Column type is 2
}
Is there a reason for the discrepancy?
Regards,
Johannes