Inserting records into a MySQL table using PreparedStatement in Java?


To insert a record in the table using PreparedStatement in Java, you need to use below syntax to insert records. The syntax is as follows −

String anyVariableName= "INSERT INTO yourTableName(yourColumnName1, yourColumnName2, yourColumnName3,.........N)" +
"VALUES (?, ?, ?,..............N)";

Now use the PreparedStatement object to set the value for all columns. The syntax is as follows −

PreparedstatementObject = con.prepareStatement(query);
PreparedstatementObject .setXXX(1, yourValue);
PreparedstatementObject .setXXX(2, yourValue);
PreparedstatementObject .setXXX(3, yourValue);
.
.
.
N

The above prepared statement will solve your problem. Now first create a table in MySQL. The query to create a table is as follows −

mysql> create table CourseDemo
-> (
-> CourseId int,
-> StudentName varchar(20),
-> CourseName varchar(30)
-> );
Query OK, 0 rows affected (1.86 sec)

The above table is in a sample database. Now here is the Java code to insert a record in the table with the help of PreparedStatement −

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.util.Scanner;
public class InsertRecordInPreparedStatement {
   static int cid;
   static String studentName;
   static String courseName;
   public static void main(String[] args) {
      String JdbcURL = "jdbc:mysql://localhost:3306/sample?useSSL=false";
      String Username = "root";
      String password = "123456";
      Connection con = null;
      Scanner keyboardInput=new Scanner(System.in);
      PreparedStatement pstmt = null;
      String query = "INSERT INTO CourseDemo(CourseId, StudentName, CourseName)" + "VALUES (?, ?, ?)";
      try {
         con = DriverManager.getConnection(JdbcURL, Username, password);
         input(keyboardInput);
         pstmt = con.prepareStatement(query);
         pstmt.setInt(1, cid);
         pstmt.setString(2, studentName);
         pstmt.setString(3, courseName);
         int status = pstmt.executeUpdate();
         if(status > 0) {
            System.out.println("Record is inserted successfully !!!");
         }
      } catch(Exception e){
         e.printStackTrace();
      }
   }
   public static void input(Scanner keyboardInput) {
      System.out.println("Enter the course id:");
      cid = keyboardInput.nextInt();
      System.out.println("Enter the Student Name:");
      studentName = keyboardInput.next();
      System.out.println("Enter the Course Name:");
      courseName = keyboardInput.next();
   }
}

The snapshot of Java code is as follows −

The following is the output −

Enter the course id:
101
Enter the Student Name:
John
Enter the Course Name:
Java
Record is inserted successfully !!!

Here is the snapshot of sample output −

Now check the record is inserted in the table or not, therefore just open MySQL. Use the database sample and the following query to display a record from the table using a select statement −

mysql> select *from CourseDemo;

The following is the output −

+----------+-------------+------------+
| CourseId | StudentName | CourseName |
+----------+-------------+------------+
| 101      | John        | Java       |
+----------+-------------+------------+
1 row in set (0.00 sec)

Here is the snapshot of table output −

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 30-Jun-2020

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements