调用 derby (java db)“显示表”来自 jdbc

发布于 2024-12-04 14:09:38 字数 1912 浏览 0 评论 0原文

我需要在 Java 程序中使用 JDBC 枚举 Derby(又名 Java DB)数据库中的表。我只知道执行此操作的是 SHOW TABLES 命令。

我首先尝试了与此类似的东西...

String strConnectionURL = "jdbc:derby:/path/to/derby/database;create=false";
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
Connection connection = DriverManager.getConnection(strConnectionURL);
Statement statement = connection.createStatement();
boolean boResult = statement.execute("SHOW TABLES");
if (boResult) {
    System.out.println("yay!");
}

...但这引发了一个异常:

java.sql.SQLSyntaxErrorException: Syntax error: Encountered "SHOW" at line 1, column 1.

所以接下来我想也许我需要使用 CallableStatement 所以我尝试了这个...

String strConnectionURL = "jdbc:derby:/path/to/derby/db;create=false";
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
Connection connection = DriverManager.getConnection(strConnectionURL);
CallableStatement statement = connection.prepareCall("SHOW TABLES");
boolean boResult = statement.execute();
if (boResult) {
    System.out.println("yippee!");
}

...但这引发了相同的异常:

java.sql.SQLSyntaxErrorException: Syntax error: Encountered "SHOW" at line 1, column 1.

所以,谁能帮我从 JDBC 枚举 Derby (Java DB) 数据库中的表?

编辑:我环顾四周,开始感觉到这可能是一个常见的 JDBC 问题。换句话说,我们可以/愿意使用可从 Connection 对象检索的 DatabaseMetaData 对象枚举所有数据库的表。对此进行调查(并期待回复)...

编辑 2:我找到了一个纯 JDBC 解决方案,但仍然很高兴听到替代方案...

String strConnectionURL = "jdbc:derby:/path/to/db;create=false";
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
Connection connection = DriverManager.getConnection(strConnectionURL);
DatabaseMetaData dbmd = connection.getMetaData();
ResultSet resultSet = dbmd.getTables(null, null, null, null);
while (resultSet.next()) {
    String strTableName = resultSet.getString("TABLE_NAME");
    System.out.println("TABLE_NAME is " + strTableName);
}

I need to enumerate the tables in a Derby (aka Java DB) database using JDBC in a Java program. All I am aware of for doing this is the SHOW TABLES command.

I first tried with something similar to this...

String strConnectionURL = "jdbc:derby:/path/to/derby/database;create=false";
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
Connection connection = DriverManager.getConnection(strConnectionURL);
Statement statement = connection.createStatement();
boolean boResult = statement.execute("SHOW TABLES");
if (boResult) {
    System.out.println("yay!");
}

...but that throws an exception:

java.sql.SQLSyntaxErrorException: Syntax error: Encountered "SHOW" at line 1, column 1.

So next I thought maybe I needed to use a CallableStatement so I tried this...

String strConnectionURL = "jdbc:derby:/path/to/derby/db;create=false";
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
Connection connection = DriverManager.getConnection(strConnectionURL);
CallableStatement statement = connection.prepareCall("SHOW TABLES");
boolean boResult = statement.execute();
if (boResult) {
    System.out.println("yippee!");
}

...but that throws the same exception:

java.sql.SQLSyntaxErrorException: Syntax error: Encountered "SHOW" at line 1, column 1.

So, can anyone help me enumerate the tables in my Derby (Java DB) database from JDBC?

EDIT: I'm looking around and starting to get a feeling this may be a general JDBC question. In other words, one could/would enumerate all a db's tables with the DatabaseMetaData object that can be retrieved from the Connection object. Looking into that (and looking forward to responses)...

EDIT 2: I found a pure JDBC solution, but am still happy to hear alternatives...

String strConnectionURL = "jdbc:derby:/path/to/db;create=false";
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
Connection connection = DriverManager.getConnection(strConnectionURL);
DatabaseMetaData dbmd = connection.getMetaData();
ResultSet resultSet = dbmd.getTables(null, null, null, null);
while (resultSet.next()) {
    String strTableName = resultSet.getString("TABLE_NAME");
    System.out.println("TABLE_NAME is " + strTableName);
}

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

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

