列出数据库中的所有表

时间:2018-10-28 16:00:30

标签: java sql jdbc ucanaccess

我正在使用 ucanaccess 来管理Java中的SQL数据库。 我已使用问题的答案-setting up ucanaccess

完成了初始设置

设置后,我可以成功 创建表,插入表 ,但是,我无法列出现有表。

我正在附上我正在使用的代码-

public class Driver {
public static void main(String[] args) {
    try {
        Connection myconnection = DriverManager.getConnection("jdbc:ucanaccess:///usr/local/var/mysql/library/database.accdb");
        Statement stmt = myconnection.createStatement();

        stmt.executeQuery("create table books ( BarCode VARCHAR(255) NOT NULL, title VARCHAR(255) NOT NULL, author VARCHAR(255), PRIMARY kEY (BarCode) );");
        stmt.executeQuery("Insert INTO books VALUES(\"123\", \"Monk\", \"Robin\")");
        ResultSet res = myconnection.getMetaData().getTables(null, null, "books", new String[] {"TABLE"});
        while (res.next()) {
            System.out.println(
                    "   "+res.getString("TABLE_CAT")
                            + ", "+res.getString("TABLE_SCHEM")
                            + ", "+res.getString("TABLE_NAME")
                            + ", "+res.getString("TABLE_TYPE")
                            + ", "+res.getString("REMARKS"));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

列出所有表名的部分从答案中使用-List all the tables

感谢您的帮助。

编辑::我试图检查数据库中是否存在“书” ,因此我在中给出了“书” 搜索中的 tableNamePattern 字段。在@Andreas指出之后,我对其进行了修改。

1 个答案:

答案 0 :(得分:0)

@AKSW指出,我应该使用 executeUpdate 而不是 executeQuery

在示例here中,为DDL和查询指定了使用不同功能的命令。

相关问题