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

Java determines whether the database exists

Release: 2019-12-28 10:42:46
Original
3260 people have browsed it

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);
            }
        }
    }
Copy after login

Key SQL syntax:

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

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template