List of usage examples for org.springframework.jdbc.core PreparedStatementCreator createPreparedStatement
PreparedStatement createPreparedStatement(Connection con) throws SQLException;
From source file:io.kahu.hawaii.util.call.sql.AbortableQuery.java
@Override protected void executeInternally(ResponseHandler responseHandler, Response response) throws ServerException { SqlParameterSource paramSource = new MapSqlParameterSource(params); PreparedStatementCreator psc = getPreparedStatementCreator(sql, paramSource); Connection connection = DataSourceUtils.getConnection(dataSource); try {/*from w ww . jav a 2 s .co m*/ preparedStatement = psc.createPreparedStatement(connection); switch (callType) { case INSERT: preparedStatement.executeUpdate(); new UpdateIdResponseHandler().addToResponse(preparedStatement, response); break; case DELETE: // fall though case UPDATE: response.set(preparedStatement.executeUpdate()); break; case SELECT: ResultSet resultSet = preparedStatement.executeQuery(); responseHandler.addToResponse(resultSet, response); break; default: throw new ServerException(ServerError.ILLEGAL_ARGUMENT, "Unknown call type '" + callType + "'."); } } catch (SQLException e) { try { connection.rollback(); } catch (Throwable t) { // } if (!aborted) { response.setStatus(ResponseStatus.BACKEND_FAILURE, e); } throw new ServerException(ServerError.UNEXPECTED_EXCEPTION, e); } finally { DataSourceUtils.releaseConnection(connection, dataSource); } }
From source file:com.danidemi.jlubricant.springbatch.NamedJdbcCursorItemReader.java
@Override protected void openCursor(Connection con) { //MyParsedSqlDel parsedSql = getParsedSql(sql); //String sqlToUse = NamedParameterUtils.substituteNamedParameters(parsedSql.getDelegate(), paramSource); //String theSql = sqlToUse; try {// w w w .j a va 2s .c om // if (isUseSharedExtendedConnection()) { // preparedStatement = con.prepareStatement(theSql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, // ResultSet.HOLD_CURSORS_OVER_COMMIT); // } // else { // preparedStatement = con.prepareStatement(theSql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); // } // applyStatementSettings(preparedStatement); // if (this.preparedStatementSetter != null) { // // // preparedStatementSetter.setValues(preparedStatement); // // // // // // } // this.rs = preparedStatement.executeQuery(); ParsedSql parsedSql1 = this.getParsedSql(sql).getDelegate(); String sqlToUse1 = NamedParameterUtils.substituteNamedParameters(parsedSql1, paramSource); Object[] params = NamedParameterUtils.buildValueArray(parsedSql1, paramSource, null); List<SqlParameter> declaredParameters = NamedParameterUtils.buildSqlParameterList(parsedSql1, paramSource); PreparedStatementCreatorFactory pscf = new PreparedStatementCreatorFactory(sqlToUse1, declaredParameters); pscf.setResultSetType(ResultSet.TYPE_FORWARD_ONLY); pscf.setUpdatableResults(false); PreparedStatementCreator preparedStatementCreator = pscf.newPreparedStatementCreator(params); //con.prepareStatement(theSql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); preparedStatement = preparedStatementCreator.createPreparedStatement(con); this.rs = preparedStatement.executeQuery(); handleWarnings(preparedStatement); } catch (SQLException se) { close(); throw getExceptionTranslator().translate("Executing query", getSql(), se); } }
From source file:com.github.ferstl.spring.jdbc.oracle.OracleNamedParameterJdbcTemplateTest.java
@Test @Ignore("unsupported") public void endsWithCollection() throws SQLException { Map<String, Object> map = new HashMap<>(3); map.put("ten", 10); map.put("twenty", 20); map.put("collection", Arrays.asList(1, 23, 42)); String sql = "SELECT 1 FROM dual WHERE 10 = :ten or 42 in (:collection)"; String expectedSql = "SELECT 1 FROM dual WHERE 10 = :ten or 42 in (?,?,?)"; PreparedStatementCreator preparedStatementCreator = this.namedJdbcTemplate.getPreparedStatementCreator(sql, new MapSqlParameterSource(map)); Connection connection = mock(Connection.class); PreparedStatement preparedStatement = mock(PreparedStatement.class); when(connection.prepareStatement(expectedSql)).thenReturn(preparedStatement); preparedStatementCreator.createPreparedStatement(connection); verify(preparedStatement).setObject(1, 10); verify(preparedStatement).setObject(2, 1); verify(preparedStatement).setObject(3, 23); verify(preparedStatement).setObject(4, 42); }
From source file:com.github.ferstl.spring.jdbc.oracle.OracleNamedParameterJdbcTemplateTest.java
@Test @Ignore("unsupported") public void endsWithCollectionSpace() throws SQLException { Map<String, Object> map = new HashMap<>(3); map.put("ten", 10); map.put("twenty", 20); map.put("collection", Arrays.asList(1, 23, 42)); String sql = "SELECT 1 FROM dual WHERE 10 = :ten or 42 in (:collection) "; String expectedSql = "SELECT 1 FROM dual WHERE 10 = :ten or 42 in (?,?,?) "; PreparedStatementCreator preparedStatementCreator = this.namedJdbcTemplate.getPreparedStatementCreator(sql, new MapSqlParameterSource(map)); Connection connection = mock(Connection.class); PreparedStatement preparedStatement = mock(PreparedStatement.class); when(connection.prepareStatement(expectedSql)).thenReturn(preparedStatement); preparedStatementCreator.createPreparedStatement(connection); verify(preparedStatement).setObject(1, 10); verify(preparedStatement).setObject(2, 1); verify(preparedStatement).setObject(3, 23); verify(preparedStatement).setObject(4, 42); }
From source file:com.github.ferstl.spring.jdbc.oracle.OracleNamedParameterJdbcTemplateTest.java
@Test @Ignore("unsupported") public void collection() throws SQLException { Map<String, Object> map = new HashMap<>(3); map.put("ten", 10); map.put("twenty", 20); map.put("collection", Arrays.asList(1, 23, 42)); String sql = "SELECT 1 FROM dual WHERE 10 = :ten or 42 in (:collection) or 20 = :twenty"; String expectedSql = "SELECT 1 FROM dual WHERE 10 = :ten or 42 in (?,?,?) or 20 = :twenty"; PreparedStatementCreator preparedStatementCreator = this.namedJdbcTemplate.getPreparedStatementCreator(sql, new MapSqlParameterSource(map)); Connection connection = mock(Connection.class); PreparedStatement preparedStatement = mock(PreparedStatement.class); when(connection.prepareStatement(expectedSql)).thenReturn(preparedStatement); preparedStatementCreator.createPreparedStatement(connection); verify(preparedStatement).setObject(1, 10); verify(preparedStatement).setObject(2, 1); verify(preparedStatement).setObject(3, 23); verify(preparedStatement).setObject(4, 42); verify(preparedStatement).setObject(5, 20); }
From source file:com.github.ferstl.spring.jdbc.oracle.OracleNamedParameterJdbcTemplateTest.java
@Test public void setWithType() throws SQLException { MapSqlParameterSource source = new MapSqlParameterSource(new HashMap<String, Object>(2)); source.addValue("ten", 10, Types.NUMERIC); source.addValue("twenty", null, Types.VARCHAR); String sql = "SELECT 1 FROM dual WHERE 1 = :ten or 20 = :twenty"; PreparedStatementCreator preparedStatementCreator = this.namedJdbcTemplate.getPreparedStatementCreator(sql, source);/*from w w w .ja va2 s . co m*/ Connection connection = mock(Connection.class); PreparedStatement preparedStatement = mock(PreparedStatement.class); OraclePreparedStatement oracleStatement = mock(OraclePreparedStatement.class); when(connection.prepareStatement(sql)).thenReturn(preparedStatement); when(preparedStatement.unwrap(OraclePreparedStatement.class)).thenReturn(oracleStatement); preparedStatementCreator.createPreparedStatement(connection); verify(oracleStatement).setObjectAtName("ten", 10, Types.NUMERIC); verify(oracleStatement).setNullAtName("twenty", Types.VARCHAR); }
From source file:com.github.ferstl.spring.jdbc.oracle.OracleNamedParameterJdbcTemplateTest.java
@Test public void repetition() throws SQLException { Map<String, Object> map = new HashMap<>(3); map.put("ten", 10); String sql = "SELECT 1 FROM dual WHERE 10 = :ten or 0 < :ten "; PreparedStatementCreator preparedStatementCreator = this.namedJdbcTemplate.getPreparedStatementCreator(sql, new MapSqlParameterSource(map)); Connection connection = mock(Connection.class); PreparedStatement preparedStatement = mock(PreparedStatement.class); OraclePreparedStatement oracleStatement = mock(OraclePreparedStatement.class); when(connection.prepareStatement(sql)).thenReturn(preparedStatement); when(preparedStatement.unwrap(OraclePreparedStatement.class)).thenReturn(oracleStatement); preparedStatementCreator.createPreparedStatement(connection); verify(oracleStatement).setObjectAtName("ten", 10); }
From source file:com.github.ferstl.spring.jdbc.oracle.OracleNamedParameterJdbcTemplateTest.java
@Test public void endingNoSpace() throws SQLException { Map<String, Object> map = new HashMap<>(3); map.put("ten", 10); map.put("twenty", 20); String sql = "SELECT 1 FROM dual WHERE 1 = :ten or 20 = :twenty"; PreparedStatementCreator preparedStatementCreator = this.namedJdbcTemplate.getPreparedStatementCreator(sql, new MapSqlParameterSource(map)); Connection connection = mock(Connection.class); PreparedStatement preparedStatement = mock(PreparedStatement.class); OraclePreparedStatement oracleStatement = mock(OraclePreparedStatement.class); when(connection.prepareStatement(sql)).thenReturn(preparedStatement); when(preparedStatement.unwrap(OraclePreparedStatement.class)).thenReturn(oracleStatement); preparedStatementCreator.createPreparedStatement(connection); verify(oracleStatement).setObjectAtName("ten", 10); verify(oracleStatement).setObjectAtName("twenty", 20); }
From source file:com.github.ferstl.spring.jdbc.oracle.OracleNamedParameterJdbcTemplateTest.java
@Test public void setNullNoType() throws SQLException { Map<String, Object> map = new HashMap<>(2); map.put("ten", 10); map.put("twenty", null); String sql = "SELECT 1 FROM dual WHERE 1 = :ten or 20 = :twenty"; PreparedStatementCreator preparedStatementCreator = this.namedJdbcTemplate.getPreparedStatementCreator(sql, new MapSqlParameterSource(map)); Connection connection = mock(Connection.class); PreparedStatement preparedStatement = mock(PreparedStatement.class); OraclePreparedStatement oracleStatement = mock(OraclePreparedStatement.class); when(connection.prepareStatement(sql)).thenReturn(preparedStatement); when(preparedStatement.unwrap(OraclePreparedStatement.class)).thenReturn(oracleStatement); preparedStatementCreator.createPreparedStatement(connection); verify(oracleStatement).setObjectAtName("ten", 10); verify(oracleStatement).setNullAtName("twenty", Types.NULL); }