Create Tables

Create Tables

In this tutorial, we are going to discuss about how to create tables using JDBC. Before going into the program, it is advised to go through JDBC Programming Basic Steps where the meaning of Connection, Statement etc. are discussed.

Here’s a simple example of creating a table using JDBC (Java Database Connectivity). In this example, I’ll demonstrate how to connect to a MySQL database and create a table named “employees” with three columns: “id”, “name”, and “salary”.

Make sure you have the JDBC driver for your database installed and added to your Java project’s classpath. If you’re using MySQL, you can download the MySQL JDBC driver from the official MySQL website.

Create Tables

Here’s a basic example:

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

 * @author ashok.mariyala
public class CreateTableExample {

    public static void main(String[] args) {
        String url = "jdbc:mysql://localhost:3306/employee";
        String user = "ashok";
        String password = "Ashok@123";
        try (Connection connection = DriverManager.getConnection(url, user, password)) {
            Statement statement = connection.createStatement();
            // Create table
            String sql = "CREATE TABLE IF NOT EXISTS employees (" +
                            "id INT AUTO_INCREMENT PRIMARY KEY," +
                            "name VARCHAR(100) NOT NULL," +
                            "salary DECIMAL(10, 2)" +
            System.out.println("Table created successfully.");
        } catch (SQLException e) {
            System.out.println("Failed to create table: " + e.getMessage());

Make sure to replace “jdbc:mysql://localhost:3306/employee” with your database URL, “ashok” with your database username, and “Ashok@123” with your database password.

This code will create a table named “employees” with three columns: “id” (auto-incremented integer), “name” (varchar), and “salary” (decimal). It first establishes a connection to the database, then creates a Statement object to execute SQL commands. Finally, it executes the SQL command to create the table.

That’s all about the how to create tables using JDBC. If you have any queries or feedback, please write us email at Enjoy learning, Enjoy Java.!!

Create Tables
Scroll to top