Java and MySql Check if database exists

0 votes
What is the code in java to check if a database exists in Mysql exists or not?
Aug 27, 2019 in Java by Ritu
3,814 views

1 answer to this question.

0 votes

You can get the list of databases from the MetaData of the Database.

Class.forName("com.mysql.cj.jdbc.Driver");

         Connection con = DriverManager.getConnection("jdbc:mysql://localhost/mysql?user=root&password=edureka");

ResultSet resultSet = connection.getMetaData().getCatalogs();

//iterate each catalog in the ResultSet

while (resultSet.next()) {

  // Get the database name, which is at position 1

  String databaseName = resultSet.getString(1);

System.out.print(databaseName);

}

Hope this helps!

Check out Java certification course to become certified expert.

Thanks!

answered Aug 27, 2019 by Kunal
In the database URL what is the database name i should provide?or is there some trick I don't know.

Hey, @There,

JDBC URL provides a way of identifying a database so that the appropriate ... After the driver is loaded, an application must specify the correct database connection URL to connect.

The server and port specify the hostname (or IP address) and port number where the server is listening for requests and databaseName is the name of the database you want to connect to. The URLAttributes can be either Derby embedded or network client attributes.

Related Questions In Java

0 votes
1 answer

Java and MySql: Delete database if exists

First, you have to check if the ...READ MORE

answered Sep 30, 2019 in Java by Kiran
1,829 views
0 votes
4 answers

How do I check if a file exists in Java?

To test to see if a file ...READ MORE

answered Dec 29, 2020 in Java by Rajiv
• 8,910 points
56,940 views
0 votes
3 answers

Check if a String is numeric in Java

Java 8 Lambda Expression is used: String someString ...READ MORE

answered Sep 3, 2018 in Java by Daisy
• 8,120 points
3,334 views
0 votes
3 answers

How to check whether a file exists or not in Java?

Using nio we can check whether file ...READ MORE

answered Aug 14, 2018 in Java by Sushmita
• 6,910 points
3,484 views
0 votes
1 answer

Accessing connection from different class in Java / MySQL?

You should just instantiate DoComms with every ...READ MORE

answered Nov 14, 2018 in Database by nirvana
• 3,130 points
1,632 views
0 votes
3 answers

MySQL "Could not create connection to database serve" error

Pls check that you have MySQL server ...READ MORE

answered Jul 3, 2020 in Database by anonymous
29,198 views
0 votes
1 answer

How to connect Java to MySql Database?

This will work: try { ...READ MORE

answered Aug 27, 2019 in Java by John
911 views
0 votes
1 answer

How to create database in MySql using Java?

First, you will have to create connection ...READ MORE

answered Aug 27, 2019 in Java by Juni
525 views
0 votes
1 answer

Java and MySql: Check if database exists

You can get the list of databases ...READ MORE

answered Sep 30, 2019 in Java by Tina
2,649 views
0 votes
1 answer

Java and MySql: Delete database if exists

First, you have to check if the ...READ MORE

answered Aug 27, 2019 in Java by Varun
566 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP