Java Program to Delete a Column in a Table Using JDBC
Last Updated :
14 Aug, 2021
Before deleting a column in a table, first is need to connect the java application to the database. Java has its own API which JDBC API which uses JDBC drivers for database connections. Before JDBC, ODBC API was used but it was written in C which means it was platform-dependent. JDBC API provides the applications to JDBC connection and JDBC driver provides manager to driver connection.
Algorithm: Always remember these 7 thumb golden steps to deal with JDBC in order to deal with the database and between App(Main) class and connection class of it.
- Importing the database
- Registering the Java class
- Establishing a connection
- Creating a statement
- Executing the query
- Process the results
- Closing connection
Procedure: to delete a column in a table from the database created using JDBC is as follows:
Step 1: Loading “mysqlconnector.jar” in the program.
Step 2: Creating a database and adding a table with records
- Using MySQL
- Further 5 steps are demonstrated in the java program. (App class or Main class) of the Connection class.
Implementation: Java program using JDBC to delete a Column in a Table
Java
import java.sql.*;
public class GFG {
static final String url
public static void main(String[] args) throws ClassNotFoundException
{
try {
Class.forName( "com.mysql.jdbc.Driver" );
Connection conn = DriverManager.getConnection(
url, "root" , "1234" );
Statement stmt = conn.createStatement();
String query
= "ALTER TABLE student Drop address" ;
int result = stmt.executeUpdate(query);
if (result > 0 )
System.out.println(
"A column from the table is deleted." );
else
System.out.println( "unsuccessful deletion " );
conn.close();
}
catch (SQLException e) {
System.out.println(e);
}
}
}
|
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...