Based on the sampleConnection example, the method of integrating with Altibasein iBATIS is explained in detail.

Note: IDE uses Eclipse.

Creating DB Tables and Sequences

The following tables and sequences are created in the DB. (Refer to create_tbl.sql file)

CREATE TABLE PERSON(
    PER_ID NUMBER (5, 0) NOT NULL,
    PER_NAME VARCHAR (40) NOT NULL,
    PER_BIRTH_DATE DATE,
    PER_WEIGHT_KG NUMBER (4, 2) NOT NULL,
    PER_HEIGHT_M NUMBER (4, 2) NOT NULL,
    PRIMARY KEY (PER_ID)
);
 
CREATE SEQUENCE PERSON_SEQ;

Creating Project

Create a project called SimpleConnection in Eclipse.

  1. Click Menu-File-Java Project

  2. Project name: Enter SimpleConnection

  3. Click the Finish button

Creating SqlMap File

Create a SqlMap file that defines CRUD SQL statement of the Person table and methods to be mapped (Person.xml).

  1. Right-click on the SimpleConnection project-src directory and click New-File.

  2. Create Person.xml in File name:.

Write the following in the Person.xml file.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN"
"http://www.ibatis.com/dtd/sql-mapconfig-2.dtd">
 
<sqlMap namespace="Person">
    <resultMap id="PersonResult" class="examples.domain.Person">
        <result property="id" column="per_id" />
        <result property="name" column="per_name" />
        <result property="birthDate" column="per_birth_date" />
        <result property="weightInKilograms" column="per_weight_kg" />
        <result property="heightInMeters" column="per_height_m" />
</resultMap>
 
<select id="getPerson" parameterClass="int" resultClass="examples.domain.Person">
        <![CDATA[
            SELECT
            PER_ID as id,
            PER_NAME as name,
            PER_BIRTH_DATE as birthDate,
            PER_WEIGHT_KG as weightInKilograms,
            PER_HEIGHT_M as heightInMeters
            FROM PERSON
            WHERE PER_ID = #value#
        ]]>
</select>
 
