Example usage for java.sql PreparedStatement getWarnings

List of usage examples for java.sql PreparedStatement getWarnings

Introduction

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

Prototype

SQLWarning getWarnings() throws SQLException;

Source Link

Document

Retrieves the first warning reported by calls on this Statement object.

Usage

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

public void testQueryWithArgsAndTypesAndRowMapper() throws SQLException {
    MockControl psControl = MockControl.createControl(PreparedStatement.class);
    final PreparedStatement ps = (PreparedStatement) psControl.getMock();

    con.prepareStatement("some SQL");
    conControl.setReturnValue(ps, 1);/*  w ww. j a  va 2s  .c om*/
    ps.setString(1, "test1");
    ps.setString(2, "test2");
    psControl.setVoidCallable(1);
    ps.executeQuery();
    psControl.setReturnValue(rs, 1);
    if (debugEnabled) {
        ps.getWarnings();
        psControl.setReturnValue(null, 1);
    }
    ps.close();
    psControl.setVoidCallable(1);

    conControl.replay();
    psControl.replay();

    result = jdbcTemplate.query("some SQL", new Object[] { "test1", "test2" },
            new int[] { Types.VARCHAR, Types.VARCHAR }, new TestRowMapper());

    psControl.verify();
    verify();
}

From source file:org.springframework.jdbc.object.BatchSqlUpdateTests.java

private void doTestBatchUpdate(boolean flushThroughBatchSize) throws Exception {
    final String sql = "UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = ?";
    final int[] ids = new int[] { 100, 200 };
    final int[] rowsAffected = new int[] { 1, 2 };

    MockControl ctrlPreparedStatement = MockControl.createControl(PreparedStatement.class);
    PreparedStatement mockPreparedStatement = (PreparedStatement) ctrlPreparedStatement.getMock();
    mockPreparedStatement.getConnection();
    ctrlPreparedStatement.setReturnValue(mockConnection);
    mockPreparedStatement.setObject(1, new Integer(ids[0]), Types.INTEGER);
    ctrlPreparedStatement.setVoidCallable();
    mockPreparedStatement.addBatch();/*  ww  w  .  ja  v  a 2  s .  co  m*/
    ctrlPreparedStatement.setVoidCallable();
    mockPreparedStatement.setObject(1, new Integer(ids[1]), Types.INTEGER);
    ctrlPreparedStatement.setVoidCallable();
    mockPreparedStatement.addBatch();
    ctrlPreparedStatement.setVoidCallable();
    mockPreparedStatement.executeBatch();
    ctrlPreparedStatement.setReturnValue(rowsAffected);
    if (debugEnabled) {
        mockPreparedStatement.getWarnings();
        ctrlPreparedStatement.setReturnValue(null);
    }
    mockPreparedStatement.close();
    ctrlPreparedStatement.setVoidCallable();

    MockControl ctrlDatabaseMetaData = MockControl.createControl(DatabaseMetaData.class);
    DatabaseMetaData mockDatabaseMetaData = (DatabaseMetaData) ctrlDatabaseMetaData.getMock();
    mockDatabaseMetaData.supportsBatchUpdates();
    ctrlDatabaseMetaData.setReturnValue(true);

    mockConnection.prepareStatement(sql);
    ctrlConnection.setReturnValue(mockPreparedStatement);
    mockConnection.getMetaData();
    ctrlConnection.setReturnValue(mockDatabaseMetaData, 1);

    ctrlPreparedStatement.replay();
    ctrlDatabaseMetaData.replay();
    replay();

    BatchSqlUpdate update = new BatchSqlUpdate(mockDataSource, sql);
    update.declareParameter(new SqlParameter(Types.INTEGER));
    if (flushThroughBatchSize) {
        update.setBatchSize(2);
    }

    update.update(ids[0]);
    update.update(ids[1]);

    if (flushThroughBatchSize) {
        assertEquals(0, update.getQueueCount());
        assertEquals(2, update.getRowsAffected().length);
    } else {
        assertEquals(2, update.getQueueCount());
        assertEquals(0, update.getRowsAffected().length);
    }

    int[] actualRowsAffected = update.flush();
    assertEquals(0, update.getQueueCount());

    if (flushThroughBatchSize) {
        assertTrue("flush did not execute updates", actualRowsAffected.length == 0);
    } else {
        assertTrue("executed 2 updates", actualRowsAffected.length == 2);
        assertEquals(rowsAffected[0], actualRowsAffected[0]);
        assertEquals(rowsAffected[1], actualRowsAffected[1]);
    }

    actualRowsAffected = update.getRowsAffected();
    assertTrue("executed 2 updates", actualRowsAffected.length == 2);
    assertEquals(rowsAffected[0], actualRowsAffected[0]);
    assertEquals(rowsAffected[1], actualRowsAffected[1]);

    update.reset();
    assertEquals(0, update.getRowsAffected().length);

    ctrlPreparedStatement.verify();
    ctrlDatabaseMetaData.verify();
}

