简体   繁体   English

Java jdbc 检查表是否存在

[英]Java jdbc check if table exists

I'm trying to check if the table I created already exists.我正在尝试检查我创建的表是否已经存在。 Somehow it always goes into the if (as if the table does not exist).不知何故,它总是进入 if (好像表不存在)。 And it drops the current table and creates a new one.它会删除当前表并创建一个新表。 What I'd like it to do is, it shouldn't drop the table if the table is already there.我想做的是,如果桌子已经在那里,它不应该丢弃桌子。

 public void createTable() throws SQLException {
        L.info("Creating tables...");
        L.info("Check if already there...");
        DatabaseMetaData dbm = connection.getMetaData();
        ResultSet tables = dbm.getTables(null, null, "videogamestable", null);
        if (!tables.next()) {

            System.out.println("Table doesn't exist so creating it...");
            L.info("Table videogamestable does not yet exists, creating it and all others");
            Statement statement = connection.createStatement();
            statement.execute("DROP TABLE IF EXISTS videogamestable");
            String createQuery = "CREATE TABLE videogamestable " +
                    "(id INTEGER NOT NULL IDENTITY," +
                    "name VARCHAR(45) NOT NULL," +
                    "price DOUBLE," +
                    "releaseDate DATE," +
                    "genre VARCHAR(15)," +
                    "founder VARCHAR(25)," +
                    "minimumRequiredAge INTEGER," +
                    "rating DOUBLE)";
            statement.execute(createQuery);
            statement.close();
            //PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO videogamestable VALUES (NULL, ?, ?, ?)");
            for (VideoGame videoGame : Data.getData()) {
                insert(videoGame);
            }
            System.out.println("Database aangemaakt");
            //preparedStatement.close();
        } else {
            System.out.println("Table already exists");
            L.info("Table videogamestable does already exist!");
            //throw new VideoGameException();
        }
    }

Taken from this SO question , you may define a helper method which can check for the existence of the table:取自这个 SO question ,您可以定义一个可以检查表是否存在的辅助方法:

public boolean tableExists(String tableName, Connection conn) {
    boolean found = false;
    DatabaseMetaData databaseMetaData = conn.getMetaData();
    ResultSet rs = databaseMetaData.getTables(null, null, tableName, null);
    while (rs.next()) {
        String name = rs.getString("TABLE_NAME");
        if (tableName.equals(name)) {
            found = true;
            break;
        }
    }

    return found;
}

Note that the third parameter to DatabaseMetaData#getTables() is actually a pattern, so it is possible that multiple table names matching it could be found and returned in the result set.请注意, DatabaseMetaData#getTables()的第三个参数实际上是一个模式,因此可能会在结果集中找到并返回多个与其匹配的表名。 Therefore, we iterate the entire result set and do an equality check against the exact table name being passed to the helper method.因此,我们迭代整个结果集并对传递给辅助方法的确切表名进行相等性检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM