fbpx
Pages Navigation Menu

Coding is much easier than you think

JDBC PreparedStatement example to Insert a Record

SONY DSC
 
Here is an example to show you how to insert a record into table via JDBC PreparedStatement.
 
To issue an insert statement, calls the PreparedStatement.executeUpdate() method
 
** UPDATE: Complete JDBC tutorial now available here.
 

String insertTableQuery= "INSERT INTO EMPLOYEE"
  + "(USER_ID, USERNAME, CREATED_BY) VALUES (?,?,?)";
PreparedStatement preparedStatement = connection.prepareStatement(insertTableQuery);
preparedStatement.setInt(1, 1000);
preparedStatement.setString(2, "nilafar");
preparedStatement.setString(3, "admin");
// execute insert SQL statement
preparedStatement .executeUpdate();

 
Full example…
 

package com.simplecode.jdbc;

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

public class JDBCPreparedStmtInsertExample 
{

private static final String dbDriver = "oracle.jdbc.driver.OracleDriver";
private static String serverName = "127.0.0.1";
private static String portNumber = "1521";
private static String sid = "XE";
private static final String dbUrl = "jdbc:oracle:thin:@" + serverName + ":"
			+ portNumber + ":" + sid;
private static final String dbUser = "system";
private static final String dbPassword = "admin";

public static void main(String[] argc) 
{
	try 
	{
		insertRecordIntoTable();
	}
	catch (SQLException e) 
	{
		System.err.println(e.getMessage());
	}
}

private static void insertRecordIntoTable() throws SQLException 
{
	Connection connection = null;
	PreparedStatement prepareStmt = null;

	String insertTableQuery = "INSERT INTO EMPLOYEE"
			+ "(USER_ID, USERNAME, CREATED_BY) VALUES (?,?,?)";

	try 
	{
		connection = getDBConnection();
		prepareStmt = connection.prepareStatement(insertTableQuery);

		prepareStmt.setInt(1, 1000);
		prepareStmt.setString(2, "Nilafar");
		prepareStmt.setString(3, "admin");
 
		// execute insert SQL statement
		prepareStmt.executeUpdate();
		System.out.println("Record is inserted into EMPLOYEE table!");
	}
	catch (SQLException e) 
	{
		System.err.println(e.getMessage());
	} 
		
	finally 
	{
		if (prepareStmt != null) 
		{
			prepareStmt.close();
		}
		if (connection != null) 
		{
			connection.close();
		}
		}
	}

	private static Connection getDBConnection() 
	{
		Connection dbConnection = null;
		try 
		{
			Class.forName(dbDriver);
		}
		
		catch (ClassNotFoundException e) 
		{
			System.err.println(e.getMessage());
		}

		try 
		{
		dbConnection = DriverManager.getConnection(dbUrl, dbUser, dbPassword);
		return dbConnection;
		}
		catch (SQLException e) 
		{
			System.err.println("Db "+e.getMessage());
	        }
	return dbConnection;
}
}

 

Note:   Once a PreparedStatement is prepared, it can be reused after execution. You reuse a PreparedStatement by setting new values for the parameters and then execute it again.

Here is a simple example.
 

String insertTableQuery= "INSERT INTO EMPLOYEE"
  + "(USER_ID, USERNAME, CREATED_BY) VALUES (?,?,?)";
PreparedStatement preparedStatement = connection.prepareStatement(insertTableQuery);
preparedStatement.setInt(1, 1000);
preparedStatement.setString(2, "nilafar");
preparedStatement.setString(3, "admin");
// execute insert SQL statement
preparedStatement .executeUpdate();

// Reuse the prepared statement
preparedStatement.setInt(1, 1001);
preparedStatement.setString(2, "Jamil");
preparedStatement.setString(3, "admin");
preparedStatement .executeUpdate();

 

About Mohaideen Jamil


I'm a Full stack developer of IT exp in J2EE, AngularJs, MicroServices, Docker, Spring (Boot, MVC, Cloud), Bluemix, DevOps. Follow me on Facebook or Google Plus. If you like my tutorials, consider making a donation to this charity, thanks.

Advertisements
%d bloggers like this: