private static ResultSet getLobStream(Connection conn, String column, int id) throws SQLException { PreparedStatement prep = conn.prepareStatement("SELECT " + column + " FROM SYSTEM_LOB_STREAM WHERE ID=? ORDER BY PART"); prep.setInt(1, id); return prep.executeQuery(); }
PreparedStatement stm = c.prepareStatement("UPDATE user_table SET name=? WHERE id=?"); stm.setString(1, "the name"); stm.setInt(2, 345); stm.executeUpdate();
/** * Check if the current session has access to this table. * This method is called by the database. * * @param conn the connection * @param pgType the PostgreSQL type oid * @param typeMod the type modifier (typically -1) * @return the name of the given type */ public static String formatType(Connection conn, int pgType, int typeMod) throws SQLException { PreparedStatement prep = conn.prepareStatement( "select typname from pg_catalog.pg_type where oid = ? and typtypmod = ?"); prep.setInt(1, pgType); prep.setInt(2, typeMod); ResultSet rs = prep.executeQuery(); if (rs.next()) { return rs.getString(1); } return null; }
/** * see SONAR-8586 */ private static void hackFixForProjectMeasureTreeQueries(Connection connection) { int metricId = 1; try (PreparedStatement preparedStatement = connection.prepareStatement("insert into PROJECT_MEASURES (METRIC_ID,COMPONENT_UUID,ANALYSIS_UUID) values (?,?,?);")) { batchExecute( 1, 1000, preparedStatement, connection, (stmt, counter) -> { preparedStatement.setInt(1, metricId); preparedStatement.setString(2, "foo_" + counter); preparedStatement.setString(3, "bar_" + counter); }); } catch (SQLException e) { throw new RuntimeException("Failed to insert fake rows into table PROJECT_MEASURES", e); } }
private byte[] fetch() { try { prep.setInt(2, seq++); ResultSet rs = prep.executeQuery(); if (rs.next()) { return rs.getBytes(1); } return null; } catch (SQLException e) { throw DbException.convert(e); } }
/** * {@inheritDoc} */ @Override public boolean delete(Customer customer) throws Exception { try (Connection connection = getConnection(); PreparedStatement statement = connection.prepareStatement("DELETE FROM CUSTOMERS WHERE ID = ?")) { statement.setInt(1, customer.getId()); return statement.executeUpdate() > 0; } catch (SQLException ex) { throw new CustomException(ex.getMessage(), ex); } } }
@Test public void testUpdateCount() throws Exception { final String sql = "UPDATE INVOICE SET DATE_DISPATCHED = SYSDATE WHERE ID = ?"; int idParam = 11111; given(this.preparedStatement.executeUpdate()).willReturn(1); Dispatcher d = new Dispatcher(idParam, sql); int rowsAffected = this.template.update(d); assertTrue("1 update affected 1 row", rowsAffected == 1); verify(this.preparedStatement).setInt(1, idParam); verify(this.preparedStatement).close(); verify(this.connection).close(); }
@Test @TestForIssue(jiraKey = "HHH-12138") public void testNamedNativeQueryStoredProcedureRefCursorWithJDBC() { EntityManager entityManager = createEntityManager(); entityManager.getTransaction().begin(); try { Session session = entityManager.unwrap( Session.class ); session.doWork( connection -> { try ( PreparedStatement function = connection.prepareStatement( "select * from fn_person_and_phones( ? )" ) ) { function.setInt( 1, 1 ); function.execute(); try ( ResultSet resultSet = function.getResultSet() ) { while ( resultSet.next() ) { Long postCommentId = resultSet.getLong( 1 ); String review = resultSet.getString( 2 ); } } } } ); } finally { entityManager.getTransaction().rollback(); entityManager.close(); } }
@Override public PreparedStatement createPreparedStatement(Connection connection) throws SQLException { PreparedStatement ps = connection.prepareStatement(this.sql); ps.setInt(1, this.id); return ps; }
@Override public void execute(Connection connection) throws SQLException { PreparedStatement statement = ((SessionImplementor)s).getJdbcCoordinator().getStatementPreparer().prepareStatement( "SELECT * FROM STRANGE_TYPED_OBJECT WHERE ID=?" ); statement.setInt(1, id.intValue()); ResultSet resultSet = ((SessionImplementor)s).getJdbcCoordinator().getResultSetReturn().extract( statement ); assertTrue("A row should have been returned", resultSet.next()); assertTrue("Default value should have been mapped to null", resultSet.getObject("VALUE_ONE") == null); assertTrue("Default value should have been mapped to null", resultSet.getObject("VALUE_TWO") == null); assertEquals("Non-Default value should not be changed", resultSet.getInt("VALUE_THREE"), 5); assertTrue("Default value should have been mapped to null", resultSet.getObject("VALUE_FOUR") == null); } }
public Iterator<DatabaseVersion> getLastDatabaseVersions(int maxDatabaseVersionCount, int startDatabaseVersionIndex, int maxFileHistoryCount) { try (PreparedStatement preparedStatement = getStatement("databaseversion.select.master.getLastDatabaseVersions.sql")) { maxDatabaseVersionCount = (maxDatabaseVersionCount > 0) ? maxDatabaseVersionCount : Integer.MAX_VALUE; startDatabaseVersionIndex = (startDatabaseVersionIndex > 0) ? startDatabaseVersionIndex : 0; preparedStatement.setInt(1, maxDatabaseVersionCount); preparedStatement.setInt(2, startDatabaseVersionIndex); try (ResultSet resultSet = preparedStatement.executeQuery()) { return new DatabaseVersionIterator(preparedStatement.executeQuery(), true, maxFileHistoryCount); } } catch (SQLException e) { throw new RuntimeException(e); } }
public synchronized void updateVersion(int id, int version) { if (psUpdateVersion == null) return; try { psUpdateVersion.setInt(1, version); psUpdateVersion.setInt(2, id); psUpdateVersion.executeUpdate(); } catch (SQLException e) { throw DbException.convert(e); } }
static void perform(Connection cxn, long id, String selections, boolean isBlacklist) throws SQLException { try (PreparedStatement ps = cxn.prepareStatement("UPDATE pipelineselections SET selections = NULL, version = ?, filters = ? WHERE id = ?")) { ps.setInt(1, SCHEMA); ps.setString(2, asJson(selections, isBlacklist)); ps.setLong(3, id); ps.executeUpdate(); } }
/** * {@inheritDoc} */ @Override public boolean add(Customer customer) throws Exception { if (getById(customer.getId()).isPresent()) { return false; } try (Connection connection = getConnection(); PreparedStatement statement = connection.prepareStatement("INSERT INTO CUSTOMERS VALUES (?,?,?)")) { statement.setInt(1, customer.getId()); statement.setString(2, customer.getFirstName()); statement.setString(3, customer.getLastName()); statement.execute(); return true; } catch (SQLException ex) { throw new CustomException(ex.getMessage(), ex); } }
private static ResultSet getLobStream(Connection conn, String column, int id) throws SQLException { PreparedStatement prep = conn .prepareStatement("SELECT " + column + " FROM SYSTEM_LOB_STREAM WHERE ID=? ORDER BY PART"); prep.setInt(1, id); return prep.executeQuery(); }
public synchronized ArrayList<TableAlterHistoryRecord> getTableAlterHistoryRecord(int id, int versionMin, int versionMax) { ArrayList<TableAlterHistoryRecord> records = new ArrayList<>(); if (psGetTableAlterHistoryRecord == null) return records; try { psGetTableAlterHistoryRecord.setInt(1, id); psGetTableAlterHistoryRecord.setInt(2, versionMin); psGetTableAlterHistoryRecord.setInt(3, versionMax); ResultSet rs = psGetTableAlterHistoryRecord.executeQuery(); while (rs.next()) { records.add(new TableAlterHistoryRecord(rs.getInt(1), rs.getInt(2), rs.getInt(3), rs.getString(4))); } return records; } catch (SQLException e) { throw DbException.convert(e); } }
PreparedStatement update = connection.prepareStatement ("UPDATE items SET name = ?, category = ?, price = ?, quantity = ? WHERE id = ?"); update.setString(1, u.getName()); update.setString(2, u.getCategory()); ... update.setInt(5, u.getId()); update.executeUpdate();
private void setBootstrapRowToCompleted(int insertedRows, RowMap startBootstrapRow, Connection connection) throws SQLException, NoSuchElementException { String sql = "update `bootstrap` set is_complete=1, inserted_rows=?, completed_at=NOW() where id=?"; PreparedStatement preparedStatement = connection.prepareStatement(sql); preparedStatement.setInt(1, insertedRows); preparedStatement.setLong(2, ( Long ) startBootstrapRow.getData("id")); if ( preparedStatement.executeUpdate() == 0) { throw new NoSuchElementException(); } }