This tutorial describe how to alter existing table using JDBC API.
This tutorial describe how to alter existing table using JDBC API.This tutorial describe how to alter existing table using JDBC API.
Alter Table :
Alter table means editing the existing table. You can add new field, drop
unused field and alter properties of field.
We can change the structure of table student in the following ways -
Example : In this example we are adding new field 'location' to the table 'student'.
package jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; class AlterTable { public static void main(String[] args) { System.out.println("Table Alter Example!"); Connection con = null; String url = "jdbc:mysql://localhost:3306/"; String dbName = "students"; String driverName = "com.mysql.jdbc.Driver"; String userName = "root"; String password = "root"; try { // Creating db connection Class.forName(driverName); con = DriverManager.getConnection(url + dbName, userName, password); try { Statement st = con.createStatement(); String sql = "ALTER TABLE student add location varchar(30)"; st.executeUpdate(sql); System.out.println("Table is altered successfully"); } catch (SQLException s) { System.out.println(s); } con.close(); } catch (Exception e) { e.printStackTrace(); } } }
Output :
Table Alter Example! Table is altered successfully
Now Your table looks like this -