Example usage for java.sql PreparedStatement setTime

List of usage examples for java.sql PreparedStatement setTime

Introduction

In this page you can find the example usage for java.sql PreparedStatement setTime.

Prototype

void setTime(int parameterIndex, java.sql.Time x) throws SQLException;

Source Link

Document

Sets the designated parameter to the given java.sql.Time value.

Usage

From source file:org.ramadda.repository.database.DatabaseManager.java

/**
 * _more_/*from   w w  w  .ja  va2s. co  m*/
 *
 * @param statement _more_
 * @param col _more_
 * @param date _more_
 *
 * @throws Exception _more_
 */
public void setDate(PreparedStatement statement, int col, Date date) throws Exception {
    //        if (!db.equals(DB_MYSQL)) {
    if (true || !db.equals(DB_MYSQL)) {
        setTimestamp(statement, col, date);
    } else {
        if (date == null) {
            statement.setTime(col, null);
        } else {
            statement.setTime(col, new java.sql.Time(date.getTime()), Repository.calendar);
        }
    }
}

From source file:org.seasar.dbflute.logic.replaceschema.loaddata.impl.DfAbsractDataWriter.java

protected boolean processDate(String tableName, String columnName, String value, Connection conn,
        PreparedStatement ps, int bindCount, Map<String, DfColumnMeta> columnInfoMap) throws SQLException {
    if (value == null) {
        return false; // basically no way
    }//from w  w  w . j  a  va 2 s.  c  o  m
    final DfColumnMeta columnInfo = columnInfoMap.get(columnName);
    if (columnInfo != null) {
        final Class<?> columnType = getBindType(tableName, columnInfo);
        if (columnType != null) {
            if (!java.util.Date.class.isAssignableFrom(columnType)) {
                return false;
            }
            bindNotNullValueByColumnType(tableName, columnName, conn, ps, bindCount, value, columnType);
            return true;
        }
    }
    // if meta data is not found (basically no way)
    try {
        Timestamp timestamp = DfTypeUtil.toTimestamp(value);
        ps.setTimestamp(bindCount, timestamp);
        return true;
    } catch (ParseTimestampException ignored) {
        // retry as time
        try {
            Time time = DfTypeUtil.toTime(value);
            ps.setTime(bindCount, time);
            return true;
        } catch (ParseTimeException ignored2) {
        }
        return false; // couldn't parse as timestamp and time
    }
}

From source file:org.springframework.jdbc.core.StatementCreatorUtils.java

