JDBC Tutorial on JDBC Drop Table Example

this chapter provides an example on how to delete a table using jdbc application. before executing the following example, make sure you have the following in place −

  • to execute the following example you can replace the username and password with your actual user name and password.

  • your mysql or whatever database you are using, is up and running.

note reformatting jdbc tutorial this is a serious operation and you have to make a firm decision before proceeding to delete a table, because everything you have in your table would be lost.

required steps

the following steps are required to create a new database using jdbc application −

  • import the packages − requires that you include the packages containing the jdbc classes needed for database programming. most often, using import java.sql.* will suffice.

  • open a connection − requires using the drivermanager.getconnection() method to create a connection object, which represents a physical connection with a database server.

  • execute a queryreformatting jdbc tutorial requires using an object of type statement for building and submitting an sql statement to drop a table in a seleted database.

  • clean up the environment reformatting jdbc tutorial try with resources automatically closes the resources.

sample code

copy and paste the following example in jdbcexample.java, compile and run as follows −

import java.sql.connection;
import java.sql.drivermanager;
import java.sql.sqlexception;
import java.sql.statement;

public class jdbcexample {
   static final string db_url = "jdbc:mysql://localhost/tutorialspoint";
   static final string user = "guest";
   static final string pass = "guest123";

   public static void main(string[] args) {
      // open a connection
      try(connection conn = drivermanager.getconnection(db_url, user, pass);
         statement stmt = conn.createstatement();
      ) {		      
         string sql = "drop table registration";
         stmt.executeupdate(sql);
         system.out.println("table deleted in given database...");   	  
      } catch (sqlexception e) {
         e.printstacktrace();
      } 
   }
}

now let us compile the above example as follows −

c:\>javac jdbcexample.java
c:\>

when you run jdbcexample, it produces the following result −

c:\>java jdbcexample
table deleted in given database...
c:\>