Check if Database Exists using Java

We can check if database exists using java.sql.DatabaseMetaData interface. using DatabaseMetaData interface we can obtain the meta data about the database catalog, for example we can obtain the database name, tables available in that database, columns in that table etc.

The getCataLogs() method which is called on the instance of┬áDataBaseMetaData, retrieves the catalog name available in database and return a ResultSet object, the ResultSet object’s each row has a single String column that is a catalog name.

code to check if database exists using java:-


The results are ordered by catalog name and if you don’t want to use rs.getString(1) the you can also write like this rs.getString(“TABLE_CAT”) to check if database exists using java. In the above code we are checking that a particular database exists or not now if you wan to get the list of database catalogs, you can simply print the rs.getString(1) value.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.