使用 Java 从 Oracle 获取表名?

发布于 2024-12-10 13:16:05 字数 614 浏览 1 评论 0原文

我正在开发一个 Java GUI 项目;在其中一个属性屏幕中,我使用 3 个组合框来获取:第一个用户选择数据库名称,该值是 2.模式名称,以及该模式下的所有表名称。当我从数据库获取值并设置它们时使用 ResultSet 是可以的。但通过这种方式,我只能带来一个值,因为你知道 ResultSet 的工作方式。那么有没有其他方法可以获取所有表名,例如:

SELECT TABLE_NAME FROM ALL_TABLES;并将它们放在字符串或其他数组中......任何可能的方式,我只想拥有他们的名字在列表或任何东西中。

谢谢。

编辑:

这是为此目的测试的简单代码:

ResultSet rs = null;
PreparedStatement ps = null;
String result;
ps = conn.prepareStatement("SELECT TABLE_NAME FROM ALL_TABLES");

rs = ps.executeQuery(); 

while(rs.next())
{
        result = rs.getString(1);
    System.out.println(rs.getString(1));
}

I'm working on a Java GUI project; in one of its property screen I use 3 comboboxes in order to get: first user chooses DB name, with this value 2.Schema names and with this all table names under that schema. It's OK when I use ResultSet when getting the values from DB and setting them. But with this way, I could only bring ONE value because you know the way ResultSet works. So is there any other way that I could fetch all the table names with let's say:

SELECT TABLE_NAME FROM ALL_TABLES; and put them on an array of Strings or sth... Anything possible, I just wanna have their names in a list or anything.

Thank you.

EDIT:

Here's the simple code tested for the purpose:

ResultSet rs = null;
PreparedStatement ps = null;
String result;
ps = conn.prepareStatement("SELECT TABLE_NAME FROM ALL_TABLES");

rs = ps.executeQuery(); 

while(rs.next())
{
        result = rs.getString(1);
    System.out.println(rs.getString(1));
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

他夏了夏天 2024-12-17 13:16:05

不确定“你知道结果集的工作方式”是什么意思?!这将返回查询中的所有行:

while (rs.next()) {
    System.out.print("Column 1 returned ");
    System.out.println(rs.getString(1));
}

Not sure what you mean by 'you know the way resultset works'?! This will return all the rows from the query:

while (rs.next()) {
    System.out.print("Column 1 returned ");
    System.out.println(rs.getString(1));
}
香草可樂 2024-12-17 13:16:05

您基本上需要查询元数据。请参阅此示例了解如何执行此操作。

You need to quesry for meta data basically. See this example on how to do it.

内心荒芜 2024-12-17 13:16:05

只需使用列表:

List<String> tableNames = new ArrayList<String>();
while(rs.next()) {
    String tableName = rs.getString(1);
    tableNames.add(tableName);
}
// there: your list contains all the table names

阅读有关集合的教程。这是所有 Java 开发人员都必须了解的内容。

Just use a List:

List<String> tableNames = new ArrayList<String>();
while(rs.next()) {
    String tableName = rs.getString(1);
    tableNames.add(tableName);
}
// there: your list contains all the table names

Read the tutorial about collections. This is stuff that all Java developers must know.

随心而道 2024-12-17 13:16:05

您还可以使用:

DatabaseMetaData  metadata = currentConnection.getMetaData();
String[] names = {"TABLE"}; 
ResultSet tables = metadata.getTables(null,"%", "%", names);
while (tables.next()) { 
  String tableName = tables.getString("TABLE_NAME"); 
  String tableSchema = tables.getString("TABLE_SCHEM");
}

阅读此 了解更多信息。

You could also use:

DatabaseMetaData  metadata = currentConnection.getMetaData();
String[] names = {"TABLE"}; 
ResultSet tables = metadata.getTables(null,"%", "%", names);
while (tables.next()) { 
  String tableName = tables.getString("TABLE_NAME"); 
  String tableSchema = tables.getString("TABLE_SCHEM");
}

Read this for more info.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文