Home Tutorials Ibatis iBatis Deletion Tutorial
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

iBatis Deletion Tutorial

Advertisement
I hope you have completely understood how to insert and show data from database in the previous examples. So in this example you will learn how to delete the data from database using iBatis.

iBatis Deletion Tutorial

     

I hope you have completely understood how to insert and show data from database in the previous examples. So in this example you will learn how to delete the data from database using iBatis. For that you have to analysis the code to understand clearly what is happening in this code. But you absolutely need not to create different database, as you already know that we are using previous MySQL as a database table and our table name is ?Contact?. But you have a choice you can use either this database or create your own it?s completely up to you! The only thing is that make sure you defined table name correctly otherwise bug will occur. In case you are following this iBatis tutorial from the starting then you need not to rewrite the code. Just copy the given code into folder and execute it finally to delete the data from the database table. 

As I earlier mentioned.. In this section of iBatis tutorial we are going to delete records from Contact table, we are using MySQL database "vin"

Our Contact.java and SqlMapConfig.xml files are same as in previous examples.

 

 

 

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>

In Contact.xml file we are using <delete> tag to delete all records from Contact table.

<delete id="deleteAll" parameterClass="Contact">
   delete from Contact
</delete>

Above lines of code deletes all records from Contact table. Here the defined id "deleteAll" will be used further in IbatisDeletion class to execute query on the database.

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">
<!--- Delete data from Contact table --> 
<delete id="deleteAll" parameterClass="Contact">
   delete from Contact
</delete>

<!--- Showing all data of table -->
<select id="getAll" resultClass="Contact">
   select * from contact
</select>
</sqlMap>

We have imported following packages :

  • com.ibatis.common.resources
  • com.ibatis.sqlmap.client

for using classes and interfaces of SQL mapping .

Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
  SqlMapClient sqlMap = 
  SqlMapClientBuilder.buildSqlMapClient(reader);

Above lines of code reads configuration from "SqlMapConfig.xml". Full source code of IbatisDeletion.java is as follows:

IbatisDeletion.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 IbatisDeletion{
  public static void main(String[] args)
    
throws IOException,SQLException{
  Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
  SqlMapClient sqlMap = 
  SqlMapClientBuilder.buildSqlMapClient
(reader);
 //Deleting all records from contacts
  System.out.println("*-- Deleting informations from Contact------*");
  Contact contct=new Contact();
  sqlMap.delete("Contact.deleteAll",contct);
  System.out.println("|Deleted 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 follow these steps :

  • Create and save Contact.xml and SqlMapConfig.xml
  • create and save Contact.java and compile Contact.java
  • create IbatisDeletion.java and compile it
  • execute IbatisDeletion and you will get following output on your command prompt

Output:

Download Source Code

     

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



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

Related Tutorials

Ask Questions?    Discuss: iBatis Deletion Tutorial  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
glenn
September 27, 2011
deletion in iBatis

can we do a truncate table in iBatis? thanks!
DMCA.com