How to check whether database exists?

How to check whether database exists?

Hi,

I have to create a database in mysql. I am able to create that successfully from using JDBC. But before to creating the database, i want to check whether database exists or not? How can i do this check. can any one help me on this.

Please find thesample code

`public class CreateDatabase {  public static void main(String[] args) {
Connection connection = null;
Statement statement = null;
try {
  Class.forName("org.gjt.mm.mysql.Driver").newInstance();
  String url = "jdbc:mysql://localhost/mysql";
  connection = DriverManager.getConnection(url, "username", "password");

  statement = connection.createStatement();
  String hrappSQL = "CREATE DATABASE hrapp";
  statement.executeUpdate(hrappSQL);
} catch (Exception e) {
  e.printStackTrace();
} finally {
  if (statement != null) {
    try {
      statement.close();
    } catch (SQLException e) {
    } // nothing we can do
  }
  if (connection != null) {
    try {
      connection.close();
    } catch (SQLException e) {
    } // nothing we can do
  }
}

}

} `

Thanks, Narayan

View Answers

November 13, 2010 at 5:30 PM

Hi Friend,

You can use the following code:

import java.sql.*;
import java.util.*;
class CheckDatabase{
    public static void main(String[] args) throws Exception{
        ArrayList<String> list=new ArrayList<String>();
        Scanner input=new Scanner(System.in);
        System.out.println("Enter Database name:");
        String database = input.nextLine();
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306","root","root");
        Statement st = con.createStatement();
        DatabaseMetaData meta = con.getMetaData();
        ResultSet rs = meta.getCatalogs();
        while (rs.next()) {
            String listofDatabases=rs.getString("TABLE_CAT");
            list.add(listofDatabases);
        }
        if(list.contains(database)){
        System.out.println("Database already exists");
            }
            else{
            st.executeUpdate("CREATE DATABASE "+database);
            System.out.println("Database is created");
            }
            rs.close();
        }

    }

Thanks









Related Tutorials/Questions & Answers:
How to check whether database exists?
Check Whether Record is already Exists or Not with Database Connectivity - Java Beginners
Advertisements
Check whether the number is palindrome or not
Check whether highest number is even or odd
how to check username & password from database using jsp
Check whether the sum of alternate digit of a number is palindrome or not.
JSP check email exists or not
java program to compare two hashmaps and check whether they are equal or not
java program to check whether a number is pallindrome or not using recursion???????
java program to check whether a number is pallindrome or not using recursion???????
java program to check whether a number is pallindrome or not using recursion???????
java program to check whether a number is pallindrome or not using recursion???????
java program to check whether a number is pallindrome or not using recursion???????
java program to check whether a number is pallindrome or not using recursion???????
java program to check whether a number is pallindrome or not using recursion???????
Program to check whether two given words pronouncing same or not?
how to check dates while retrieving data from database
how to ckeck whether the date has expired - JDBC
How to Check Automorphic Number using Java Program
check all database stored procedures - JDBC
how to check the radio button automatically depending up on the value of database when editing the form...
check radio button on retrieving the value from database.
check box realtive information in page store in database
Check Properties
Java Check Permutations of String
how to insert check box
how to check gremlin version
How to check a checkbox - Struts
How to check spark version
How to check spark version
How to check if a pid is alive?
Mysql Check
How to check no records - JSP-Servlet
how to check a remote applications validity
How to check keras version in anaconda
How to check Hadoop version in Ubuntu?
Check Perfect Number in Java Program
To Determine whether the Session is New or Old
how to normalize a database schema
check
How to check a file exists or not in java.
How to check a file is hidden or not in java
how to insert data into database using jsp & retrive
how to show data in database ?
How to check a file is read-able or not in java
How to store image into database
How to access the database from JSP?
how to check the size of a tar file in linux
How to check text in textbox using JavaScript
How to check CentOS version via command line?

Ads