private static void setValue(PreparedStatement ps, int paramIndex, int sqlType, @Nullable String typeName,
        @Nullable Integer scale, Object inValue) throws SQLException {

    if (inValue instanceof SqlTypeValue) {
        ((SqlTypeValue) inValue).setTypeValue(ps, paramIndex, sqlType, typeName);
    } else if (inValue instanceof SqlValue) {
        ((SqlValue) inValue).setValue(ps, paramIndex);
    } else if (sqlType == Types.VARCHAR || sqlType == Types.LONGVARCHAR) {
        ps.setString(paramIndex, inValue.toString());
    } else if (sqlType == Types.NVARCHAR || sqlType == Types.LONGNVARCHAR) {
        ps.setNString(paramIndex, inValue.toString());
    } else if ((sqlType == Types.CLOB || sqlType == Types.NCLOB) && isStringValue(inValue.getClass())) {
        String strVal = inValue.toString();
        if (strVal.length() > 4000) {
            // Necessary for older Oracle drivers, in particular when running against an Oracle 10 database.
            // Should also work fine against other drivers/databases since it uses standard JDBC 4.0 API.
            if (sqlType == Types.NCLOB) {
                ps.setNClob(paramIndex, new StringReader(strVal), strVal.length());
            } else {
                ps.setClob(paramIndex, new StringReader(strVal), strVal.length());
            }//from  www. j a  va  2s. c o m
            return;
        } else {
            // Fallback: setString or setNString binding
            if (sqlType == Types.NCLOB) {
                ps.setNString(paramIndex, strVal);
            } else {
                ps.setString(paramIndex, strVal);
            }
        }
    } else if (sqlType == Types.DECIMAL || sqlType == Types.NUMERIC) {
        if (inValue instanceof BigDecimal) {
            ps.setBigDecimal(paramIndex, (BigDecimal) inValue);
        } else if (scale != null) {
            ps.setObject(paramIndex, inValue, sqlType, scale);
        } else {
            ps.setObject(paramIndex, inValue, sqlType);
        }
    } else if (sqlType == Types.BOOLEAN) {
        if (inValue instanceof Boolean) {
            ps.setBoolean(paramIndex, (Boolean) inValue);
        } else {
            ps.setObject(paramIndex, inValue, Types.BOOLEAN);
        }
    } else if (sqlType == Types.DATE) {
        if (inValue instanceof java.util.Date) {
            if (inValue instanceof java.sql.Date) {
                ps.setDate(paramIndex, (java.sql.Date) inValue);
            } else {
                ps.setDate(paramIndex, new java.sql.Date(((java.util.Date) inValue).getTime()));
            }
        } else if (inValue instanceof Calendar) {
            Calendar cal = (Calendar) inValue;
            ps.setDate(paramIndex, new java.sql.Date(cal.getTime().getTime()), cal);
        } else {
            ps.setObject(paramIndex, inValue, Types.DATE);
        }
    } else if (sqlType == Types.TIME) {
        if (inValue instanceof java.util.Date) {
            if (inValue instanceof java.sql.Time) {
                ps.setTime(paramIndex, (java.sql.Time) inValue);
            } else {
                ps.setTime(paramIndex, new java.sql.Time(((java.util.Date) inValue).getTime()));
            }
        } else if (inValue instanceof Calendar) {
            Calendar cal = (Calendar) inValue;
            ps.setTime(paramIndex, new java.sql.Time(cal.getTime().getTime()), cal);
        } else {
            ps.setObject(paramIndex, inValue, Types.TIME);
        }
    } else if (sqlType == Types.TIMESTAMP) {
        if (inValue instanceof java.util.Date) {
            if (inValue instanceof java.sql.Timestamp) {
                ps.setTimestamp(paramIndex, (java.sql.Timestamp) inValue);
            } else {
                ps.setTimestamp(paramIndex, new java.sql.Timestamp(((java.util.Date) inValue).getTime()));
            }
        } else if (inValue instanceof Calendar) {
            Calendar cal = (Calendar) inValue;
            ps.setTimestamp(paramIndex, new java.sql.Timestamp(cal.getTime().getTime()), cal);
        } else {
            ps.setObject(paramIndex, inValue, Types.TIMESTAMP);
        }
    } else if (sqlType == SqlTypeValue.TYPE_UNKNOWN || (sqlType == Types.OTHER
            && "Oracle".equals(ps.getConnection().getMetaData().getDatabaseProductName()))) {
        if (isStringValue(inValue.getClass())) {
            ps.setString(paramIndex, inValue.toString());
        } else if (isDateValue(inValue.getClass())) {
            ps.setTimestamp(paramIndex, new java.sql.Timestamp(((java.util.Date) inValue).getTime()));
        } else if (inValue instanceof Calendar) {
            Calendar cal = (Calendar) inValue;
            ps.setTimestamp(paramIndex, new java.sql.Timestamp(cal.getTime().getTime()), cal);
        } else {
            // Fall back to generic setObject call without SQL type specified.
            ps.setObject(paramIndex, inValue);
        }
    } else {
        // Fall back to generic setObject call with SQL type specified.
        ps.setObject(paramIndex, inValue, sqlType);
    }
}

From source file:org.wso2.carbon.dataservices.core.description.query.SQLQuery.java

private void setTimeValue(int queryType, String paramName, String value, String paramType,
        PreparedStatement sqlQuery, int i) throws SQLException, DataServiceFault {
    Time time = null;// ww  w . j av  a  2 s.co m
    try {
        if (value != null) {
            time = DBUtils.getTime(value);
        }
    } catch (ParseException e) {
        throw new DataServiceFault(e, "Incorrect Time format for parameter : " + paramName
                + ". Time should be in the format hh:mm:ss");
    } catch (DataServiceFault e) {
        throw new DataServiceFault(e,
                "Error processing parameter - " + paramName + ", Error - " + e.getMessage());
    }
    if ("IN".equals(paramType)) {
        if (queryType == SQLQuery.DS_QUERY_TYPE_NORMAL) {
            if (value == null) {
                sqlQuery.setNull(i + 1, java.sql.Types.TIME);
            } else {
                sqlQuery.setTime(i + 1, time);
            }
        } else {
            if (value == null) {
                ((CallableStatement) sqlQuery).setNull(i + 1, java.sql.Types.TIME);
            } else {
                ((CallableStatement) sqlQuery).setTime(i + 1, time);
            }
        }
    } else if ("INOUT".equals(paramType)) {
        if (value == null) {
            ((CallableStatement) sqlQuery).setNull(i + 1, java.sql.Types.TIME);
        } else {
            ((CallableStatement) sqlQuery).setTime(i + 1, time);
        }
        ((CallableStatement) sqlQuery).registerOutParameter(i + 1, java.sql.Types.TIME);
    } else {
        ((CallableStatement) sqlQuery).registerOutParameter(i + 1, java.sql.Types.TIME);
    }
}

