Java JDBC: Creating a table using JDBC Statement Interface

In this article, we will use JDBC API to create table in MySQL database from Java application (i.e.; using Statement Interface)


1. Pre-requisite:

  • Java JDK 1.8.0_77
  • MySQL database 5.5.16
  • Eclipse Luna IDE 4.4.0
  • mysql-connector-java-5.1.38.jar file


2. Database parameters :

Let us move on and code an example to connect MySQL database from Java application to create a table using JDBC API. But before that, we will list down required things to connect database

  • database server IP or address (localhost)
  • sever port (3306)
  • database name (PLAYER_INFO)
  • username (root)
  • password (root@123)

Note: All bold are database values to connect MySQL database


3. Creating a table using JDBC Statement Interface

  • As we are ready with required things to connect MySQL database from Java application, let us code a simple example using Statement Interface
  • We can use one of the methods from Statement Interface to execute “create table” SQL query
    1. execute(String sqlQuery);
    2. executeUpdate(String sqlQuery);
    3. executeQuery(String sqlQuery);
  • Let us code a simple example using Statement Interface

package in.bench.resources.mysql.db.example;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class CreateTableUsingJDBCStatement {

	public static void main(String[] args) {

		// variables
		Connection connection = null;
		Statement statement = null;

		// Step 1: Loading or registering MySQL JDBC driver class
		try {
		catch(ClassNotFoundException cnfex) {
			System.out.println("Problem in loading MySQL JDBC driver");

		// Step 2: Opening database connection
		try {

			// Step 2.A: Create and get connection using DriverManager
			connection = DriverManager.getConnection(
          "jdbc:mysql://localhost:3306/PLAYER_INFO", "root", "root@123"); 

			// Step 2.B: Creating JDBC Statement 
			statement = connection.createStatement();

			// create SQL query to create new table PLAYER
			String sqlQuery = "CREATE TABLE PLAYER (" +
					" NAME VARCHAR(50) NOT NULL, " + 
					" AGE INT(3) NOT NULL, " + 
					" MATCHES INT(3) NOT NULL, " + 

			// Step 2.C: Executing SQL & retrieve data into ResultSet
			int sqlQueryResult = statement.executeUpdate(sqlQuery);

			// output of database creation
			if(0 == sqlQueryResult) {
				System.out.println("New table created successfully");
			else {
				System.out.println("Error in creating table");
		catch(SQLException sqlex){
		finally {

			// Step 3: Closing database connection
			try {
				if(null != connection) {

					// cleanup resources, once after processing

					// and then finally close connection
			catch (SQLException sqlex) {


New table PLAYER created under PLAYER_INFO database successfully


4. Download:


Read Also:




Happy Coding !!
Happy Learning !!

Java JDBC: Inserting a record using JDBC Statement Interface
Java JDBC: Droping database using JDBC Statement Interface