Example usage for org.springframework.jdbc.core JdbcTemplate setDatabaseProductName

List of usage examples for org.springframework.jdbc.core JdbcTemplate setDatabaseProductName

Introduction

In this page you can find the example usage for org.springframework.jdbc.core JdbcTemplate setDatabaseProductName.

Prototype

public void setDatabaseProductName(String dbName) 

Source Link

Document

Specify the database product name for the DataSource that this accessor uses.

Usage

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();// www  .  j av  a2s .  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();
}