From source file:org.wso2.carbon.dataservices.core.odata.RDBMSDataHandler.java

/**
 * This method bind values to prepared statement.
 *
 * @param type            data Type//from   w  w w . j  a va 2 s  . co m
 * @param value           String value
 * @param ordinalPosition Ordinal Position
 * @param sqlStatement    Statement
 * @throws SQLException
 * @throws ParseException
 * @throws ODataServiceFault
 */
private void bindValuesToPreparedStatement(int type, String value, int ordinalPosition,
        PreparedStatement sqlStatement) throws SQLException, ParseException, ODataServiceFault {
    byte[] data;
    try {
        switch (type) {
        case Types.INTEGER:
            if (value == null) {
                sqlStatement.setNull(ordinalPosition, type);
            } else {
                sqlStatement.setInt(ordinalPosition, ConverterUtil.convertToInt(value));
            }
            break;
        case Types.TINYINT:
            if (value == null) {
                sqlStatement.setNull(ordinalPosition, type);
            } else {
                sqlStatement.setByte(ordinalPosition, ConverterUtil.convertToByte(value));
            }
            break;
        case Types.SMALLINT:
            if (value == null) {
                sqlStatement.setNull(ordinalPosition, type);
            } else {
                sqlStatement.setShort(ordinalPosition, ConverterUtil.convertToShort(value));
            }
            break;
        case Types.DOUBLE:
            if (value == null) {
                sqlStatement.setNull(ordinalPosition, type);
            } else {
                sqlStatement.setDouble(ordinalPosition, ConverterUtil.convertToDouble(value));
            }
            break;
        case Types.VARCHAR:
            /* fall through */
        case Types.CHAR:
            /* fall through */
        case Types.LONGVARCHAR:
            if (value == null) {
                sqlStatement.setNull(ordinalPosition, type);
            } else {
                sqlStatement.setString(ordinalPosition, value);
            }
            break;
        case Types.CLOB:
            if (value == null) {
                sqlStatement.setNull(ordinalPosition, type);
            } else {
                sqlStatement.setClob(ordinalPosition, new BufferedReader(new StringReader(value)),
                        value.length());
            }
            break;
        case Types.BOOLEAN:
            /* fall through */
        case Types.BIT:
            if (value == null) {
                sqlStatement.setNull(ordinalPosition, type);
            } else {
                sqlStatement.setBoolean(ordinalPosition, ConverterUtil.convertToBoolean(value));
            }
            break;
        case Types.BLOB:
            /* fall through */
        case Types.LONGVARBINARY:
            if (value == null) {
                sqlStatement.setNull(ordinalPosition, type);
            } else {
                data = this.getBytesFromBase64String(value);
                sqlStatement.setBlob(ordinalPosition, new ByteArrayInputStream(data), data.length);
            }
            break;
        case Types.BINARY:
            /* fall through */
        case Types.VARBINARY:
            if (value == null) {
                sqlStatement.setNull(ordinalPosition, type);
            } else {
                data = this.getBytesFromBase64String(value);
                sqlStatement.setBinaryStream(ordinalPosition, new ByteArrayInputStream(data), data.length);
            }
            break;
        case Types.DATE:
            if (value == null) {
                sqlStatement.setNull(ordinalPosition, type);
            } else {
                sqlStatement.setDate(ordinalPosition, DBUtils.getDate(value));
            }
            break;
        case Types.DECIMAL:
            /* fall through */
        case Types.NUMERIC:
            if (value == null) {
                sqlStatement.setNull(ordinalPosition, type);
            } else {
                sqlStatement.setBigDecimal(ordinalPosition, ConverterUtil.convertToBigDecimal(value));
            }
            break;
        case Types.FLOAT:
            /* fall through */
        case Types.REAL:
            if (value == null) {
                sqlStatement.setNull(ordinalPosition, type);
            } else {
                sqlStatement.setFloat(ordinalPosition, ConverterUtil.convertToFloat(value));
            }
            break;
        case Types.TIME:
            if (value == null) {
                sqlStatement.setNull(ordinalPosition, type);
            } else {
                sqlStatement.setTime(ordinalPosition, DBUtils.getTime(value));
            }
            break;
        case Types.LONGNVARCHAR:
            /* fall through */
        case Types.NCHAR:
            /* fall through */
        case Types.NVARCHAR:
            if (value == null) {
                sqlStatement.setNull(ordinalPosition, type);
            } else {
                sqlStatement.setNString(ordinalPosition, value);
            }
            break;
        case Types.NCLOB:
            if (value == null) {
                sqlStatement.setNull(ordinalPosition, type);
            } else {
                sqlStatement.setNClob(ordinalPosition, new BufferedReader(new StringReader(value)),
                        value.length());
            }
            break;
        case Types.BIGINT:
            if (value == null) {
                sqlStatement.setNull(ordinalPosition, type);
            } else {
                sqlStatement.setLong(ordinalPosition, ConverterUtil.convertToLong(value));
            }
            break;
        case Types.TIMESTAMP:
            if (value == null) {
                sqlStatement.setNull(ordinalPosition, type);
            } else {
                sqlStatement.setTimestamp(ordinalPosition, DBUtils.getTimestamp(value));
            }
            break;
        default:
            if (value == null) {
                sqlStatement.setNull(ordinalPosition, type);
            } else {
                sqlStatement.setString(ordinalPosition, value);
            }
            break;
        }
    } catch (DataServiceFault e) {
        throw new ODataServiceFault(e, "Error occurred while binding values. :" + e.getMessage());
    }
}

