Example usage for org.springframework.jdbc BadSqlGrammarException getCause

List of usage examples for org.springframework.jdbc BadSqlGrammarException getCause

Introduction

In this page you can find the example usage for org.springframework.jdbc BadSqlGrammarException getCause.

Prototype

public synchronized Throwable getCause() 

Source Link

Document

Returns the cause of this throwable or null if the cause is nonexistent or unknown.

Usage

From source file:wiki.link.LinkResource.java

public static void insertAll(List<Link> links, DbConnector dbc) {
    BatchPreparedStatementSetter bpss = new BatchPreparedStatementSetter() {
        @Override//from w  w w .  ja v a 2 s  . c  om
        public void setValues(PreparedStatement preparedStatement, int i) throws SQLException {
            preparedStatement.setLong(1, links.get(i).from);
            preparedStatement.setLong(2, links.get(i).to);
        }

        @Override
        public int getBatchSize() {
            return links.size();
        }
    };

    try {
        dbc.jdbcTemplate.batchUpdate("INSERT INTO links(fromPage, toPage) values(?, ?)", bpss);
    } catch (BadSqlGrammarException e) {
        e.printStackTrace();
        BatchUpdateException bue = (BatchUpdateException) e.getCause();
        System.out.println(bue.getNextException());
        System.exit(1);
    }
}

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

public void testSQLErrorCodeTranslation() throws Exception {
    final SQLException sex = new SQLException("I have a known problem", "99999", 1054);
    final String sql = "SELECT ID FROM CUSTOMER";

    MockControl ctrlResultSet = MockControl.createControl(ResultSet.class);
    ResultSet mockResultSet = (ResultSet) ctrlResultSet.getMock();
    mockResultSet.next();//w  w  w  .  jav  a  2  s  . co  m
    ctrlResultSet.setReturnValue(true);
    mockResultSet.close();
    ctrlResultSet.setVoidCallable();

    MockControl ctrlStatement = MockControl.createControl(PreparedStatement.class);
    PreparedStatement mockStatement = (PreparedStatement) ctrlStatement.getMock();
    mockStatement.executeQuery(sql);
    ctrlStatement.setReturnValue(mockResultSet);
    mockStatement.close();
    ctrlStatement.setVoidCallable();

    MockControl ctrlDatabaseMetaData = MockControl.createControl(DatabaseMetaData.class);
    DatabaseMetaData mockDatabaseMetaData = (DatabaseMetaData) ctrlDatabaseMetaData.getMock();
    mockDatabaseMetaData.getDatabaseProductName();
    ctrlDatabaseMetaData.setReturnValue("MySQL");

    mockConnection.createStatement();
    ctrlConnection.setReturnValue(mockStatement);
    mockConnection.getMetaData();
    ctrlConnection.setReturnValue(mockDatabaseMetaData);

    ctrlResultSet.replay();
    ctrlStatement.replay();
    ctrlDatabaseMetaData.replay();
    replay();

    JdbcTemplate template = new JdbcTemplate(mockDataSource);
    try {
        template.query(sql, new RowCallbackHandler() {
            public void processRow(ResultSet rs) throws SQLException {
                throw sex;
            }
        });
        fail("Should have thrown BadSqlGrammarException");
    } catch (BadSqlGrammarException ex) {
        // expected
        assertTrue("Wanted same exception back, not " + ex, sex == ex.getCause());
    }

    ctrlResultSet.verify();
    ctrlStatement.verify();
    ctrlDatabaseMetaData.verify();
}

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

