java推断数据库是不是存在的代码:
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语法:
String sql = "SELECT COUNT(*) FROM information_schema.schemata WHERE schema_name=\"" + database + "\"";
更多java学问请关注java基础教程栏目。
以上就是java推断数据库是不是存在的细致内容,更多请关注ki4网别的相干文章!