From source file:org.wso2.carbon.ml.model.internal.DatabaseHandler.java

/**
 * This method initialize insering model into the database
 *
 * @param modelID            Model ID/*from ww w  .  j ava2s.  co m*/
 * @param workflowID         Workflow ID
 * @param executionStartTime Model execution start time
 * @throws DatabaseHandlerException
 */
public void insertModel(String modelID, String workflowID, Time executionStartTime)
        throws DatabaseHandlerException {
    Connection connection = null;
    PreparedStatement insertStatement = null;
    try {
        // insert model settings to the database.
        connection = this.dataSource.getConnection();
        connection.setAutoCommit(false);
        insertStatement = connection.prepareStatement(SQLQueries.INSERT_ML_MODEL);
        insertStatement.setString(1, modelID);
        insertStatement.setString(2, workflowID);
        insertStatement.setTime(3, executionStartTime);
        insertStatement.execute();
        connection.commit();
        if (logger.isDebugEnabled()) {
            logger.debug("Succesfully inserted model details for model id " + modelID);
        }
    } catch (SQLException e) {
        // rollback the changes.
        MLDatabaseUtils.rollBack(connection);
        throw new DatabaseHandlerException("An error occured while inserting model details for model id "
                + modelID + " " + "to the database: " + e.getMessage(), e);
    } finally {
        // enable auto commit.
        MLDatabaseUtils.enableAutoCommit(connection);
        // close the database resources.
        MLDatabaseUtils.closeDatabaseResources(connection, insertStatement);
    }
}

From source file:org.wso2.carbon.ml.model.internal.DatabaseHandler.java

/**
 * This method inserts model and model summary into the database
 *
 * @param modelID          Model ID/*  w  w  w. j a  va 2 s.co m*/
 * @param model            Machine learning model
 * @param modelSummary     Machine learning model summary
 * @param executionEndTime Model execution end time
 * @param <T>              Type of machine learning  model
 * @throws DatabaseHandlerException
 */
