Java SQL ResultSet Read getHighPrecisionString(ResultSet rs, int ix, int sql_type)

Here you can find the source of getHighPrecisionString(ResultSet rs, int ix, int sql_type)

Description

get High Precision String

License

Apache License

Declaration

public static String getHighPrecisionString(ResultSet rs, int ix, int sql_type) throws Exception 

Method Source Code

//package com.java2s;
/*//w w  w  .  ja  v a 2 s . c om
 * #!
 * Ontopia DB2TM
 * #-
 * Copyright (C) 2001 - 2013 The Ontopia Project
 * #-
 * 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.
 * !#
 */

import java.sql.Date;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.sql.Types;
import java.text.DateFormat;

import java.text.SimpleDateFormat;

public class Main {
    static DateFormat df_date = new SimpleDateFormat("yyyy-MM-dd");
    static DateFormat df_datetime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static String getHighPrecisionString(ResultSet rs, int ix, int sql_type) throws Exception {
        // HACK: to make DATE type include timestamp information if available
        switch (sql_type) {
        case Types.DATE:
            return getString(rs, ix, Types.TIMESTAMP);
        default:
            return getString(rs, ix, sql_type);
        }
    }

    public static String getString(ResultSet rs, int ix, int sql_type) throws SQLException {
        switch (sql_type) {
        case Types.DATE:
            Date date = rs.getDate(ix);
            if (date == null)
                return null;
            return df_date.format(date);
        case Types.TIMESTAMP:
            Timestamp timestamp = rs.getTimestamp(ix);
            if (timestamp == null)
                return null;
            return df_datetime.format(timestamp);
        default:
            return rs.getString(ix);
        }
    }
}

Related

  1. getFirstInt(ResultSet resultSet)
  2. getFlags(ResultSet rs)
  3. getGeneratedIdFromResultSet(ResultSet resultSet)
  4. getHashMap(ResultSet resultSet)
  5. getHeaders(ResultSetMetaData rsmd)
  6. getHtmlRows(ResultSet results)
  7. getHtmlTable(ResultSet results)
  8. getId(ResultSet key)
  9. getIndex(ResultSet rs, int index)