如何从java中的sys.tables获取表名?

时间:2013-05-24 15:03:55

标签: java sql-server eclipse

我们需要在Eclipse中从SQL Server 2005获取表名而不是referenced_object_id。它是这样做的,但没有得到表名:

   Statement smt = con.createStatement();
            smt.executeQuery("SELECT * from table_references");
            ResultSet rs = smt.getResultSet();

            while (rs.next()) { 
                String column1 = rs.getString("referenced_object_id");
                String column2 = rs.getString("name");
                String column3 = rs.getString("parent_column_id");
                String column4 = rs.getString("referenced_column_id");

                String sql = "SELECT name from sys.tables WHERE object_id='" + column1 + "'";

                System.out.println(column1 + " | " + column2 + " | " + column3 + " | " + column4);

1 个答案:

答案 0 :(得分:1)

您需要从数据库中获取元数据才能获取表的名称。

DatabaseMetaData meta = conn.getMetaData();  
ResultSet rs = meta.getTables(null, null, "%", null);
while (rs.next()) {
  System.out.println(rs.getString(3));
}

请查看此处 - http://docs.oracle.com/javase/6/docs/api/java/sql/DatabaseMetaData.html#getTables%28java.lang.String,%20java.lang.String,%20java.lang.String,%20java.lang.String%5b%5d%29 - 它会对您有帮助。