java判断数据库是否存在
内容摘要
java判断数据库是否存在的代码:public static boolean isExistDatabase(String database) {
Connection conn = null;
Statement stmt = null;
Resu
Connection conn = null;
Statement stmt = null;
Resu
文章正文
java判断数据库是否存在的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | 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); } } } |
关键SQL语法:
1 | String sql = "SELECT COUNT(*) FROM information_schema.schemata WHERE schema_name=" " + database + " "" ; |
更多java知识请关注java基础教程栏目。
代码注释
[!--zhushi--]