Home Tutorials Ibatis iBatis Update -Updating data of a table
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

iBatis Update -Updating data of a table

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

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

 

 

 

 

 

Contact.java

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;
  
}

SqlMapConfig.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">

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

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".

Contact.xml

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

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

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

sqlMap.update("Contact.updateById",contactId);

Full source code of IbatisUpdate.java is as follows:

IbatisUpdate.java

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(
 
"SqlMapConfig.xml"
 
);
  SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
  //Updating one record of contact
  System.out.println("*---- Updating informations of Contact -----*");
  Contact contct=new Contact();
  long contactId=1;
  sqlMap.update("Contact.updateById",contactId);
  System.out.println("|Updated Record Successfully ");
  System.out.println("All Contacts");
  List<Contact> contacts = (List<Contact>)
  sqlMap.queryForList("Contact.getAll",null);
  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; 
  System.out.println("");
  }
  System.out.println("============================================");
 }  
}

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

Output:

Download Source Code

     

Advertisements

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: September 9, 2008

Ask Questions?    Discuss: iBatis Update -Updating data of a table  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com