Here you can find the source of setTimestampOrNull(PreparedStatement pstmt, int paramIndex, Timestamp value)
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 |
Parameter | Description |
---|---|
SQLException | if a database access error occurs. |
public static void setTimestampOrNull(PreparedStatement pstmt, int paramIndex, Timestamp value) throws SQLException
//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); } } }