Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training

Inserting Records using the Prepared Statement

In this section we are going to learn how we will insert the records in the database table by using the PreparedStatement interface of java.sql package.

Ads

Tutorials   
Change Column Name in MySQL Java Mysql Connection Example JDBC Example with MySQL Jdbc Mysql Connection String Jdbc Mysql Connection Url Getting Column Names from a database table in Java Count Rows from a Database Table JDBC Example with MySQL Write Text File to Table Inserting Image in Database Table Insert Data in Table Using Stored Procedure Copy One Database Table to Another Copy Table in a MySQL Database Set Timestamp by using the Prepared Statement Set Time by using the Prepared Statement Set Date by using the Prepared Statement Prepared Statement Set Big Decimal Set byte, short and long data types by using the Prepared Statement Set Data Types by using Prepared Statement Using the Prepared Statement Twice Deleting Records using the Prepared Statement Count Records using the Prepared Statement Inserting Records using the Prepared Statement Update Records using Prepared Statement Select Records Using Prepared Statement Prepared Statement With Batch Update Statement Batch Update Prepared Statement Set Object Cross Join Tables in a Specific Database Join tables with the NATURAL RIGHT JOIN operation Join tables with the NATURAL LEFT JOIN operation Join tables in the specific database Delete a Column from a Database Table Delete a Specific Row from a Database Table Deleting All Rows from a Database Table Sum of Column in a Database Table Arrange a Column of Database Table Arrange a Column of Database Table Remove Unique Column in Database Table Make Unique Column in Database Table Change Column Name of a Table Adding a New Column Name in Database Table Retrieving All Rows from a Database Table Inserting values in MySQL database table Retrieving Tables from a Database Deleting a Table from Database Description of Database Table Creating a MySQL Database Table to store Java Types Creating a Database Table Creating a Database in MySQL
Ads

Inserting Records using the Prepared Statement

     

In this section we are going to learn how we will insert the records in the database table by using the PreparedStatement interface of java.sql package.  As we all know that in database all records are added in the table in a tabular format like: rows and columns. See brief description below:

Description of program:

This program will perform the work for inserting the records in 'movies' database table. For this, first of all we need to establish the connection with MySQL database by using the JDBC driver. When the connection has been established then we will pass a SQL statement in the prepareStatement that returns the PreparedStatement object.  After setting the values of movie name and release year the values are inserted in the movies table by the help of executeUpdate method then it will show a message "row(s) affected" otherwise display "SQL statement is not executed!" . The executeUpdate method also executes the SQL statement (INSERT, UPDATE, DELETE) at the time of  inserting, updating and deleting the records from the database table.

Here is the code of program:

import java.io.*;
import java.sql.*;

public class InsertRecords{
  public static void main(String[] args) {
  System.out.println("Insert records example using prepared statement!");
  Connection con = null;
  try{
  Class.forName("com.mysql.jdbc.Driver");
  con = DriverManager.getConnection("jdbc:mysql:
//localhost:3306/jdbctutorial","root","root");
  try{
  String sql = "INSERT movies VALUES(?,?)";
  PreparedStatement prest = con.prepareStatement(sql);
  BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
  System.out.println("Enter movie name:");
  String mov = bf.readLine();
  prest.setString(1, mov);
  System.out.println("Enter releases year of movie:");
  int year = Integer.parseInt(bf.readLine());
  prest.setInt(2, year);
  int count = prest.executeUpdate();
  System.out.println(count + "row(s) affected");
  con.close();
  }
  catch (SQLException s){
  System.out.println("SQL statement is not executed!");
  }
  }
  catch (Exception e){
  e.printStackTrace();
  }
  }
} 

Download this example.

Database Table: movies

title year_made
alam ara 1945
Bagal bali 2002
Diwar 1980
Nadia ke par 1975
Karan 1996
Ramayan 1966
Yarana 1988
Muhabte 1998

Output of program:

C:\vinod\jdbc\jdbc\PreparedStatement>javac InsertRecords.java

C:\vinod\jdbc\jdbc\PreparedStatement>java InsertRecords
Insert records example using prepared statement!
Enter movie name:
Bagban
Enter releases year of movie:
2003
1row(s) affected

After executing the program: 

Database table: movies table

title year_made
alam ara 1945
Bagal bali 2002
Diwar 1980
Nadia ke par 1975
Karan 1996
Ramayan 1966
Yarana 1988
Muhabte 1998
Bagban 2003

Advertisements

Online Training Classes

Share on Google+Share on Google+

Inserting Records using the Prepared Statement

Posted on: April 13, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: Inserting Records using the Prepared Statement  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:3
sivakrishna
January 11, 2012
hii

hi,nice wrk had u done.its try in forloop to better undestand .
guest
May 22, 2012
thanks

Thanks you
rengaraj
September 9, 2012
error

Sir/Mam, I suppose there is an error in this program.Plz rectify it. R.Rengaraj

Ads

 

Ads