PreparedStatement stm = c.prepareStatement("UPDATE user_table SET name=? WHERE id=?"); stm.setString(1, "the name"); stm.setInt(2, 345); stm.executeUpdate();
/** * Delete undo log. * * @param xid the xid * @param branchId the branch id * @param conn the conn * @throws SQLException the sql exception */ public static void deleteUndoLog(String xid, long branchId, Connection conn) throws SQLException { PreparedStatement deletePST = conn.prepareStatement(DELETE_UNDO_LOG_SQL); deletePST.setLong(1, branchId); deletePST.setString(2, xid); deletePST.executeUpdate(); } }
/** * {@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); } } }
public int deleteFiredTriggers(Connection conn, String theInstanceId) throws SQLException { PreparedStatement ps = null; try { ps = conn.prepareStatement(rtp(DELETE_INSTANCES_FIRED_TRIGGERS)); ps.setString(1, theInstanceId); return ps.executeUpdate(); } finally { closeStatement(ps); } }
public static int executeUpdate(Connection conn, String sql, List<Object> parameters) throws SQLException { PreparedStatement stmt = null; int updateCount; try { stmt = conn.prepareStatement(sql); setParameters(stmt, parameters); updateCount = stmt.executeUpdate(); } finally { JdbcUtils.close(stmt); } return updateCount; }
@Test public void testJDBC() { doInHibernate( this::sessionFactory, session -> { session.doWork( connection -> { Time time = Time.valueOf( LocalTime.of( 12, 0, 0 ) ); try (PreparedStatement ps = connection.prepareStatement( "INSERT INTO Person (id, shiftStartTime) VALUES (?, ?)" )) { ps.setLong( 1, 1L ); ps.setTime( 2, time, new GregorianCalendar( TimeZone.getTimeZone( "UTC" ) ) ); ps.executeUpdate(); } try (Statement st = connection.createStatement()) { try (ResultSet rs = st.executeQuery( "SELECT shiftStartTime FROM Person WHERE id = 1" )) { while ( rs.next() ) { Time dbTime = rs.getTime( 1, new GregorianCalendar( TimeZone.getTimeZone( "UTC" ) ) ); assertEquals( time, dbTime ); } } } } ); } ); }
@Test public void whenInsertFailsUpdateSucceeds() throws Exception { when(statement.executeUpdate()).thenReturn(0); PreparedStatement updateStatement = mock(PreparedStatement.class); when(updateStatement.executeUpdate()).thenReturn(1); when(connection.prepareStatement(any())).thenReturn(statement).thenReturn(updateStatement); handler.write(region, Operation.CREATE, new Object(), value); verify(statement).executeUpdate(); verify(updateStatement).executeUpdate(); verify(statement).close(); verify(updateStatement).close(); }
public void create(Entity entity) throws SQLException { Connection connection = null; PreparedStatement statement = null; try { connection = dataSource.getConnection(); statement = connection.prepareStatement(SQL_CREATE); statement.setSomeObject(1, entity.getSomeProperty()); // ... statement.executeUpdate(); } finally { if (statement != null) try { statement.close(); } catch (SQLException logOrIgnore) {} if (connection != null) try { connection.close(); } catch (SQLException logOrIgnore) {} } }
ps = con.prepareStatement(getInsertString()); setParameterValues(ps, values, getInsertTypes()); ps.executeUpdate(); keyStmt = con.createStatement(); rs = keyStmt.executeQuery(keyQuery); if (rs.next()) { long key = rs.getLong(1); keys.put(getGeneratedKeyNames()[0], key);
/** * Test update with dynamic SQL. */ @Test public void testSqlUpdateWithArguments() throws Exception { final String sql = "UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = ? and PR = ?"; int rowsAffected = 33; given(this.preparedStatement.executeUpdate()).willReturn(rowsAffected); int actualRowsAffected = this.template.update(sql, 4, new SqlParameterValue(Types.NUMERIC, 2, Float.valueOf(1.4142f))); assertTrue("Actual rows affected is correct", actualRowsAffected == rowsAffected); verify(this.preparedStatement).setObject(1, 4); verify(this.preparedStatement).setObject(2, Float.valueOf(1.4142f), Types.NUMERIC, 2); verify(this.preparedStatement).close(); verify(this.connection).close(); }
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(); } }
public int deleteExtendedTriggerProperties(Connection conn, TriggerKey triggerKey) throws SQLException { PreparedStatement ps = null; try { ps = conn.prepareStatement(Util.rtp(DELETE_SIMPLE_PROPS_TRIGGER, tablePrefix, schedNameLiteral)); ps.setString(1, triggerKey.getName()); ps.setString(2, triggerKey.getGroup()); return ps.executeUpdate(); } finally { Util.closeStatement(ps); } }
@Test public void testUpdateIntInt() throws SQLException { given(preparedStatement.executeUpdate()).willReturn(1); given(connection.prepareStatement(UPDATE_INT_INT)).willReturn(preparedStatement); IntIntUpdater pc = new IntIntUpdater(); int rowsAffected = pc.run(1, 1); assertEquals(1, rowsAffected); verify(preparedStatement).setObject(1, 1, Types.NUMERIC); verify(preparedStatement).setObject(2, 1, Types.NUMERIC); }
@Test public void testDBTimeValueAsEpochDST() { doInHibernate( this::sessionFactory, session -> { session.doWork( connection -> { Time time = Time.valueOf( LocalTime.of( 12, 0, 0 ) ); GregorianCalendar utcCalendar = new GregorianCalendar( TimeZone.getTimeZone( "UTC" ) ); utcCalendar.setTimeInMillis( time.getTime() ); LocalTime utcLocalTime = utcCalendar.toZonedDateTime().toLocalTime(); Time utcTime = Time.valueOf( utcLocalTime ); try (PreparedStatement ps = connection.prepareStatement( "INSERT INTO Person (id, shiftStartTime) VALUES (?, ?)" )) { ps.setLong( 1, 1L ); ps.setTime( 2, time, new GregorianCalendar( TimeZone.getTimeZone( "UTC" ) ) ); ps.executeUpdate(); } try (Statement st = connection.createStatement()) { try (ResultSet rs = st.executeQuery( "SELECT shiftStartTime FROM Person WHERE id = 1" )) { while ( rs.next() ) { Time dbTime = rs.getTime( 1 ); assertEquals( utcTime, dbTime ); } } } } ); } ); }
@Test public void whenInsertFailsWithExceptionUpdateSucceeds() throws Exception { when(statement.executeUpdate()).thenThrow(SQLException.class); PreparedStatement updateStatement = mock(PreparedStatement.class); when(updateStatement.executeUpdate()).thenReturn(1); when(connection.prepareStatement(any())).thenReturn(statement).thenReturn(updateStatement); handler.write(region, Operation.CREATE, new Object(), value); verify(statement).executeUpdate(); verify(updateStatement).executeUpdate(); verify(statement).close(); verify(updateStatement).close(); }
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();
@Override public void doDeleteOldMessages(TransactionContext c) throws SQLException, IOException { PreparedStatement s = null; try { LOG.debug("Executing SQL: " + this.statements.getDeleteOldMessagesStatementWithPriority()); s = c.getExclusiveConnection().prepareStatement(this.statements.getDeleteOldMessagesStatementWithPriority()); int priority = priorityIterator++%10; s.setInt(1, priority); s.setInt(2, priority); int i = s.executeUpdate(); LOG.debug("Deleted " + i + " old message(s) at priority: " + priority); } finally { close(s); } }
static void perform(Connection cxn, long id, String filters) throws SQLException { try (PreparedStatement ps = cxn.prepareStatement("UPDATE pipelineselections SET version = ?, filters = ? WHERE id = ?")) { ps.setInt(1, SCHEMA); ps.setString(2, addStateIfNull(filters)); ps.setLong(3, id); ps.executeUpdate(); } }
public int deleteExtendedTriggerProperties(Connection conn, TriggerKey triggerKey) throws SQLException { PreparedStatement ps = null; try { ps = conn.prepareStatement(Util.rtp(DELETE_CRON_TRIGGER, tablePrefix, schedNameLiteral)); ps.setString(1, triggerKey.getName()); ps.setString(2, triggerKey.getGroup()); return ps.executeUpdate(); } finally { Util.closeStatement(ps); } }
@Test public void testUpdateInt() throws SQLException { given(preparedStatement.executeUpdate()).willReturn(1); given(connection.prepareStatement(UPDATE_INT)).willReturn(preparedStatement); IntUpdater pc = new IntUpdater(); int rowsAffected = pc.run(1); assertEquals(1, rowsAffected); verify(preparedStatement).setObject(1, 1, Types.NUMERIC); }