Java SQL ResultSet String Read getString(ResultSet rs, int ix, int sql_type)

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

Description

get String

License

Apache License

Declaration

public static String getString(ResultSet rs, int ix, int sql_type) throws SQLException 

Method Source Code

//package com.java2s;
/*//from w  w  w .j a  v a  2 s. c o  m
 * #!
 * 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 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. getString(ResultSet p_rset, int p_col)
  2. getString(ResultSet res, String name)
  3. getString(ResultSet resultSet, String columnName)
  4. getString(ResultSet resultSet, String columnName)
  5. getString(ResultSet rs, int index, boolean trim)
  6. getString(ResultSet rs, String column)
  7. getString(ResultSet rs, String columnLabel)
  8. getString(ResultSet rs, String columnName)
  9. getString(ResultSet rs, String name)