Share on Google+Share on Google+

Narayanan
How to check whether database exists?
1 Answer(s)      6 years and 5 months ago
Posted in : Vurduk mu Şamarı Oynar Gotünün Damar

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

Ads

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:
Advertisements
Tutorials   
Java Spring Hibernate Struts Training how to retrieve text and images from mysql database and show on html page using jsp servlet How to install Neo4j on Ubuntu 15.10? please help me in these prog MYSQL - mysql copy table to another table example by creating new table solution for mapping hibernate and pojos from net beans through database Connect GWT application with Oracle inventory System of Medical Store inventory System of Medical Store inventory System of Medical Store inventory System of Medical Store This code have created jTable that connected with a database.every cell of last column have a button.But this button doesn't work properly.Where is the error java program Java Database Program jdbc Database connection class in a live project ..?? What is JDBC architecture? Unable to Create DSN for excel to ms access importing data and edit that data Database Scrolling Buttons how to use foreign key in java How to delete content of a log file from linux terminal? JSP-Oracle connectivity Student Management System Java code for Saving Marathi (Indian local language ) in Mysql and retrieving the same crud operations with foreign key constraint. I really need help with this assignment question Please help me out Please Backup Failed Mysqldump Got Errno 28 On Write Upgrade MySQL 5.1 to 5.5 in cpanel mysqldump all databases com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'jpa.sequence' doesn't exist persistence xml class element persistence.xml for MySQL MySQL JDBC Driver pom dependency code Blob column jdk6 and oracle 8i connexion oracle 8i et jdk6 java [ cannot retrive date from sql ] why?? Order Entry System How to threow timeout exception when DB is not responding How to threow timeout exception when DB is not responding This is the employee form to add data to mysql database but I got error of NullPointerException, Is this write code please give me feedback as soon as possible make a method which takes user name & pwd as parameter & validate them against the database. If the corresponding user exist return true otherwise false. prabhakar validating credentials and displaying error message in login .jsp if not valid JDBC jdbc jdbc code register servlet vibhu connectiondetails from vibhu

Ads

 
Advertisement null

Ads