<insert id="insertPerson" parameterClass="examples.domain.Person">
        <![CDATA[
            INSERT INTO
            PERSON (PER_ID, PER_NAME, PER_BIRTH_DATE,
PER_WEIGHT_KG, PER_HEIGHT_M)
            VALUES (#id#, #name#, #birthDate#,
#weightInKilograms#, #heightInMeters#)
        ]]>
</insert>
 
<update id="updatePerson" parameterClass="examples.domain.Person">
        <![CDATA[
            UPDATE PERSON
            SET PER_NAME = #name#,
            PER_BIRTH_DATE = #birthDate#,
            PER_WEIGHT_KG = #weightInKilograms#,
            PER_HEIGHT_M = #heightInMeters#
            WHERE PER_ID = #id#
        ]]>
</update>
 
<delete id="deletePerson" parameterClass="int">
        <![CDATA[
            DELETE PERSON
            WHERE PER_ID = #id#
        ]]>
</delete>
 
<select id="getAllPersons"  resultMap="PersonResult">
        <![CDATA[
            SELECT * FROM person
        ]]>
</select>
</sqlMap>

SQL matching the ID defined in <insert>, <update>, <delete>, and <select> tags defined in the above file when calling insert, update, delete, queryForXXX () method of SqlMapClient in the application are automatically executed.

Creating SqlMapConfig File

  1. Create a properties file (db.properties) that defines properties for Altibase connection. (Right-click on SimpleConnection project-src directory and click New-File. Create db.properties in File name :)

 

driver=Altibase.jdbc.driver.AltibaseDriver
url=jdbc:Altibase://192.168.1.35:21129/mydb
username=sys
password=manager

2. Set the dataSource and SqlMap files for interworking with Altibase in the SqlMapConfig file (SqlMapConfigExample.xml). (Right-click on the SimpleConnection project-src directory and click New-File. Create SqlMapConfigExample.xml in File name:)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-config-2.dtd">
 
<sqlMapConfig>
 
<properties resource="db.properties" />
 
<transactionManager type="JDBC" >
    <dataSource type="SIMPLE">
       <property name="JDBC.Driver" value="${driver}"/>
       <property name="JDBC.ConnectionURL" value="${url}"/>
       <property name="JDBC.Username" value="${username}"/>
       <property name="JDBC.Password" value="${password}"/>
    </dataSource>
</transactionManager>
 
<sqlMap resource="Person.xml" />
 
</sqlMapConfig>

Creating Application

  1. Create a Person class (Person.java) that is a DO object for the person table.

  2. Right-click in the src directory of the SimpleConnection project and click New-Class.

  3. Enter examples.domain in Package: and Person in Name:

 

Write the following in the Person.java file.

package examples.domain;
import java.sql.*;
public class Person {
    private int id;
    private String name;
    private Date birthDate;
    private double weightInKilograms;
    private double heightInMeters;
    public int getId () {
        return id;
    }
    public void setId (int id) {
        this.id = id;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
    public void setBirthDate(Date birthDate) {
        this.birthDate = birthDate;
    }
    public Date getBirthDate() {
        return birthDate;
    }
    public void setWeightInKilograms(double weightInKilograms) {
        this.weightInKilograms = weightInKilograms;
    }
    public double getWeightInKilograms() {
        return weightInKilograms;
    }
    public void setHeightInMeters(double heightInMeters) {
        this.heightInMeters = heightInMeters;
    }
    public double getHeightInMeters() {
        return heightInMeters;
    }
    public String toString(){
                return "id="+id+", name="+name+", birthData="+birthDate+",
weightInKillograms="+weightInKilograms+
",heightInMeters="+heightInMeters;
    }
}

3. Write a main program (PersonApp.java) that executes CRUD in the DB.

4. Right-click in the src directory of the SimpleConnection project and click New-Class.

 5. Enter PersonApp in Name :.

 

Write the following in the PersonApp.java file.

import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.util.List;
import examples.domain.Person;
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
 
public class PersonApp {
 
public static void main(String[] args) throws Exception {
    String resource ="SqlMapConfigExample.xml";
    Reader reader = Resources.getResourceAsReader(resource);
    SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
 
    //insert Person
    Person newPerson1 = new Person();
    newPerson1.setName("KIM");
    newPerson1.setBirthDate (new java.sql.Date(1978,1-1,1));
    newPerson1.setHeightInMeters(1.82);
    newPerson1.setWeightInKilograms(80.23);
    sqlMap.insert ("insertPerson", newPerson1);
 
    Person newPerson2 = new Person();
    newPerson2.setName("LEE");
    newPerson2.setBirthDate (new java.sql.Date(1975,5-1,5));
    newPerson2.setHeightInMeters(1.57);
    newPerson2.setWeightInKilograms(45.23);
    sqlMap.insert ("insertPerson", newPerson2);
    System.out.println();
    System.out.println("insert Person");
 
    List<Person> list = (List<Person>)sqlMap.queryForList("getAllPersons");
    System.out.println("Selected "+list.size()+" records.");
    for(int i=0; i< list.size();i++){
        System.out.println(list.get(i));
    }
 
    //update Person
    newPerson1.setHeightInMeters(1.93);
    newPerson1.setWeightInKilograms(86.36);
    sqlMap.update("updatePerson", newPerson1);
    System.out.println();
    System.out.println("update Person");
    list = sqlMap.queryForList("getAllPersons");
    System.out.println("Selected "+list.size()+" records.");
    for(int i=0; i< list.size();i++){
        System.out.println(list.get(i).toString());
    }
    System.out.println();
    System.out.println("get Person");
 
    //get Person
    Integer personPk = new Integer(1);
    Person person = (Person) sqlMap.queryForObject ("getPerson", personPk);
    System.out.println(person);
 
    //delete Person
    sqlMap.delete ("deletePerson", new Integer(1));
    sqlMap.delete ("deletePerson", new Integer(2));
    System.out.println();
    System.out.println("delete Person");
    list = sqlMap.queryForList("getAllPersons");
    System.out.println("Selected "+list.size()+" records.");
    for(int i=0; i< list.size();i++){
        System.out.println(list.get(i));
    }
 
}
}

Adding Related JAR Files

Add the Altibase.jar and ibatis-2.3.4.x.jar files.

Right-click on SimpleConnection project, click Properties-Java Build Path-Click Add External JARS in Libraries to add Altibase.jar and ibatis-2.3.4.x.jar files.

Running Application

Run the SimpleConnection project.

After clicking the SimpleConnection project, run from the menu or click the Run button.