Home Tutorial Java Jdbc Mysql JDBC Drop Table Example

 
 

JDBC Drop Table Example
Posted on: October 15, 2010 at 12:00 AM
Advertisement
In this tutorial we will learn how drop Table from Database. This tutorial first create database "testjdbc" if not exist and create a table "user" under "testjdbc" database

JDBC Drop Table Example:

  In this tutorial we will learn  how drop Table from Database. This tutorial  first  create database "testjdbc" if not exist and create a table "user" under "testjdbc"  database.

        First  we create "DropTable.java " class that used for drop table. We create  class variable  that use for connection to the database,  driverName, url, dbName, userName , 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 = "";

Again create the Connection con , Statement stmt

Connection con;
Statement stmt;

   Now given below  "DropTable.java "  class that defined how connect to database and drop table. Drop table before connect the database  if not find database give Exception  "Unknown database 'testjdbc'" and if not  find Table user get  error message "Table not deleted" and "Unknown table 'user'  ". So if not fined any database or table first cerate and again drop table. The  query applied drop table as:  "DROP Table user"

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.SQLException;

public class DropTable{
	// 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 Table user";
				stmt.executeUpdate(query);
				System.out.println("Table deleted successfully...");				
			   } catch(SQLException s){
					System.out.println("Table not deleted ");		
					s.printStackTrace();
				 }
			// close Connection
			con.close();
			}catch (Exception e){
				e.printStackTrace();
			 }
	}
}

Program Output :

F:\jdbc>javac DropTable.java

F:\jdbc>java DropTable
Table deleted successfully...

F:\jdbc>java DropTable
Table not deleted
com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Unknown table 'user'

Download Code

Advertisement

Related Tags for JDBC Drop Table Example:


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: October 15, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com