Java Code Examples for org.springframework.jdbc.core.StatementCreatorUtils#setParameterValue()
The following examples show how to use
org.springframework.jdbc.core.StatementCreatorUtils#setParameterValue() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractJdbcInsert.java From spring-analysis-note with MIT License | 5 votes |
/** * Internal implementation for setting parameter values. * @param preparedStatement the PreparedStatement * @param values the values to be set */ private void setParameterValues(PreparedStatement preparedStatement, List<?> values, @Nullable int... columnTypes) throws SQLException { int colIndex = 0; for (Object value : values) { colIndex++; if (columnTypes == null || colIndex > columnTypes.length) { StatementCreatorUtils.setParameterValue(preparedStatement, colIndex, SqlTypeValue.TYPE_UNKNOWN, value); } else { StatementCreatorUtils.setParameterValue(preparedStatement, colIndex, columnTypes[colIndex - 1], value); } } }
Example 2
Source File: AbstractJdbcInsert.java From java-technology-stack with MIT License | 5 votes |
/** * Internal implementation for setting parameter values. * @param preparedStatement the PreparedStatement * @param values the values to be set */ private void setParameterValues(PreparedStatement preparedStatement, List<?> values, @Nullable int... columnTypes) throws SQLException { int colIndex = 0; for (Object value : values) { colIndex++; if (columnTypes == null || colIndex > columnTypes.length) { StatementCreatorUtils.setParameterValue(preparedStatement, colIndex, SqlTypeValue.TYPE_UNKNOWN, value); } else { StatementCreatorUtils.setParameterValue(preparedStatement, colIndex, columnTypes[colIndex - 1], value); } } }
Example 3
Source File: AbstractJdbcInsert.java From lams with GNU General Public License v2.0 | 5 votes |
/** * Internal implementation for setting parameter values * @param preparedStatement the PreparedStatement * @param values the values to be set */ private void setParameterValues(PreparedStatement preparedStatement, List<?> values, int... columnTypes) throws SQLException { int colIndex = 0; for (Object value : values) { colIndex++; if (columnTypes == null || colIndex > columnTypes.length) { StatementCreatorUtils.setParameterValue(preparedStatement, colIndex, SqlTypeValue.TYPE_UNKNOWN, value); } else { StatementCreatorUtils.setParameterValue(preparedStatement, colIndex, columnTypes[colIndex - 1], value); } } }
Example 4
Source File: AbstractJdbcInsert.java From spring4-understanding with Apache License 2.0 | 5 votes |
/** * Internal implementation for setting parameter values * @param preparedStatement the PreparedStatement * @param values the values to be set */ private void setParameterValues(PreparedStatement preparedStatement, List<?> values, int... columnTypes) throws SQLException { int colIndex = 0; for (Object value : values) { colIndex++; if (columnTypes == null || colIndex > columnTypes.length) { StatementCreatorUtils.setParameterValue(preparedStatement, colIndex, SqlTypeValue.TYPE_UNKNOWN, value); } else { StatementCreatorUtils.setParameterValue(preparedStatement, colIndex, columnTypes[colIndex - 1], value); } } }
Example 5
Source File: AbstractJdbcInsert.java From effectivejava with Apache License 2.0 | 5 votes |
/** * Internal implementation for setting parameter values * @param preparedStatement the PreparedStatement * @param values the values to be set */ private void setParameterValues(PreparedStatement preparedStatement, List<Object> values, int[] columnTypes) throws SQLException { int colIndex = 0; for (Object value : values) { colIndex++; if (columnTypes == null || colIndex > columnTypes.length) { StatementCreatorUtils.setParameterValue(preparedStatement, colIndex, SqlTypeValue.TYPE_UNKNOWN, value); } else { StatementCreatorUtils.setParameterValue(preparedStatement, colIndex, columnTypes[colIndex - 1], value); } } }
Example 6
Source File: RdbEventRecordLoader.java From DataLink with Apache License 2.0 | 4 votes |
@Override protected void fillPreparedStatement(PreparedStatement ps, LobCreator lobCreator, RdbEventRecord record, DbDialect dbDialect, TaskWriterContext context) throws SQLException { EventType type = record.getEventType(); // 注意insert/update语句对应的字段数序都是将主键排在后面 List<EventColumn> columns = new ArrayList<EventColumn>(); if (type.isInsert()) { columns.addAll(record.getColumns()); // insert为所有字段 columns.addAll(record.getKeys()); } else if (type.isDelete()) { columns.addAll(record.getKeys()); } else if (type.isUpdate()) { boolean existOldKeys = !CollectionUtils.isEmpty(record.getOldKeys()); boolean hasAutoIncrementNotKeyColumns = dbDialect.hasAutoIncrementNotKeyColumns(record.getSchemaName(), record.getTableName()); if (hasAutoIncrementNotKeyColumns) { columns.addAll(record.getUpdatedColumns());// 只更新带有isUpdate=true的字段 } else { columns.addAll(record.getColumns());// update、upsert都更新所有字段 } columns.addAll(record.getKeys()); if (existOldKeys) { columns.addAll(record.getOldKeys()); } } // 获取一下当前字段名的数据是否必填 boolean isSyncAutoAddColumn = context.getWriterParameter().isSyncAutoAddColumn(); Map<String, Boolean> isRequiredMap = buildRequiredMap(dbDialect, record, columns, isSyncAutoAddColumn); Table table = dbDialect.findTable(record.getSchemaName(), record.getTableName()); for (int i = 0; i < columns.size(); i++) { int paramIndex = i + 1; EventColumn column = columns.get(i); Boolean isRequired = isRequiredMap.get(StringUtils.lowerCase(column.getColumnName())); int sqlType = getSqlType(column, table, record); Object param = null; if (dbDialect instanceof MysqlDialect && (sqlType == Types.TIME || sqlType == Types.TIMESTAMP || sqlType == Types.DATE)) { // 解决mysql的0000-00-00 00:00:00问题,直接依赖mysql // driver进行处理,如果转化为Timestamp会出错 param = column.getColumnValue(); } else { param = SqlUtils.stringToSqlValue(column.getColumnValue(), sqlType, isRequired, dbDialect.isEmptyStringNulled()); } try { switch (sqlType) { case Types.CLOB: lobCreator.setClobAsString(ps, paramIndex, (String) param); break; case Types.BLOB: lobCreator.setBlobAsBytes(ps, paramIndex, param instanceof String ? ((String) param).getBytes() : (byte[]) param); break; case Types.TIME: case Types.TIMESTAMP: case Types.DATE: // 只处理mysql的时间类型,oracle的进行转化处理 if (dbDialect instanceof MysqlDialect) { // 解决mysql的0000-00-00 00:00:00问题,直接依赖mysql // driver进行处理,如果转化为Timestamp会出错 ps.setObject(paramIndex, param); } else { StatementCreatorUtils.setParameterValue(ps, paramIndex, sqlType, null, param); } break; case Types.BIT: // 只处理mysql的bit类型,bit最多存储64位,所以需要使用BigInteger进行处理才能不丢精度 // mysql driver将bit按照setInt进行处理,会导致数据越界 if (dbDialect instanceof MysqlDialect) { StatementCreatorUtils.setParameterValue(ps, paramIndex, Types.DECIMAL, null, param); } else if (dbDialect instanceof SqlServerDialect) { StatementCreatorUtils.setParameterValue(ps, paramIndex, sqlType, null, param.toString()); } else { StatementCreatorUtils.setParameterValue(ps, paramIndex, sqlType, null, param); } break; default: StatementCreatorUtils.setParameterValue(ps, paramIndex, sqlType, null, param); break; } } catch (SQLException ex) { logger.error("SetParam error , [mappingId={}, sqltype={}, value={}]", RecordMeta.mediaMapping(record).getId(), sqlType, param); throw ex; } } }
Example 7
Source File: ArgumentTypePreparedStatementSetter.java From sqlhelper with GNU Lesser General Public License v3.0 | 2 votes |
/** * Set the value for the prepared statement's specified parameter position using the passed in * value and type. This method can be overridden by sub-classes if needed. * * @param ps the PreparedStatement * @param parameterPosition index of the parameter position * @param argType the argument type * @param argValue the argument value * @throws SQLException if thrown by PreparedStatement methods */ protected void doSetValue(PreparedStatement ps, int parameterPosition, int argType, Object argValue) throws SQLException { StatementCreatorUtils.setParameterValue(ps, parameterPosition, argType, argValue); }