iBatis Update -Updating data of a table

Add, Update and Delete are very common and essential feature for any database application.

iBatis Update -Updating data of a table

iBatis Update -Updating data of a table


Add, Update and Delete are very common and essential feature for any database application. In this iBatis tutorial we have already explained about Insert and Delete in Java using iBatis, now this section will introduce you how you can update data in data table with the iBatis. In iBatis executing an update statement is very simple. For updating you have to add SQL "Update" statement in SQL mapping file "Contact.xml". Contact.java and SqlMapConfig.xml is same as in our previous examples.

iBatis update statement Example







public class Contact {
  private String firstName; 
  private String lastName; 
  private String email;  
  private int id;

  public Contact() {}
  public Contact(
  String firstName,
    String lastName,
    String email) {

  this.firstName = firstName;
  this.lastName = lastName;
  this.email = email;
  public String getEmail() {
  return email;
  public void setEmail(String email) {
  this.email = email;
  public String getFirstName() {
  return firstName;
  public void setFirstName(String firstName) {
  this.firstName = firstName;
  public int getId() {
  return id;
  public void setId(int id) {
  this.id = id;
  public String getLastName() {
  return lastName;
  public void setLastName(String lastName) {
  this.lastName = lastName;


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"

   <settings useStatementNamespaces="true"/>
   <transactionManager type="JDBC">
  <dataSource type="SIMPLE">
  <property name="JDBC.Driver" value="com.mysql.jdbc.Driver"/>
  <property name="JDBC.ConnectionURL"
  <property name="JDBC.Username" value="root"/>
  <property name="JDBC.Password" value="root"/>
   <sqlMap resource="Contact.xml"/> 

iBatis Update Query

Here in our example we are updating table with an id as specified in our parameter and therefore for "id" we have assigned "parameterClass" property value to "long".


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap 
PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"

<sqlMap namespace="Contact">
 <!--- Showing all data of table -->
   <select id="getAll" resultClass="Contact">
   select * from contact
<!--- Update data of Contact table --> 
 <update id="updateById" parameterClass="long">
   update Contact 
  lastName = 'Raghuwanshi'

Now we can execute update command from our java program by the following code as:


Full source code of IbatisUpdate.java is as follows:


import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import java.io.*;
import java.sql.SQLException;
import java.util.*;

public class IbatisUpdate{
  public static void main(String[] args)
throws IOException,SQLException{
  Reader reader = Resources.getResourceAsReader(
  SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
  //Updating one record of contact
  System.out.println("*---- Updating informations of Contact -----*");
  Contact contct=new Contact();
  long contactId=1;
  System.out.println("|Updated Record Successfully ");
  System.out.println("All Contacts");
  List<Contact> contacts = (List<Contact>)
  Contact contact = null;
  for (Contact c : contacts) {
  System.out.print("  " + c.getId());
  System.out.print("  " + c.getFirstName());
  System.out.print("  " + c.getLastName());
  System.out.print("  " + c.getEmail());
  contact = c; 

To run this example program of Update statement follow these steps:

  1. Create and Save Contact.java and SqlMapConfig.xml
  2. Compile Contact.java
  3. make Contact.xml
  4. create and save IbatisUpdate.java and compile it
  5. On executing IbatisUpdate class file you will get this output


Download Source Code