JDBC – Create Table in Database

Create table: (static query)

  • execute() method of Statement object is used to execute DDL commands in database from java application.
  • It returns boolean value after executing the query
package online;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class CreateTable
            public static void main(String args[]) throws Exception
                        Connection conn = null;
                        String driver = “oracle.jdbc.driver.OracleDriver”;
                                    System.out.println(“Driver loaded”);
                                    String url = “jdbc:oracle:thin:@localhost:1521:xe”;
                                    String uname = “system”;
                                    String pwd = “admin”;
                                    conn = DriverManager.getConnection(url, uname, pwd);
                                    System.out.println(“Connection is ready”);     
                                    Statement stmt = conn.createStatement();
                                    String query = “create table account(num number(5), name varchar2(30), balance number(7))”;
                                    System.out.println(“Table created successfully”);
                                    if(conn != null)
                                                System.out.println(“Connection closed”);

Leave a Comment

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

Scroll to Top