public <T> void updateModel(String modelID, T model, ModelSummary modelSummary, Time executionEndTime)
        throws DatabaseHandlerException {
    Connection connection = null;
    PreparedStatement updateStatement = null;
    try {
        connection = this.dataSource.getConnection();
        connection.setAutoCommit(false);
        updateStatement = connection.prepareStatement(SQLQueries.UPDATE_ML_MODEL);
        updateStatement.setObject(1, model);
        updateStatement.setObject(2, modelSummary);
        updateStatement.setTime(3, executionEndTime);
        updateStatement.setString(4, modelID);
        updateStatement.execute();
        connection.commit();
        if (logger.isDebugEnabled()) {
            logger.debug("Successfully updated the details of model: model ID" + modelID);
        }

    } catch (SQLException e) {
        // rollback the changes
        MLDatabaseUtils.rollBack(connection);
        throw new DatabaseHandlerException(
                "An error occured while updating the details of model id " + modelID + " : " + e.getMessage(),
                e);

    } finally {
        // enable auto commit
        MLDatabaseUtils.enableAutoCommit(connection);
        // close the database resources
        MLDatabaseUtils.closeDatabaseResources(connection, updateStatement);
    }
}

From source file:org.wso2.ws.dataservice.DBUtils.java

public static PreparedStatement getProcessedPreparedStatement(HashMap inputs, HashMap params,
        HashMap paramOrder, HashMap originalParamNames, HashMap paramTypes, Connection conn,
        String sqlStatement, String callee, String serviceName) throws AxisFault {

    String paramName = null;//ww  w . jav  a 2 s. c om
    String originalParamName = null;
    String sqlType = null;
    String value = null;
    String paramType = null;

    log.debug("[" + serviceName + "] Processing prepared statement for SQL " + sqlStatement);
    Set paramNames = params.keySet();
    Object pramNameArray[] = paramNames.toArray();

    try {
        PreparedStatement sqlQuery = null;
        if ("SQL".equals(callee)) {
            sqlQuery = conn.prepareStatement(sqlStatement);
            //SQL expects parameters, but not params set in config file
            if (sqlStatement.indexOf("?") > -1 && pramNameArray.length == 0) {
                throw new AxisFault(
                        "[" + serviceName + "]  SQL : " + sqlStatement + " expects one or more parameters. "
                                + "But none is mentioned in the configuration file.");
            }
        } else if ("STORED-PROCEDURE".equals(callee)) {
            sqlQuery = conn.prepareCall(sqlStatement);
        }

        for (int i = 0; i < pramNameArray.length; i++) {
            paramName = (String) paramOrder.get(new Integer(i + 1));
            originalParamName = (String) originalParamNames.get(new Integer(i + 1));
            sqlType = (String) params.get(paramName);
            paramType = (String) paramTypes.get(paramName);
            value = (String) inputs.get(paramName);
            log.debug("[" + serviceName + "]  Param name : " + paramName + " SQL Type : " + sqlType
                    + " Value : " + value);

            if ("IN".equals(paramType) || "INOUT".equals(paramType)) {
                if (value == null || value.trim().length() == 0) {
                    log.error("[" + serviceName + "]  Empty value found for parameter : " + originalParamName);
                    throw new AxisFault(
                            "[" + serviceName + "]  Empty value found for parameter : " + originalParamName);
                }
            }
            //work-a-round for setting NULL
            if ("NULL".equalsIgnoreCase(value)) {
                value = null;
            }
            //TODO : work-a-round for setting space

            if (sqlType == null) {
                // Defaults to string
                if ("IN".equals(paramType)) {
                    sqlQuery.setString(i + 1, value);
                } else if ("INOUT".equals(paramType)) {
                    sqlQuery.setString(i + 1, value);
                    ((CallableStatement) sqlQuery).registerOutParameter(i + 1, java.sql.Types.VARCHAR);
                } else {
                    ((CallableStatement) sqlQuery).registerOutParameter(i + 1, java.sql.Types.VARCHAR);
                }
            } else if (DBConstants.DataTypes.INTEGER.equals(sqlType)) {
                if ("IN".equals(paramType)) {
                    if ("SQL".equals(callee)) {
                        sqlQuery.setInt(i + 1, Integer.parseInt(value));
                    } else {
                        ((CallableStatement) sqlQuery).setInt(i + 1, Integer.parseInt(value));
                    }
                } else if ("INOUT".equals(paramType)) {
                    ((CallableStatement) sqlQuery).setInt(i + 1, Integer.parseInt(value));
                    ((CallableStatement) sqlQuery).registerOutParameter(i + 1, java.sql.Types.INTEGER);
                } else {
                    ((CallableStatement) sqlQuery).registerOutParameter(i + 1, java.sql.Types.INTEGER);
                }
            } else if (DBConstants.DataTypes.STRING.equals(sqlType)) {
                if ("IN".equals(paramType)) {
                    if ("SQL".equals(callee)) {
                        sqlQuery.setString(i + 1, value);
                    } else {
                        ((CallableStatement) sqlQuery).setString(i + 1, value);
                    }
                } else if ("INOUT".equals(paramType)) {
                    ((CallableStatement) sqlQuery).setString(i + 1, value);
                    ((CallableStatement) sqlQuery).registerOutParameter(i + 1, java.sql.Types.VARCHAR);
                } else {
                    ((CallableStatement) sqlQuery).registerOutParameter(i + 1, java.sql.Types.VARCHAR);
                }
            } else if (DBConstants.DataTypes.DOUBLE.equals(sqlType)) {
                if ("IN".equals(paramType)) {
                    if ("SQL".equals(callee)) {
                        sqlQuery.setDouble(i + 1, Double.parseDouble(value));
                    } else {
                        ((CallableStatement) sqlQuery).setDouble(i + 1, Double.parseDouble(value));
                    }
                } else if ("INOUT".equals(paramType)) {
                    ((CallableStatement) sqlQuery).setDouble(i + 1, Double.parseDouble(value));
                    ((CallableStatement) sqlQuery).registerOutParameter(i + 1, java.sql.Types.DOUBLE);
                } else {
                    ((CallableStatement) sqlQuery).registerOutParameter(i + 1, java.sql.Types.DOUBLE);
                }
            } else if (DBConstants.DataTypes.DATE.equals(sqlType)) {
                try {
                    //Only yyyy-MM-dd part is needed
                    String modifiedValue = value.substring(0, 10);
                    if ("IN".equals(paramType)) {
                        if ("SQL".equals(callee)) {
                            sqlQuery.setDate(i + 1, Date.valueOf(modifiedValue));
                        } else {
                            ((CallableStatement) sqlQuery).setDate(i + 1, Date.valueOf(modifiedValue));
                        }
                    } else if ("INOUT".equals(paramType)) {
                        ((CallableStatement) sqlQuery).setDate(i + 1, Date.valueOf(modifiedValue));
                        ((CallableStatement) sqlQuery).registerOutParameter(i + 1, java.sql.Types.DATE);
                    } else {
                        ((CallableStatement) sqlQuery).registerOutParameter(i + 1, java.sql.Types.DATE);
                    }
                } catch (IllegalArgumentException e) {
                    log.error("Incorrect date format(" + value + ") for parameter : " + paramName, e);
                    throw new AxisFault("Incorrect date format for parameter  : " + paramName
                            + ".Date should be in yyyy-mm-dd format.", e);
                }
            } else if (DBConstants.DataTypes.TIMESTAMP.equals(sqlType)) {
                Timestamp timestamp = getTimestamp(value, paramName);
                if ("IN".equals(paramType)) {
                    if ("SQL".equals(callee)) {
                        sqlQuery.setTimestamp(i + 1, timestamp);
                    } else {
                        ((CallableStatement) sqlQuery).setTimestamp(i + 1, timestamp);
                    }
                } else if ("INOUT".equals(paramType)) {
                    ((CallableStatement) sqlQuery).setTimestamp(i + 1, timestamp);
                    ((CallableStatement) sqlQuery).registerOutParameter(i + 1, java.sql.Types.TIMESTAMP);
                } else {
                    ((CallableStatement) sqlQuery).registerOutParameter(i + 1, java.sql.Types.TIMESTAMP);
                }
            } else if (DBConstants.DataTypes.TIME.equals(sqlType)) {
                Time time = getTime(value, paramName);
                if ("IN".equals(paramType)) {
                    if ("SQL".equals(callee)) {
                        sqlQuery.setTime(i + 1, time);
                    } else {
                        ((CallableStatement) sqlQuery).setTime(i + 1, time);
                    }
                } else if ("INOUT".equals(paramType)) {
                    ((CallableStatement) sqlQuery).setTime(i + 1, time);
                    ((CallableStatement) sqlQuery).registerOutParameter(i + 1, java.sql.Types.TIME);
                } else {
                    ((CallableStatement) sqlQuery).registerOutParameter(i + 1, java.sql.Types.TIME);
                }
            } else {
                log.error("[" + serviceName + "]  Unsupported data type : " + sqlType + " as input parameter.");
                throw new AxisFault("[" + serviceName + "]  Found Unsupported data type : " + sqlType
                        + " as input parameter.");
            }
        }
        return sqlQuery;
    } catch (NumberFormatException e) {
        log.error("[" + serviceName + "]  Incorrect value found for parameter : " + originalParamName, e);
        throw new AxisFault("[" + serviceName + "]  Incorrect value found for parameter : " + originalParamName,
                e);
    } catch (SQLException e) {
        log.error("[" + serviceName + "]  Error occurred while preparing prepared statement for sql : "
                + sqlStatement, e);
        throw new AxisFault("[" + serviceName
                + "]  Error occurred while preparing prepared statement for sql : " + sqlStatement, e);
    }
}

