Dashboard > iBATIS DataMapper > Home > 3rd Party Contributions > Convert ResultSet to JSTL Result
Convert ResultSet to JSTL Result
Added by Chadwick, last edited by Chadwick on Jun 21, 2009
Labels: 
(None)


Note

I'm updating this just to fix some errors in the Wiki page that have cropped-up in the 14+ months since I originally posted this. I haven't been following iBATIS lately (not by choice) so I'm not sure if this is still correct or useful.

This is the version I've currently got in my personal repo. It makes use of two classes that I'm not including: com.dotech.servlet.jstl.ResultSupport and com.dotech.sql.SqlHelper. For ResultSupport, simply replace it with javax.servlet.jsp.jstl.sql.ResultSupport. It should be pretty obvious that SqlHelper is just a utility class to help with JDBC stuff, so just replace it with whatever you would normally use.

This was posted to gmane.comp.java.ibatisdb.user (although the attachment didn't seem to make it into the thread), but I thought it might be convenient to dump it here as well: ResultSetToResultHandler.java

Here's a simple config (it's also an example of using an Oracle cursor as an OUT parameter for a stored procedure):

<sqlMapConfig>
    
    <typeAlias alias="result"
    type="javax.servlet.jsp.jstl.sql.Result"/>
    
    <typeAlias alias="cursorHandler"
    type="com.dotech.ibatis.ResultSetToResultHandler"/>
    
    <typeHandler javaType="result"
    jdbcType="ORACLECURSOR"
    callback="cursorHandler"/>
    </sqlMapConfig>
<sqlMap>
    
    <parameterMap id="noInputsSingleResult" class="map">
    <parameter property="resultCode"
    javaType="int"
    jdbcType="NUMERIC"
    nullValue="-1"
    mode="OUT"/>
    <parameter property="result"
    javaType="result"
    jdbcType="ORACLECURSOR"
    mode="OUT"/>
    </parameterMap>
    
    <procedure id="getFunds" parameterMap="noInputsSingleResult">
    {?= call get_funds(?)}
    </procedure>
    
    </sqlMap>
SqlMapExecutor exec = ...;
    Map params = new HashMap(2);
    exec.queryForObject("getFunds", params);
    Integer resultCode = (Integer)params.get("resultCode");
    if (resultCode == null) {
    // error
    } else {
    int resultCodeValue = resultCode.intValue();
    if (resultCodeValue == 0) {
    Result result = (Result)params.get("result");
    // process
        } else {
    // error
        }
    }

Although this solution relies on JSTL classes, it is not limited to Servlet/JSP or J2EE applications, nor does it require the use of other JSTL features. The javax.servlet.jsp.jstl.sql.Result interface (API) and the javax.servlet.jsp.jstl.sql.ResultSupport class (API) only depend on J2SE. In other words, this solution will work for pretty much any Java application.

An implementation of JSTL is provided by Apache's Jakarta Standard Taglib Project. Please note that if you do want to use the other features of JSTL in a web application, the provided link is for a version of the Standard taglib that is compatible with JSP 2.0. A version that is compatible with JSP 1.2 can be found here. No matter which version you choose, the classes will be located in the jstl.jar file that is included in the distribution.


Site running on a free Atlassian Confluence Open Source Project License granted to OSS. Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.5.5 Build:#811 Jul 25, 2007) - Bug/feature request - Contact Administrators