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?
Sep 30 in Java by slayer
• 29,170 points
21 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);

}

answered Sep 30 by Tina

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 in Java by Kiran
16 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,050 points
726 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,880 points
218 views
0 votes
2 answers

What's the best way to check if a String represents an integer in Java?

You can also use regular expression. str.matches("-?\\d+"); It will ...READ MORE

answered Aug 9, 2018 in Java by Daisy
• 8,050 points
168 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,060 points
40 views
0 votes
2 answers

MySQL "Could not create connection to database serve" error

When I am trying to connect netbeans ...READ MORE

answered Apr 14 in Database by anonymous

edited Apr 15 by Kalgi 2,002 views
0 votes
1 answer

How to connect Java to MySql Database?

This will work: try { ...READ MORE

answered Aug 27 in Java by John
47 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 in Java by Juni
29 views
0 votes
1 answer

Java and MySql: Check if database exists

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

answered Aug 27 in Java by Kunal
62 views
0 votes
1 answer

Java and MySql: Delete database if exists

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

answered Aug 27 in Java by Varun
29 views