From source file:ro.nextreports.engine.queryexec.QueryExecutor.java

private void setParameterValue(PreparedStatement pstmt, Class paramValueClass, Object paramValue, int index)
        throws SQLException, QueryException {

    // for "NOT IN (?)" setting null -> result is undeterminated
    // ParameterUtil.NULL was good only for list of strings (for NOT IN)!
    if (ParameterUtil.NULL.equals(paramValue)) {
        paramValue = null;//from  w  w w .  j  a va2s.c  o  m
    }
    if (paramValueClass.equals(Object.class)) {
        if (paramValue == null) {
            pstmt.setNull(index + 1, Types.JAVA_OBJECT);
        } else {
            if (paramValue instanceof IdName) {
                pstmt.setObject(index + 1, ((IdName) paramValue).getId());
            } else {
                pstmt.setObject(index + 1, paramValue);
            }
        }
    } else if (paramValueClass.equals(Boolean.class)) {
        if (paramValue == null) {
            pstmt.setNull(index + 1, Types.BIT);
        } else {
            if (paramValue instanceof IdName) {
                pstmt.setBoolean(index + 1, (Boolean) ((IdName) paramValue).getId());
            } else {
                pstmt.setBoolean(index + 1, (Boolean) paramValue);
            }
        }
    } else if (paramValueClass.equals(Byte.class)) {
        if (paramValue == null) {
            pstmt.setNull(index + 1, Types.TINYINT);
        } else {
            if (paramValue instanceof IdName) {
                pstmt.setByte(index + 1, (Byte) ((IdName) paramValue).getId());
            } else {
                pstmt.setByte(index + 1, (Byte) paramValue);
            }
        }
    } else if (paramValueClass.equals(Double.class)) {
        if (paramValue == null) {
            pstmt.setNull(index + 1, Types.DOUBLE);
        } else {
            if (paramValue instanceof IdName) {
                pstmt.setDouble(index + 1, (Double) ((IdName) paramValue).getId());
            } else {
                pstmt.setDouble(index + 1, (Double) paramValue);
            }
        }
    } else if (paramValueClass.equals(Float.class)) {
        if (paramValue == null) {
            pstmt.setNull(index + 1, Types.FLOAT);
        } else {
            if (paramValue instanceof IdName) {
                pstmt.setFloat(index + 1, (Float) ((IdName) paramValue).getId());
            } else {
                pstmt.setFloat(index + 1, (Float) paramValue);
            }
        }
    } else if (paramValueClass.equals(Integer.class)) {
        if (paramValue == null) {
            pstmt.setNull(index + 1, Types.INTEGER);
        } else {
            if (paramValue instanceof IdName) {
                pstmt.setObject(index + 1, ((IdName) paramValue).getId());
            } else {
                pstmt.setInt(index + 1, (Integer) paramValue);
            }
        }
    } else if (paramValueClass.equals(Long.class)) {
        if (paramValue == null) {
            pstmt.setNull(index + 1, Types.BIGINT);
        } else {
            if (paramValue instanceof IdName) {
                pstmt.setLong(index + 1, (Long) ((IdName) paramValue).getId());
            } else {
                pstmt.setLong(index + 1, (Long) paramValue);
            }
        }
    } else if (paramValueClass.equals(Short.class)) {
        if (paramValue == null) {
            pstmt.setNull(index + 1, Types.SMALLINT);
        } else {
            if (paramValue instanceof IdName) {
                pstmt.setShort(index + 1, (Short) ((IdName) paramValue).getId());
            } else {
                pstmt.setShort(index + 1, (Short) paramValue);
            }
        }

        //@todo    
        // ParameterUtil -> values are taken from dialect (where there is no BigDecimal yet!)
        // or from meta  data
    } else if (paramValueClass.equals(BigDecimal.class)) {
        if (paramValue == null) {
            pstmt.setNull(index + 1, Types.DECIMAL);
        } else {
            if (paramValue instanceof IdName) {
                Serializable ser = ((IdName) paramValue).getId();
                if (ser instanceof BigDecimal) {
                    pstmt.setBigDecimal(index + 1, (BigDecimal) (ser));
                } else {
                    pstmt.setInt(index + 1, (Integer) (ser));
                }
            } else {
                // a simple value cannot be cast to BigDecimal!                   
                pstmt.setObject(index + 1, paramValue);
            }
        }
    } else if (paramValueClass.equals(BigInteger.class)) {
        if (paramValue == null) {
            pstmt.setNull(index + 1, Types.BIGINT);
        } else {
            if (paramValue instanceof IdName) {
                Serializable ser = ((IdName) paramValue).getId();
                if (ser instanceof BigInteger) {
                    pstmt.setBigDecimal(index + 1, new BigDecimal((BigInteger) (ser)));
                } else if (ser instanceof BigDecimal) {
                    pstmt.setBigDecimal(index + 1, (BigDecimal) (ser));
                } else {
                    pstmt.setInt(index + 1, (Integer) (ser));
                }
            } else {
                // a simple value cannot be cast to BigDecimal!                   
                pstmt.setObject(index + 1, paramValue);
            }
        }
    } else if (paramValueClass.equals(String.class)) {
        if (paramValue == null) {
            pstmt.setNull(index + 1, Types.VARCHAR);
        } else {
            if (paramValue instanceof IdName) {
                if (((IdName) paramValue).getId() == null) {
                    pstmt.setNull(index + 1, Types.VARCHAR);
                } else {
                    pstmt.setString(index + 1, ((IdName) paramValue).getId().toString());
                }
            } else {
                pstmt.setString(index + 1, paramValue.toString());
            }
        }
    } else if (paramValueClass.equals(Date.class)) {
        if (paramValue == null) {
            pstmt.setNull(index + 1, Types.DATE);
        } else {
            if (paramValue instanceof IdName) {
                Serializable obj = ((IdName) paramValue).getId();
                Date date;
                if (obj instanceof String) {
                    try {
                        date = IdNameRenderer.sdf.parse((String) obj);
                    } catch (ParseException e) {
                        e.printStackTrace();
                        LOG.error(e.getMessage(), e);
                        date = new Date();
                    }
                } else {
                    date = (Date) obj;
                }
                pstmt.setDate(index + 1, new java.sql.Date(date.getTime()));
            } else {
                pstmt.setDate(index + 1, new java.sql.Date(((Date) paramValue).getTime()));
            }
        }
    } else if (paramValueClass.equals(Timestamp.class)) {
        if (paramValue == null) {
            pstmt.setNull(index + 1, Types.TIMESTAMP);
        } else {
            if (paramValue instanceof IdName) {
                Serializable obj = ((IdName) paramValue).getId();
                Date date;
                if (obj instanceof String) {
                    try {
                        date = IdNameRenderer.sdf.parse((String) obj);
                    } catch (ParseException e) {
                        e.printStackTrace();
                        LOG.error(e.getMessage(), e);
                        date = new Date();
                    }
                } else {
                    date = (Date) obj;
                }

                pstmt.setTimestamp(index + 1, new Timestamp(date.getTime()));
            } else {
                pstmt.setTimestamp(index + 1, new Timestamp(((Date) paramValue).getTime()));
            }
        }
    } else if (paramValueClass.equals(Time.class)) {
        if (paramValue == null) {
            pstmt.setNull(index + 1, Types.TIME);
        } else {
            if (paramValue instanceof IdName) {
                Serializable obj = ((IdName) paramValue).getId();
                Date date;
                if (obj instanceof String) {
                    try {
                        date = IdNameRenderer.sdf.parse((String) obj);
                    } catch (ParseException e) {
                        e.printStackTrace();
                        LOG.error(e.getMessage(), e);
                        date = new Date();
                    }
                } else {
                    date = (Date) obj;
                }
                pstmt.setTime(index + 1, new Time(date.getTime()));
            } else {
                pstmt.setTime(index + 1, new Time(((Date) paramValue).getTime()));
            }
        }
    } else {
        throw new QueryException("Parameter type " + paramValueClass.getName() + " not supported in query");
    }

    // for logSql()
    statementParameters.put(index, paramValue);
}