调用 derby (java db)“显示表”来自 jdbc
我需要在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
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)
正如 Bryan 建议ij.runScript -代码如下所示:
假设
conn
是一个打开的 derby 连接但缺点是你只能得到字符串输出。不是您从以下位置获得的 ResultSet:
或者如果您只需要用户表名称,您可以使用以下 SQL:
As Bryan suggested ij.runScript - the code would look like this:
assumming
conn
is a opened derby ConnectionBut the disadvantage is that you are getting only string output. Not an ResultSet as you would get from:
or if you want only user table names you can use following SQL:
非常相似的输出
显示表格;
可以通过使用以下符合 jdbc 的查询来生成:
它还显示每个 TABLE 条目可能有用的 SCHEMA 信息。跳过
表类型='T'
如果您还想查看数据库的系统表,正如用户之前已经提到的那样。
A very similar output to
SHOW TABLES;
can be produced by using the following jdbc compliant query:
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.