我有一個SQL查詢來回回,我需要的列名和數據類型,它的表名和模式名從SQL查詢獲取元數據:需要使用Java
Thsi是我使用了使用和測試方法它SQLSERVER:
public static void getMetadataForConn(Connection conn) throws SQLException
{
ResultSet rs = null;
try
{
Statement stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT AB_DEMO_SRC.dbo.employee.dept_id dept_id, AB_DEMO_SRC.dbo.employee.email_add email_add, AB_DEMO_SRC.dbo.employee.emp_address emp_address, AB_DEMO_SRC.dbo.employee.emp_id emp_id, AB_DEMO_SRC.dbo.employee.emp_name emp_name FROM AB_DEMO_SRC.dbo.employee ");
ResultSetMetaData md = rs.getMetaData();
int columnCount = md.getColumnCount();
for (int i = 1; i <= columnCount; i++)
System.out.print(md.getColumnName(i) + "(" + md.getColumnType(i) + ") "+md.getSchemaName(i)+"."+md.getTableName(i));
System.out.println();
while (rs.next())
{
for (int i = 1; i <= columnCount; i++)
{
Object o = rs.getObject(i);
System.out.print(null == o ? "" : o.toString() + " ");
}
System.out.println();
}
}
finally
{
if (null != rs)
{
rs.close();
}
}
}
我得到像數據類型,精度和scale..Strangely其他元數據的細節,我得到的表名和架構名稱爲「」那是blank..IS有任何其他方式來獲取Query中列的元數據?
通過添加sqljdbc41.jar作爲well..which提到這罐子爲JDK7和jre7試過了,並且它具有JDBC API方法實施..我真的不明白爲什麼同樣的方法返回TableName和ScehmaName爲Teradata和DB2,但不是爲SQL Server和Oracle – user2176576 2015-03-25 13:08:23