First, you have to check if the database exists or not and then use conditional branching to drop database if it exists.
ResultSet resultSet = connection.getMetaData().getCatalogs();
//iterate each catalog in the ResultSet
while (resultSet.next()) {
  // Get the database name, which is at position 1
  String databaseName = resultSet.getString(1);
  if(databaseName.equals("library")) {
  System.out.print("yes");
  Statement stmt = connection.createStatement();
      
      String sql = "DROP DATABASE library";
      stmt.executeUpdate(sql);
  }
}