x Java Java 8 JUnit JSON
  • XML
  • JDBC Spring Boot Microservices React Contact Us

    Java JDBC create table example

    In this example we will show how to create a table in MYSQL DB using Java JDBC. We will first create Statement object from Connection object and then call executeUpdate on it with the CREATE TABLE DDL query.

    import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;import java.sql.Statement;public class TableCreater {		private static final String CREATE_TABLE_SQL = "CREATE TABLE STUDENT"            + "("            + " ID INT NOT NULL,"            + " NAME varchar(100) NOT NULL,"            + " ROLL_NUMBER numeric(10) NOT NULL,"            + " CREATED_DATE timestamp,"            + " PRIMARY KEY (ID)"            + ")";    public static void main(String[] args) {        try (Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/world",             "root", "root");            Statement stmt = con.createStatement()) {            stmt.executeUpdate(CREATE_TABLE_SQL);            System.out.println("Table STUDENT created succesfully");        } catch (SQLException e) {            System.out.println(e);        }    }} 

    Console Output :

    Table STUDENT created succesfully 

    If you run "desc student" sql command, you will see below table details

    Maven Build

    Comments

    Leave a Reply

    Your email address will not be published. Required fields are marked *











    Share This