这是一个ResultSetMetaData
对象如何告诉您您查询的表的示例:
import java.sql.*;
public class JDBCQuery {
public static void main(String args[]) {
Connection conn = null;
Statement s = null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection(
"jdbc:odbc:Driver={SQL Server};" +
"Server=.\\SQLEXPRESS;" +
"Trusted_Connection=yes;" +
"Database=myDb");
s = conn.createStatement();
s.executeQuery("SELECT * FROM dbo.SalesSummary WHERE 0 = 1");
ResultSet rs = s.getResultSet();
ResultSetMetaData rsmd = rs.getMetaData();
for (int i = 1; i <= rsmd.getColumnCount(); i++) {
System.out.println(String.format("-- Column %d --", i));
System.out.println(String.format("Column name: %s", rsmd.getColumnName(i)));
System.out.println(String.format("Database-specific type name: %s", rsmd.getColumnTypeName(i)));
System.out.println(String.format("Column size (DisplaySize): %d", rsmd.getColumnDisplaySize(i)));
System.out.println(String.format("java.sql.Type of column: %d", rsmd.getColumnType(i)));
System.out.println();
}
} catch( Exception e ) {
e.printStackTrace();
} finally {
try {
if (s != null) {
s.close();
}
if (conn != null) {
conn.close();
}
} catch( Exception e ) {
e.printStackTrace();
}
}
}
}
这是输出:
-- Column 1 --
Column name: Item
Database-specific type name: nvarchar
Column size (DisplaySize): 255
java.sql.Type of column: -9
-- Column 2 --
Column name: FiscalYear
Database-specific type name: int
Column size (DisplaySize): 11
java.sql.Type of column: 4
-- Column 3 --
Column name: Quarter
Database-specific type name: int
Column size (DisplaySize): 11
java.sql.Type of column: 4
-- Column 4 --
Column name: Sales
Database-specific type name: money
Column size (DisplaySize): 21
java.sql.Type of column: 3
此处java.sql.Types
提供了完整列表。