Java and MySql Delete database if exists

0 votes
I am building a Java application where I am giving admin to reset the database. When the user chooses to reset the database, I want to reset all the contents of the database. For this I want to drop the database and then create database with new tables. I want to know the code to implement execution of drop query if a database exists by a particular name.
Sep 30, 2019 in Java by slayer
• 29,370 points
2,238 views

1 answer to this question.

0 votes

First, you have to check if the database exists or not and then use conditional branching to drop database if it exists.

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);

  if(databaseName.equals("library")) {

  System.out.print("yes");

  Statement stmt = connection.createStatement();

      

      String sql = "DROP DATABASE library";

      stmt.executeUpdate(sql);

  }

}
answered Sep 30, 2019 by Kiran

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 Aug 27, 2019 in Java by Varun
825 views
0 votes
1 answer

Java and MySql: Check if database exists

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

answered Aug 27, 2019 in Java by Kunal
4,473 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,976 views
0 votes
1 answer

How to connect Java program to the MySQL database?

You can connect your Java code with ...READ MORE

answered May 11, 2018 in Java by Parth
• 4,640 points
1,882 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,917 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,841 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
1,124 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
747 views
+1 vote
3 answers

What is the syntax to declare and initialize an array in java?

You can use this method: String[] strs = ...READ MORE

answered Jul 25, 2018 in Java by samarth295
• 2,220 points
3,530 views
0 votes
2 answers

How can I create File and write data in it using Java?

import java.io.BufferedWriter; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; public class WriteFiles{ ...READ MORE

answered Jul 26, 2018 in Java by samarth295
• 2,220 points
2,890 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