net.kamhon.ieagle.jdbc.oracle.OracleUtil.java Source code

Java tutorial

Introduction

Here is the source code for net.kamhon.ieagle.jdbc.oracle.OracleUtil.java

Source

/*
 * Copyright 2012 Eng Kam Hon (kamhon@gmail.com)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.kamhon.ieagle.jdbc.oracle;

import java.sql.Timestamp;
import java.util.Date;

import oracle.sql.TIMESTAMP;

import org.springframework.jdbc.support.rowset.SqlRowSet;

public class OracleUtil {
    /**
     * 
     * @param rowSet
     * @param fieldName
     * @return
     */
    public static Date getDate(SqlRowSet rowSet, String fieldName) {
        Date date = null;

        Object object = rowSet.getObject(fieldName);
        if (object instanceof TIMESTAMP) {
            TIMESTAMP timestamp = (TIMESTAMP) object;
            try {
                date = new Date(timestamp.dateValue().getTime());
            } catch (Exception ex) {
            }
        } else if (object instanceof Timestamp) {
            Timestamp timestamp = (Timestamp) object;
            date = new Date(timestamp.getTime());
        } else if (object instanceof java.sql.Date) {
            java.sql.Date sqlDate = (java.sql.Date) object;
            date = new Date(sqlDate.getTime());
        }

        return date;
    }

    /**
     * <p>
     * convert sql to paginable sql
     * 
     * For example, need to item from 51 to 80 offset is 50 size is 30
     * 
     * refer to <a href= "http://www.oracle.com/technology/oramag/oracle/06-sep/o56asktom.html" >Oracle Article</a> for
     * full description
     * </p>
     * 
     * @param sql
     * @param offset
     * @param size
     * @return
     */
    public static String convertToPaginationSql(String sql, int offset, int size) {
        String convertedSql = "select * from (select a.*, rownum rnum " + " from (";
        convertedSql = convertedSql + sql + ") a where rownum <= " + (offset + size);
        convertedSql += ") where rnum> " + offset;

        return convertedSql;
    }
}