SQL DDL with JDBC Statements

There are five different types of SQL statements. SQL stands for Structured Query Language.
In this post we are considering MySQL as example.

#1.DDL, Data Definition Language:
Below SQL statements are considered as DDL
CREATE DATABASE
CREATE TABLE
ALTER
DROP

#2.DML, Data Manipulation Language:
Below SQL statements are considered as DML
INSERT
UPDATE
DELETE
TRUNCATE

#3.DQL, Data Query Language:
Below SQL statement are considered as DQL
SELECT

#4.TCL, Transaction Control Language:
Below SQL statements are considered as TCL
COMMIT
ROLLBACK

#5.DCL, Data Control Language:
Below SQL statements are considered as DCL
GRANT
REVOKE

Can DDL statements be invoked from JDBC?
Yes, DDL statements like CREATE, ALTER, DROP can be executed from JDBC Statement. MySQL JDBC jar need to be added to Project, for this Program to connect to Database.

Below is an example, which is self explanatory

import java.sql.*;

public class DDLJDBC {

    public static void main(String[] args)
    {
    try
    {
        Class.forName("com.mysql.jdbc.Driver");

        System.out.println("Connecting to database...");

        Connection con = DriverManager.getConnection("jdbc:mysql://localhost/mondaybt",
                "myuser","mypassword");

        System.out.println("Creating Statement...");

        Statement stmt = con.createStatement();
        stmt.execute("create table institute(id bigint, sname varchar(50), saddr varchar(50))");

        System.out.println("Creating Table...");
        stmt.execute("alter table institute add column sfather_name varchar(100)");

        System.out.println("Altering Table...");

        stmt.close();
        con.close();
    }
    catch(SQLException se)
    {
        se.printStackTrace();
    }
    catch(Exception ep)
    {
        ep.printStackTrace();
    }
    }
}

You may also like to Read:
What is JDBC?