Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8967481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:15:11+00:00 2026-06-15T17:15:11+00:00

I have a big question… I have a database java program creation. I want

  • 0

I have a big question…
I have a database java program creation.

I want to know if the database exists or not, and the if exists just connect, if not to create it.

I tried this one:

if (dbName.exists() == false) {}

THIS IS ALL THE CODE…

Class.forName("com.mysql.jdbc.Driver");
System.out.println("MySQL JDBC driver loaded ok.");

THIS IS A BACKUP CODE FOR IT, JUST TO WORK FOR NOW….
PARTIAL CODE THAT WORKS !

conn = DriverManager.getConnection(DBurl + url
+ "?createDatabaseIfNotExist=true& + "
+ "useUnicode=true&characterEncoding=utf-8&user="
+ userName + "&&password=" + password);


System.out.println("Connected to database ");           
System.out.println("Connected to the database " + url);

BUT I WANT SOMETHING LIKE:

FILE dbName = new FILE (url);
Statement stmt = new Statement;

if (dbName.exists() == true)
   System.out.println("Database exists ! Connecting ... ");
else {
   String sql = "CREATE DATABASE "+url;
   stmt.executeUpdate (sql);
}

I don’t want to put the url with the password and username in the same place… because they are provided from an external part, but that is allready implemented and working.

So I want to rip in 2 peaces, 1 Connect “jdbc:mysql://localhost:3306/”; WITHOUT URL which is the database NAME …
AND THEN IF A DATABASE DOES NOT EXISTS THERE WITH THAT NAME JUST CREATE ON.

It is not working…. not entering in the else more, and says that Exeption Database already exists.

Thanks you very much.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T17:15:12+00:00Added an answer on June 15, 2026 at 5:15 pm

    If it is a MySQL database, the following code should work. Other databases may give a different error code, but the general way should be clear. Important is that you connect to the instance, not a specific database initially. For creating the tables, you will need to connect to the newly created database. You can’t use the instance connection that I use in my example for creating the tables:

        Connection connection = null;
        Statement statement = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://localhost/",
                    "root", "admin");
            statement = connection.createStatement();
            String sql = "CREATE DATABASE DBNAME";
            //To delete database: sql = "DROP DATABASE DBNAME";
            statement.executeUpdate(sql);
            System.out.println("Database created!");
        } catch (SQLException sqlException) {
            if (sqlException.getErrorCode() == 1007) {
                // Database already exists error
                System.out.println(sqlException.getMessage());
            } else {
                // Some other problems, e.g. Server down, no permission, etc
                sqlException.printStackTrace();
            }
        } catch (ClassNotFoundException e) {
            // No driver class found!
        }
        // close statement & connection
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Big O notation question. Say I have a Java program that
I know this is a big question and it's not a yes or no
I have a big question about how newsstand works. When you have to create
I have a big question.. In many books of Java and SUN Microsystems is
I have a big question that has been puzzling me for a long time
If you have seen my previous questions, you'd already know I am a big
I have big set of urls and I want to implement an autocompletion. I
Hy, I have a very big question . I have an array like this
I know this is a BIG question, but i am looking to integrate magento
I know its a big question but I'm a complete beginner. I've limited experience

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.