English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

JDBC Create Table

This chapter provides examples of how to create tables using JDBC applications. Before executing the following examples, please ensure that you have the following conditions:

  • To execute the following example, you can useUsernameAndPasswordReplace with the actual username and password.

  • Your MySQL or any database you are using has been started and is running.

Necessary steps

To create a new database using a JDBC application, you need to perform the following steps-

  • Import package:It requires you to include the software package that contains the JDBC classes required for database programming. Typically, use import java.sql.* That is enough.

  • Register JDBC driver: It requires you to initialize the driver so that you can open a communication channel with the database.

  • Establish connection:It is necessary to use DriverManager.getConnection() A method to create a Connection object that represents a physical connection to the database server.

  • Execute query: It is necessary to use an object of type Statement to construct and submit SQL statements to create tables in the selected database.

  • Clean up environment: All database resources must be explicitly closed, rather than relying on JVM's garbage collection.

Sample code

Copy and paste the following example into JDBCExample.java, compile and run as follows-

//Step1.Import the required packages
import java.sql.*;
public class JDBCExample {
   // JDBC driver name and database URL
   static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
   static final String DB_URL = "jdbc:mysql://localhost/STUDENTS";
   //  Database credentials
   static final String USER = "username";
   static final String PASS = "password";
   
   public static void main(String[] args) {
   Connection conn = null;
   Statement stmt = null;
   try{
      //Step2:Register JDBC driver
      Class.forName("com.mysql.jdbc.Driver");
      //Step3:Establish connection
      System.out.println("Connecting to a selected database...");
      conn = DriverManager.getConnection(DB_URL, USER, PASS);
      System.out.println("Connected database successfully...");
      
      //Step4:Execute query
      System.out.println("Creating table in given database...");
      stmt = conn.createStatement();
      
      String sql = "CREATE TABLE REGISTRATION" +
                   "(id INTEGER not NULL," +
                   "first VARCHAR("255)," + 
                   "last VARCHAR("255)," + 
                   "age INTEGER," + 
                   "PRIMARY KEY (id)"; 
      stmt.executeUpdate(sql);
      System.out.println("Created table in given database...");
   catch(SQLException se){
      //Handling JDBC errors
      se.printStackTrace();
   }
      //Handling Class.forName error
      e.printStackTrace();
   }
      //Used for closing resources
      try{
         if(stmt!=null)
            conn.close();
      catch(SQLException se){
      }// Nothing to do
      try{
         if(conn!=null)
            conn.close();
      catch(SQLException se){
         se.printStackTrace();
      }
   }//End try
   System.out.println("Goodbye!");
}//End main
}//End JDBCExample

Now, let's compile the above example as follows:

C:\>javac JDBCExample.java
C:\>

RuntimeJDBCExampleIt will produce the following result-

C:\>java JDBCExample
Connecting to a selected database...
Connected database successfully...
Creating table in given database...
Created table in given database...
Goodbye!
C:\>