developer.com
Search EarthWeb
CodeGuru | Gamelan | Jars | Wireless | Discussions
Navigate developer.com
Architecture & Design  
Database  
Java
Languages & Tools
Microsoft & .NET
Open Source  
Project Management  
Security  
Techniques  
Voice  
Web Services  
Wireless/Mobile
XML  
Technology Jobs  

   Developer.com Webcasts:
  The Impact of Coding Standards and Code Reviews

  Project Management for the Developer

  Defining Your Own Software Development Methodology

  more Webcasts...




See the Winners!


Developer Jobs

Be a Commerce Partner
Web Design
KVM over IP
Promos and Premiums
Server Racks
Car Donations
Online Shopping
Promotional Golf
Compare Prices
PDA Phones & Cases
Web Hosting Directory
Imprinted Promotions
Build a Server Rack
Find Software
Online Education

 


Developer News -
SaaS Tool Offers Custom Database Development    May 9, 2008
Microsoft’s Automated Agent: Can We Talk?    May 7, 2008
Borland Finally Sells CodeGear    May 7, 2008
Red Hat Heads For The JON 2.0    May 7, 2008
Free Tech Newsletter -

Best Practices for Developing a Web Site: Checklists, Tips, Strategies & More. Download Exclusive eBook Now.

Using iBatis SQL Maps for Java Data Access
By Michael Klaene

Go to page: 1  2  3  Next  

Finding the best approach when accessing a database from Java can be a daunting task. The most common solution is to program directly to the JDBC (Java Database Connectivity) APIs. The result is hard-to-read source files, bloated with complex code that has nothing to do with business logic. To make matters worse, JDBC does nothing to address the natural differences that usually exist between a system's object model and its relational data model. This necessitates still more code to glue these disparate views together. There is clearly a need for tools to assist us in this area.

The iBATIS Database Layer, written by Clinton Begin, is a collection of open-source components that assist Java developers with data access. The most important of these components is the SQL Maps framework. SQL Maps provides an efficient way to graph database values to Java objects through the use of XML configuration files. This article will use the latest production release of SQL Maps, 1.3.1, to demonstrate how SQL Maps work using a series of examples. It will then conclude with a discussion of the benefits of using iBatis SQL Maps.

A First Look

To view SQL Maps in action, I will use a simple demo application that allows employees to make conference room reservations. It will not be possible to cover SQL Maps in its entirety, but hopefully there will be enough here to entice you to visit the iBATIS site and explore the project in greater detail.

There are two tables to consider in our application, conference_rooms and room_reservations. The conference_rooms table identifies conference_rooms by id and name. The field number_of_seats can be used to guage the size of a room. The room_reservations table, linked to conference_rooms by room_id, stores reservation times and the team ('development team', 'accounting team') making the reservation. For simplicity's sake, there is a one-to-one mapping between these tables, conference_rooms and room_reservations, and the JavaBeans ConferenceRoom and RoomReservation.

Given this setup, let's first consider a code snippet that you might use if populating a ConferenceRoom bean with JDBC:

String roomToUse        = "Conf Room North";
ConferenceRoom confRoom = new ConferenceRoom();

