In this tutorial, you can learn how drop database if exist use mysql JDBC driver . In this tutorial we create the 'DropDatabase.java' class that drop database if exist another give suggested error message.
In this tutorial, you can learn how drop database if exist use mysql JDBC driver . In this tutorial we create the 'DropDatabase.java' class that drop database if exist another give suggested error message.In this tutorial, you can learn how drop database if exist use mysql JDBC driver . In this tutorial we create the 'DropDatabase.java' class that drop database if exist another give suggested error message.
In this tutorial we used the "com.mysql.jdbc.Driver" mysql Driver, and another url, database name, user name, and password as:
static String driverName = "com.mysql.jdbc.Driver";
static String url = "jdbc:mysql://localhost:3306/"
static String dbName = "testjdbc";
static String userName = "root";
static String password = "";
we used the Connection con, and Statement stmt variable , that defined given below. In this tutorial drop database and if any table exist inside of database also deleted.
Connection con;
Statement stmt;
The code of "DropDatabase.java" given below that appllied query "DROP DATABASE testjdbc" as:
import java.sql.DriverManager; import java.sql.Connection; import java.sql.Statement; import java.sql.SQLException; public class DropDatabase{ // JDBC driver name and database URL static String driverName = "com.mysql.jdbc.Driver"; static String url = "jdbc:mysql://localhost:3306/"; // defined and set value in dbName, userName and password variables static String dbName = "testjdbc"; static String userName = "root"; static String password = ""; public static void main(String[] args){ // create Connection con, and Statement stmt Connection con; Statement stmt; try{ Class.forName(driverName).newInstance(); con = DriverManager.getConnection(url+dbName, userName, password); try{ stmt = con.createStatement(); String query = "DROP DATABASE testjdbc"; stmt.executeUpdate(query); System.out.println("DATABASE deleted successfully..."); } catch(SQLException s){ System.out.println("Database not deleted "); s.printStackTrace(); } // close Connection con.close(); }catch (Exception e){ e.printStackTrace(); } } }
Program Output:
F:\jdbc>javac DropDatabase.java F:\jdbc>java DropDatabase DATABASE deleted successfully... F:\jdbc> |
If not find database "testjdbc" then give error message "Unknown database 'testjdbc' "