Java Timestamp setTimestampOrNull(PreparedStatement pstmt, int paramIndex, Timestamp value)

Here you can find the source of setTimestampOrNull(PreparedStatement pstmt, int paramIndex, Timestamp value)

Description

Sets a parameter in a PreparedStatement to either a value or null.

License

Open Source License

Parameter

Parameter Description
pstmt the prepared statement
paramIndex the parameter index of the parameter to be set
value the value to which the parameter must be set

Exception

Parameter Description
SQLException if a database access error occurs.

Declaration

public static void setTimestampOrNull(PreparedStatement pstmt, int paramIndex, Timestamp value)
        throws SQLException 

Method Source Code

//package com.java2s;

import java.io.StringReader;

import java.sql.PreparedStatement;

import java.sql.SQLException;

import java.sql.Timestamp;
import java.sql.Types;

public class Main {
    /**/*  w  w w  . jav  a  2s.  c o  m*/
     * Sets a parameter in a PreparedStatement to either a value or null.
     *
     * @param pstmt the prepared statement
     * @param paramIndex the parameter index of the parameter to be set
     * @param value the value to which the parameter must be set
     * @throws SQLException if a database access error occurs.
     */
    public static void setTimestampOrNull(PreparedStatement pstmt, int paramIndex, Timestamp value)
            throws SQLException {
        setParameterOrNull(pstmt, paramIndex, value, Types.TIMESTAMP);
    }

    private static void setParameterOrNull(PreparedStatement pstmt, int paramIndex, Object value, int type)
            throws SQLException {
        if (value != null) {
            switch (type) {
            case Types.VARCHAR:
            case Types.CHAR:
                pstmt.setString(paramIndex, (String) value);
                break;
            case Types.CLOB:
                final String strVal = (String) value;
                pstmt.setCharacterStream(paramIndex, new StringReader(strVal), strVal.length());
                break;
            case Types.TIMESTAMP:
                final Timestamp tstampVal = (Timestamp) value;
                pstmt.setTimestamp(paramIndex, tstampVal);
                break;
            case Types.INTEGER:
                final Integer intVal = (Integer) value;
                pstmt.setInt(paramIndex, intVal.intValue());
                break;
            default:
                throw new RuntimeException("Unexpected SQL type " + type + " encountered");
            }
        } else {
            pstmt.setNull(paramIndex, type);
        }
    }
}

Related

  1. roundToHour(Timestamp arg)
  2. roundToSecond(Timestamp t)
  3. safeTimestamp(Date value)
  4. setTimestamp(byte[] ba, int offset, Timestamp val)
  5. setTimestamp(int index, Timestamp t, PreparedStatement stmt)
  6. sqlDate(Timestamp t)
  7. sqlDateConvertoStr(Timestamp date)
  8. sqlDateToCalendar(Timestamp date)
  9. SQLTimestampToDate(java.sql.Timestamp ts)