How to Insert Record using JDBC Statement in Java

Insert Record using JDBC Statement

In this tutorial, We will show you How to Insert Record using JDBC Statement in Java.

Table Information

1. Table Query

CREATE TABLE customer (customerId INT, customerName VARCHAR(30), customerAge INT, customerAddress VARCHAR(60), PRIMARY KEY(customerId));

2. Table Structure

Insert Record using JDBC Statement

Project Structure

Insert Record using JDBC Statement

RecordInsert.java

package com.dineshkrish.jdbc;

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

/**
 * 
 * @author Dinesh Krishnan
 *
 */

public class RecordInsert {

	// Method to Insert records to Database
	public static boolean doInsert(int customerId, String customerName,
			int customerAge, String customerAddress) {

		Connection connection = null;
		
		try {
			
			// Loading the Driver
			Class.forName("com.mysql.jdbc.Driver");
			
			// Getting the Database Connection
			connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/dineshkrish", "root", "");

			// Validation
			boolean flag = doValidate(customerId, customerName, customerAge, customerAddress);
			
			if(!flag) {
				
				return false;
			}
			
			// Insert Query
			final String INSERT_QUERY = "insert into customer (customerId, customerName, customerAge, customerAddress) values("
					+ customerId
					+ ", '"
					+ customerName
					+ "', "
					+ customerAge
					+ ", '" + customerAddress + "')";
			
			Statement statement = connection.createStatement();
			
			// Executing Query to perform Insert Operation
			statement.execute(INSERT_QUERY);
			
			return true;
			
		} catch (SQLException e) {
			
			System.out.println(e.getMessage());
			e.printStackTrace();
			
		} catch (ClassNotFoundException e) {
			
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
		
		
		return false;
	}
	
	// Method for Input Validation
	public static boolean doValidate(int customerId, String customerName,
			int customerAge, String customerAddress) {
		
		if(customerId <= 0) {
			
			return false;
		}
		
		if(customerName == null || customerName.isEmpty()) {
			
			return false;
		}
		
		if(customerAge <= 0) {
			
			return false;
		}
		
		if(customerAddress == null || customerAddress.isEmpty()) {
			
			return false;
		}
		
		return true;
	}

	public static void main(String[] args) {

		
		// Calling Method to perform Insertion
		boolean status = doInsert(101, "Dinesh Krishnan", 26, "India");
		
		// Printing the Result based on the Status
		if(status) {
			
			System.out.println("Record Inserted Successfully..");
		} else {
			
			System.out.println("Application Error Occurred..");
		}
		
	}

}

Output

Record Inserted Successfully..

How to Insert Record using JDBC Statement in Java

Download Source Code

Download Here

References

1. Java SQL Documentation
2. Java SQL Connection API
3. Java Statement API

Hello everyone, I am a Founder of dineshkrish.com.