发布评论

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

评论(3

晚风撩人 2024-12-11 14:09:38

Show Tables 是一个 ij 命令,而不是基本 SQL 语句,因此您不能直接执行它。正如您在“编辑 2”中所述,您可以使用 DatabaseMetaData 来执行此操作。其他两种方法是:您可以从系统目录中进行选择(请参阅 http://db.apache.org/derby/docs/10.8/ref/rrefsistabs24269.html) ,或者您可以使用“ij.runScript”方法来运行ij 工具,并在程序中传递“显示表”命令(请参阅 http://db.apache.org/derby/docs/10.8/publishedapi/jdbc3/org/apache/derby/tools/ij.html)

Show Tables is an ij command, not a base SQL statement, so you can't directly execute it. As you noted in your "EDIT 2", you can use the DatabaseMetaData to do this. Two other ways to do it are: you can select from the system catalogs (see http://db.apache.org/derby/docs/10.8/ref/rrefsistabs24269.html) , or you can use the "ij.runScript" method to run the ij tool from within your program, and pass it the "show tables" command (see http://db.apache.org/derby/docs/10.8/publishedapi/jdbc3/org/apache/derby/tools/ij.html)

星星的軌跡 2024-12-11 14:09:38

正如 Bryan 建议ij.runScript -代码如下所示:

public void showTbls() throws Exception{
    String sqlIn = "SHOW TABLES;";
    InputStream stream = new ByteArrayInputStream(sqlIn.getBytes(StandardCharsets.UTF_8));
    ij.runScript(conn,stream,StandardCharsets.UTF_8.name(), System.out,"UTF-8");
    stream.close();
}

假设 conn 是一个打开的 derby 连接

但缺点是你只能得到字符串输出。不是您从以下位置获得的 ResultSet:

Statement stmt = conn.createStatement();
ResultSet results = stmt.executeQuery("SELECT * FROM sys.systables");

或者如果您只需要用户表名称,您可以使用以下 SQL:

ResultSet results = stmt.executeQuery("SELECT TABLENAME FROM SYS.SYSTABLES WHERE TABLETYPE='T'");

As Bryan suggested ij.runScript - the code would look like this:

public void showTbls() throws Exception{
    String sqlIn = "SHOW TABLES;";
    InputStream stream = new ByteArrayInputStream(sqlIn.getBytes(StandardCharsets.UTF_8));
    ij.runScript(conn,stream,StandardCharsets.UTF_8.name(), System.out,"UTF-8");
    stream.close();
}

assumming conn is a opened derby Connection

But the disadvantage is that you are getting only string output. Not an ResultSet as you would get from:

Statement stmt = conn.createStatement();
ResultSet results = stmt.executeQuery("SELECT * FROM sys.systables");

or if you want only user table names you can use following SQL:

ResultSet results = stmt.executeQuery("SELECT TABLENAME FROM SYS.SYSTABLES WHERE TABLETYPE='T'");
知足的幸福 2024-12-11 14:09:38

非常相似的输出
显示表格;
可以通过使用以下符合 jdbc 的查询来生成:

    SELECT TABLENAME, (SELECT SCHEMANAME  
            FROM SYS.SYSSCHEMAS  
            WHERE SYS.SYSTABLES.SCHEMAID = SYS.SYSSCHEMAS.SCHEMAID) 
    AS SCHEMANAME
    FROM SYS.SYSTABLES WHERE TABLETYPE='T'

它还显示每个 TABLE 条目可能有用的 SCHEMA 信息。跳过
表类型='T'
如果您还想查看数据库的系统表,正如用户之前已经提到的那样。

A very similar output to
SHOW TABLES;
can be produced by using the following jdbc compliant query:

    SELECT TABLENAME, (SELECT SCHEMANAME  
            FROM SYS.SYSSCHEMAS  
            WHERE SYS.SYSTABLES.SCHEMAID = SYS.SYSSCHEMAS.SCHEMAID) 
    AS SCHEMANAME
    FROM SYS.SYSTABLES WHERE TABLETYPE='T'

It also shows you the probably useful SCHEMA information for each TABLE entry. Skip
TABLETYPE='T'
if you also want to see the system tables of your database as the user before has mentioned already.

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