Alter Query in Database (Add or Delete Column)

Add Column:


import java.sql.*;
class Alter_Add
{
      public static void main ( String args[] )
      {
            try
            {
                  Class.forName ( "com.mysql.jdbc.Driver" );
                  Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/db_name","root","abcd");
                  PreparedStatement del = con.prepareStatement ( "alter table student add email varchar(20);" );
                  del.executeUpdate();
                  con.close();
            }
            catch ( Exception e )
            {
                  System.out.println ( e );
            }
      }
}


Delete Column:

import java.sql.*;
class Alter_Del
{
      public static void main ( String args[] )
      {
            try
            {
                  Class.forName ( "com.mysql.jdbc.Driver" );
                  Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/db_name","root","abcd");
                  PreparedStatement del = con.prepareStatement ( "alter table student drop email;" );
                  del.executeUpdate();
                  con.close();
            }
            catch ( Exception e )
            {
                  System.out.println ( e );
            }
      }
}

Post a Comment

0 Comments