Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Jdbc Jdbc-mysql Deleting a Table from Database

Related Tutorials


 
 

Share on Google+Share on Google+

Deleting a Table from Database

Advertisement
Imagine a situation where we need to delete a table from the database. We can do it very easily by using the commands in the MySQL database.

Deleting a Table from Database

     

Imagine a situation where we need to delete a table from the database. We can do it very easily by using the commands in the MySQL database. But how we can delete the table using java methods and API. In this section we are describing, how to delete a table from database using java methods. Java provides the facility for deleting a specific table from a given database with the help of some specified methods. See detailed information given below:

Description of program:

Create a class DeleteTable inside which, firstly establishes the connection with MySQL database. After establishing the connection we will delete a table from specific database. If the table which we want to delete get deletes then we will print the message "Table Deletion process is completely successfully!", otherwise it will display " Table is not exists!".

Description of code:

DROP TABLE table_name:
Above code is used for deleting any table from a given database.

Here is the code of program:

import java.sql.*;

public class DeleteTable{
  public static void main(String[] args) {
  System.out.println("Tabel Deletion Example");
  Connection con = null;
  String url = "jdbc:mysql://localhost:3306/";
  String dbName = "jdbctutorial";
  String driverName = "com.mysql.jdbc.Driver";
  String userName = "root";
  String password = "root";
  try{
  Class.forName(driverName).newInstance();
  con = DriverManager.getConnection(url+dbName, userName, password);
  try{
  Statement st = con.createStatement();
  st.executeUpdate("DROP TABLE Employee1");
  System.out.println ("Table Deletion process is completly successfully!");
  }
  catch(SQLException s){
  System.out.println("Table is not exists!");
  }
  con.close();
  }
  catch (Exception e){
  e.printStackTrace();
  }
  }
} 

Download this example.

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 13, 2007

Related Tutorials

Discuss: Deleting a Table from Database   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
arpit khandelwal
April 13, 2011
java code to drop table

java code to drop table from data base in NWDS J2EE PESPECTIVE. WE ARE GETTING ERROR AS DROP NOT SUPPORTED
DMCA.com