
- Example - Home
- Example - Environment
- Example - Strings
- Example - Arrays
- Example - Date & Time
- Example - Methods
- Example - Files
- Example - Directories
- Example - Exceptions
- Example - Data Structure
- Example - Collections
- Example - Networking
- Example - Threading
- Example - Applets
- Example - Simple GUI
- Example - JDBC
- Example - Regular Exp
- Example - Apache PDF Box
- Example - Apache POI PPT
- Example - Apache POI Excel
- Example - Apache POI Word
- Example - OpenCV
- Example - Apache Tika
- Example - iText
- Java Useful Resources
- Java - Quick Guide
- Java - Useful Resources
How to edit(Add or update) columns of a Table and how to delete a table in Java
Problem Description
How to edit(Add or update) columns of a Table and how to delete a table?
Solution
Following example uses create, alter & drop SQL commands to create, edit or delete table.
import java.sql.*; public class jdbcConn { public static void main(String[] args) throws Exception { Class.forName("org.apache.derby.jdbc.ClientDriver"); Connection con = DriverManager.getConnection( "jdbc:derby://localhost:1527/testDb","username", "password"); Statement stmt = con.createStatement(); String query ="CREATE TABLE employees(id INTEGER PRIMARY KEY, first_name CHAR(50), last_name CHAR(75))"; stmt.execute(query); System.out.println("Employee table created"); String query1 = "aLTER TABLE employees ADD address CHAR(100) "; String query2 = "ALTER TABLE employees DROP COLUMN last_name"; stmt.execute(query1); stmt.execute(query2); System.out.println("Address column added to the table & last_name column removed from the table"); String query3 = "drop table employees"; stmt.execute(query3); System.out.println("Employees table removed"); } }
Result
The above code sample will produce the following result. The result may vary.
Employee table created Address column added to the table & last_name column removed from the table Employees table removed from the database
java_jdbc.htm
Advertisements