public void testSQLErrorCodeTranslationWithSpecifiedDbName() throws Exception {
    final SQLException sex = new SQLException("I have a known problem", "99999", 1054);
    final String sql = "SELECT ID FROM CUSTOMER";

    MockControl ctrlResultSet = MockControl.createControl(ResultSet.class);
    ResultSet mockResultSet = (ResultSet) ctrlResultSet.getMock();
    mockResultSet.next();/*from w w  w. j  av  a2 s  .  co  m*/
    ctrlResultSet.setReturnValue(true);
    mockResultSet.close();
    ctrlResultSet.setVoidCallable();

    MockControl ctrlStatement = MockControl.createControl(PreparedStatement.class);
    PreparedStatement mockStatement = (PreparedStatement) ctrlStatement.getMock();
    mockStatement.executeQuery(sql);
    ctrlStatement.setReturnValue(mockResultSet);
    mockStatement.close();
    ctrlStatement.setVoidCallable();

    mockConnection.createStatement();
    ctrlConnection.setReturnValue(mockStatement);

    ctrlResultSet.replay();
    ctrlStatement.replay();
    replay();

    JdbcTemplate template = new JdbcTemplate();
    template.setDataSource(mockDataSource);
    template.setDatabaseProductName("MySQL");
    template.afterPropertiesSet();
    try {
        template.query(sql, new RowCallbackHandler() {
            public void processRow(ResultSet rs) throws SQLException {
                throw sex;
            }
        });
        fail("Should have thrown BadSqlGrammarException");
    } catch (BadSqlGrammarException ex) {
        // expected
        assertTrue("Wanted same exception back, not " + ex, sex == ex.getCause());
    }

    ctrlResultSet.verify();
    ctrlStatement.verify();
}

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

/**
 * Test that we see an SQLException translated using Error Code.
 * If we provide the SQLExceptionTranslator, we shouldn't use a connection
 * to get the metadata/*from  w ww .  ja  v  a 2s.  c  o m*/
 */
public void testUseCustomSQLErrorCodeTranslator() throws Exception {
    // Bad SQL state
    final SQLException sex = new SQLException("I have a known problem", "07000", 1054);
    final String sql = "SELECT ID FROM CUSTOMER";

    MockControl ctrlResultSet = MockControl.createControl(ResultSet.class);
    ResultSet mockResultSet = (ResultSet) ctrlResultSet.getMock();
    mockResultSet.next();
    ctrlResultSet.setReturnValue(true);
    mockResultSet.close();
    ctrlResultSet.setVoidCallable();

    MockControl ctrlStatement = MockControl.createControl(PreparedStatement.class);
    PreparedStatement mockStatement = (PreparedStatement) ctrlStatement.getMock();
    mockStatement.executeQuery(sql);
    ctrlStatement.setReturnValue(mockResultSet);
    mockStatement.close();
    ctrlStatement.setVoidCallable();

    mockConnection.createStatement();
    ctrlConnection.setReturnValue(mockStatement);

    // Change behaviour in setUp() because we only expect one call to getConnection():
    // none is necessary to get metadata for exception translator
    ctrlConnection = MockControl.createControl(Connection.class);
    mockConnection = (Connection) ctrlConnection.getMock();
    mockConnection.createStatement();
    ctrlConnection.setReturnValue(mockStatement, 1);
    mockConnection.close();
    ctrlConnection.setVoidCallable(1);
    ctrlConnection.replay();

    ctrlDataSource = MockControl.createControl(DataSource.class);
    mockDataSource = (DataSource) ctrlDataSource.getMock();
    mockDataSource.getConnection();
    ctrlDataSource.setReturnValue(mockConnection, 1);
    ctrlDataSource.replay();
    ///// end changed behaviour

    ctrlResultSet.replay();
    ctrlStatement.replay();

    JdbcTemplate template = new JdbcTemplate();
    template.setDataSource(mockDataSource);
    // Set custom exception translator
    template.setExceptionTranslator(new SQLStateSQLExceptionTranslator());
    template.afterPropertiesSet();
    try {
        template.query(sql, new RowCallbackHandler() {
            public void processRow(ResultSet rs) throws SQLException {
                throw sex;
            }
        });
        fail("Should have thrown exception");
    } catch (BadSqlGrammarException ex) {
        assertTrue("Wanted same exception back, not " + ex, sex == ex.getCause());
    }

    ctrlResultSet.verify();
    ctrlStatement.verify();

    // We didn't call superclass replay() so we need to check these ourselves
    ctrlDataSource.verify();
    ctrlConnection.verify();
}