Insert a record with PreparedStatement using JDBC in Java?

In this article, we are going to learn how to insert record with the help of PreparedStatement class using JDBC through java program? By Manu Jemini Last updated : March 23, 2024

Prerequisite

  1. How to create a table using JDBC in Java?
  2. How to insert records through JDBC in Java?
  3. How to display all records using JDBC in Java?
  4. How to display particular record by a field using JDBC in Java?
  5. How to delete a particular record using JDBC in Java?
  6. How to edit a record using JDBC in Java?

Insert a record with PreparedStatement using JDBC

Create an object of Connection class and connect to the database.

Then, we need to take inputs of all the fields of the MYSQL table. After that we create an object of PreparedStatement class and prepare a MySQL query with passing parameter (?). After this, we set the values of passing parameters.

Then, we execute our query by using executeUpdate() method, which is a method of Statement class.

Database details

  • Hostname: localhost
  • Port number: 3306
  • Username: root
  • Password: 123
  • Database name: demo
  • Table name: employees
  • Field: empid (employee id)

Java program to insert a record with the help of PreparedStatement class using JDBC

import java.io.DataInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;

public class ExPrepareStatement {
  public static void main(String[] args) {
    try {
      Class.forName("com.mysql.jdbc.Driver").newInstance();

      //serverhost = localhost, port=3306, username=root, password=123
      Connection cn = DriverManager.getConnection("jdbc:mysql://localhost:3306/demo", "root", "123");

      DataInputStream KB = new DataInputStream(System.in);

      //input employee id
      System.out.print("Enter Employee ID: ");
      String eid = KB.readLine();
      //input employee name
      System.out.print("Enter Employee Name: ");
      String en = KB.readLine();
      //input employee Date Of Birth
      System.out.print("Enter Employee Date Of Birth: ");
      String ed = KB.readLine();
      //input employee city
      System.out.print("Enter Employee City: ");
      String ec = KB.readLine();
      //input employee Salary
      System.out.print("Enter Employee Salary: ");
      String es = KB.readLine();

      //creating object of PreparedStatement class and passing parameter (?)
      PreparedStatement smt = cn.prepareStatement("insert into employees values(?,?,?,?,?)");

      // set the values
      smt.setString(1, eid);
      smt.setString(2, en);
      smt.setString(3, ed);
      smt.setString(4, ec);
      smt.setInt(5, Integer.parseInt(es));

      //to execute update
      smt.executeUpdate();
      System.out.println("Record Submitted....");

      //close the file
      cn.close();
    } catch (Exception e) {
      System.out.println(e);
    }
  }
}

Output (In console)

Enter Employee ID: 200
Enter Employee Name: Akash
Enter Employee Date Of Birth: 04/04/1990
Enter Employee City: Mumbai
Enter Employee Salary: 40000
Record Submitted....

Output (In database)

insert record using PreparedStatement in java using JDBC

Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.