From source file:org.springframework.jdbc.object.SqlQueryTests.java

public void testFindCustomerMixed() throws SQLException {
    MockControl ctrlResultSet2;/*ww w  .  jav  a 2  s  . com*/
    ResultSet mockResultSet2;
    MockControl ctrlPreparedStatement2;
    PreparedStatement mockPreparedStatement2;

    mockResultSet.next();
    ctrlResultSet.setReturnValue(true);
    mockResultSet.getInt("id");
    ctrlResultSet.setReturnValue(1);
    mockResultSet.getString("forename");
    ctrlResultSet.setReturnValue("rod");
    mockResultSet.next();
    ctrlResultSet.setReturnValue(false);
    mockResultSet.close();
    ctrlResultSet.setVoidCallable();

    mockPreparedStatement.setObject(1, new Integer(1), Types.INTEGER);
    ctrlPreparedStatement.setVoidCallable();
    mockPreparedStatement.setString(2, "rod");
    ctrlPreparedStatement.setVoidCallable();
    mockPreparedStatement.executeQuery();
    ctrlPreparedStatement.setReturnValue(mockResultSet);
    if (debugEnabled) {
        mockPreparedStatement.getWarnings();
        ctrlPreparedStatement.setReturnValue(null);
    }
    mockPreparedStatement.close();
    ctrlPreparedStatement.setVoidCallable();

    ctrlResultSet2 = MockControl.createControl(ResultSet.class);
    mockResultSet2 = (ResultSet) ctrlResultSet2.getMock();
    mockResultSet2.next();
    ctrlResultSet2.setReturnValue(false);
    mockResultSet2.close();
    ctrlResultSet2.setVoidCallable();

    ctrlPreparedStatement2 = MockControl.createControl(PreparedStatement.class);
    mockPreparedStatement2 = (PreparedStatement) ctrlPreparedStatement2.getMock();
    mockPreparedStatement2.setObject(1, new Integer(1), Types.INTEGER);
    ctrlPreparedStatement2.setVoidCallable();
    mockPreparedStatement2.setString(2, "Roger");
    ctrlPreparedStatement2.setVoidCallable();
    mockPreparedStatement2.executeQuery();
    ctrlPreparedStatement2.setReturnValue(mockResultSet2);
    if (debugEnabled) {
        mockPreparedStatement2.getWarnings();
        ctrlPreparedStatement2.setReturnValue(null);
    }
    mockPreparedStatement2.close();
    ctrlPreparedStatement2.setVoidCallable();

    mockConnection.prepareStatement(SELECT_ID_WHERE);
    ctrlConnection.setReturnValue(mockPreparedStatement);
    mockConnection.prepareStatement(SELECT_ID_WHERE);
    ctrlConnection.setReturnValue(mockPreparedStatement2);

    ctrlResultSet2.replay();
    ctrlPreparedStatement2.replay();
    replay();

    class CustomerQuery extends MappingSqlQuery {

        public CustomerQuery(DataSource ds) {
            super(ds, SELECT_ID_WHERE);
            declareParameter(new SqlParameter(COLUMN_NAMES[0], COLUMN_TYPES[0]));
            declareParameter(new SqlParameter(COLUMN_NAMES[1], COLUMN_TYPES[1]));
            compile();
        }

        protected Object mapRow(ResultSet rs, int rownum) throws SQLException {
            Customer cust = new Customer();
            cust.setId(rs.getInt(COLUMN_NAMES[0]));
            cust.setForename(rs.getString(COLUMN_NAMES[1]));
            return cust;
        }

        public Customer findCustomer(int id, String name) {
            return (Customer) findObject(new Object[] { new Integer(id), name });
        }
    }

    CustomerQuery query = new CustomerQuery(mockDataSource);

    Customer cust1 = query.findCustomer(1, "rod");
    assertTrue("Found customer", cust1 != null);
    assertTrue("Customer id was assigned correctly", cust1.getId() == 1);

    Customer cust2 = query.findCustomer(1, "Roger");
    assertTrue("No customer found", cust2 == null);
}