0

I already export MySQL database.Now i want to know database name present in dump file through java code.I require this because, if database present in dump file is not present in server then first i want to create database and then import it.I do not know is this possible or not? suggest any answer..

3
  • If your dump has a database creation statement, then why do you need to find the database name? Commented Nov 16, 2013 at 10:34
  • 1
    what kind of command did you use to dump MySQL database, if you are using mysqldump, then the dump file is possibly a txt file, you can read it using Java. As far as I know, mysqldump with default parameters would dump the database name into the file. Commented Nov 16, 2013 at 10:34
  • That is also my point. Simply searching for "CREATE DATABASE X" would solve the problem. But if you apply the whole dump, why bother finding something you already know? Commented Nov 16, 2013 at 10:46

1 Answer 1

1

Put your data base connecting code in try catch. If you get an Unknown database exception then write the code for creation of the data base in catch block

for example:

try{
           System.out.println("Connecting to database");              
           con=DriverManager.getConnection("jdbc:mysql://localhost/iie1","root","");
           System.out.println("Database connected ");
       }catch(Exception e)              
       {
         System.out.println("Creating data base");
        // Code for creating data base
       }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.