Home >Java >JavaBase >Java determines whether the database exists

Java determines whether the database exists

尚
Original
2019-12-28 10:42:463269browse

Java determines whether the database exists

java code to determine whether the database exists:

public static boolean isExistDatabase(String database) {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;// 数据库结果集
        try {
            conn = getConnection();
            stmt = conn.createStatement();
            String sql = "SELECT COUNT(*) FROM information_schema.schemata WHERE schema_name=\"" + database + "\"";
            System.out.println(sql);
            rs = stmt.executeQuery(sql);
            if (rs.next()) {
                if (rs.getInt(1) == 0) {
                    return false;
                } else {
                    return true;
                }
            }
            return false;
        } catch (Exception e) {
            throw new TenantException(e.getMessage(), Status.INTERNAL_SERVER_ERROR);
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                throw new TenantException("mysql关闭连接失败:" + e.getMessage(), Status.INTERNAL_SERVER_ERROR);
            }
        }
    }

Key SQL syntax:

String sql = "SELECT COUNT(*) FROM information_schema.schemata WHERE schema_name=\"" + database + "\"";

For more java knowledge, please pay attention to java basic tutorial column.

The above is the detailed content of Java determines whether the database exists. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:How to use t in javaNext article:How to use t in java