databasemetadata.getimportedkeys()抛出where子句中的列“referenced\u table\u name”不明确

mrwjdhj3  于 2021-06-20  发布在  Mysql
关注(0)|答案(0)|浏览(276)

我正在尝试使用databasemetadata检索mysql数据库上表的键,并尝试使用informationschema来实现这一点。mysql配置属性

import java.sql.*

class DataLoader {

    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"
    static final String DB_URL = "jdbc:mysql://localhost:3306/mydb?useInformationSchema=true"
    static final String USER = "user"
    static final String PASS = "password"
    static final String DB = "mydb"

    public static void main(String[] args) {
        Connection conn = null
        Statement stmt = null
        try {
            Class.forName(JDBC_DRIVER)
            conn = DriverManager.getConnection(DB_URL, USER, PASS)

            println "Database Product Name: " + conn.getMetaData().getDatabaseProductName()
            println "Database Product Version: " + conn.getMetaData().getDatabaseProductVersion()
            println "JDBC Driver: " + conn.getMetaData().getDriverName()
            println "Driver Version: " + conn.getMetaData().getDriverVersion()

            ResultSet importedKeysRS = conn.getMetaData().getImportedKeys(DB, null, "user")

        } catch (Exception e) {
            e.printStackTrace()
        } finally {
            try {
                if (stmt != null) {
                    stmt.close()
                    conn.close()
                }
            } catch (SQLException se) {
            }// do nothing
            try {
                if (conn != null)
                    conn.close()
            } catch (SQLException se) {
                se.printStackTrace()
            }
        }
    }
}

当我执行上面的groovy代码时,抛出的异常如下。

Database Product Name: MySQL
Database Product Version: 5.6.15-rel63.0-log
JDBC Driver: MySQL-AB JDBC Driver
Driver Version: mysql-connector-java-5.1.6 ( Revision: ${svn.Revision} )
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'REFERENCED_TABLE_NAME' in where clause is ambiguous
    at com.mysql.jdbc.DatabaseMetaDataUsingInfoSchema.executeMetadataQuery(DatabaseMetaDataUsingInfoSchema.java:50)
    at com.mysql.jdbc.DatabaseMetaDataUsingInfoSchema.getImportedKeys(DatabaseMetaDataUsingInfoSchema.java:780)

当我移除 useInformationSchema=true 属性,它工作时没有错误,但执行时间太长 getImportedKeys() 如何启用 useInformationSchema ?
如果我不能使用 useInformationSchema ,我可以做什么来进行查询 getImportedKeys() 以及 getExportedKeys() 跑得更快?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题