try{
  Connection conn = dSrc.getConnection();
  PreparedStatement pstmt =
    conn.prepareStatement("SELECT room_id,room_name,number_of_seats,
                           room_active " +
     "FROM conference_rooms WHERE room_name = ?";
  pstmt.setString(1,roomToUse);
  rstst = ps.executeQuery();

  rstst.next();
  confRoom.roomId        = rstst.getInt(1);
  confRoom.roomName      = rstst.getString(2);
  confRoom.numberOfSeats = rstst.getInt(3);
  confRoom.roomActive    = rstst.getBoolean(4);

  rstst.close();
  pstmt.close();
}
catch(SQLException e) {
   throw newRuntimeException(e);
}
finally {
  conn.close();
}

As simple as the code's objective may be, it is complicated by JDBC 'plumbing.' Let's try this once again, the SQL Maps way:

String roomToUse = "Conf Room North";

ConferenceRoom confRoom = (ConferenceRoom) 
   sqlMap.executeQueryForObject("getConferenceRoomByName", roomToUse);

The SqlMap method, getQueryForObject, accepts two String parameters. The first parameter identifies the name of the mapped SQL statement we use to access the database. The second specifies a room name used in the query. The bean is instantiated, populated, and then returned, leaving us to focus our coding efforts on application logic.

Now, I want to step backwards through this example and examine what makes such clean code possible. To do this, we need to look at two additional items. The first is the SQL Map file. This file is an XML descriptor that contains our mapped SQL statements. At runtime, these statements are read into an application. When "getConferenceRoomByName" is referenced in Java, a PreparedStatement object executes the query and a ResultSet populates the bean. The demo application uses a single SQL Map file called Conferences.xml. An application may use several such SQL Map files.

<!Conferences.xml SQL Map file-->

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sql-map PUBLIC "-//iBATIS.com//DTD SQL Map 1.0//EN"
   "http://www.ibatis.com/dtd/sql-map.dtd">

<sql-map name="Conferences">
  <mapped-statement name="getConferenceRoomByName"
                    result-class="entities.ConferenceRoom">
    SELECT room_id         as roomId,
           room_name       as roomName, 
           number_of_seats as numberOfSeats,
           room_active     as roomActive
      FROM conference_rooms
      WHERE room_name = #value#
  </mapped-statement>
</sql-map>

The bulk of the "getConferenceRoomByName" mapped statement consists of SQL. The result-class attribute specifies the fully qualified Java object returned from a mapped statement. The #value# keyword denotes that a statement's parameter is either a String or a Java primitive, such as int.

The final piece of the puzzle is the SQL Map Configuration file:

<!SqlMapConfig.xml SQL Map configuration file-->

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sql-map-config PUBLIC "-//iBATIS.com
                                  //DTD SQL Map Config 1.0//EN"
   "http://www.ibatis.com/dtd/sql-map-config.dtd">
   
<sql-map-config>

  <datasource name = "hsql" 
    factory-class="com.ibatis.db.sqlmap.datasource.SimpleDataSourceFactory"
            default="true">
    <property name="JDBC.Driver" value="org.hsqldb.jdbcDriver"/>
    <property name="JDBC.ConnectionURL"
              value="jdbc:hsqldb:hsql://localhost/conferences"/>
    <property name="JDBC.Username" value="sa"/>
    <property name="JDBC.Password" value=""/> 
  </datasource>

  <sql-map resource="maps/Conferences.xml"/>
 
</sql-map-config>

The purpose of this file is two-fold. First, it configures a JDBC DataSource. In this case, it uses iBATIS' own DataSource implementation, SimpleDataSource. Secondly, the file declares each SQL Map file that an application uses. Our examples use the hsqldb database and a single Map file, Conferences.xml.

Upon application startup, we search the classpath for this configuration file and process it.

String resource = "maps/SqlMapConfig.xml";
Reader reader   = Resources.getResourceAsReader(resource);
SqlMap sqlMap   = XmlSqlMapBuilder.buildSqlMap(reader);

The configuration needs to happen but one time, prior to the calling of any mapped statements. The sqlMap instance could be a Singleton, or possibly a ServletContext object in a web application, that we retrieve when we need to call a mapped statement. The Resource class we are using is provided by iBATIS to read files from the classpath.

Go to page: 1  2  3  Next  


Tools:
Add www.developer.com to your favorites
Add www.developer.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed


Languages & Tools Archives

Work With InterSystems. Not Separate Systems. Rapidly develop and deploy connectable applications.
Flash Demo: Learn how IBM Information Server Blade is easy to manage, highly scalable and efficient.
Learn about expanding business opportunities for the reseller channel. Visit IT Channel Planet.
Five Trends for Application Development. Download Your Complimentary Report. Exclusive. Act Now.
Whitepaper: XML Processing in Applications--Take the Next Step



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: HyperV-The Killer Feature in WinServer ‘08
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Win Server ‘08
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES