minor cleanup formatting work

This commit is contained in:
eugenp 2015-01-24 15:50:51 +02:00
parent 07f20a87bf
commit 4ad6cc93f2
6 changed files with 134 additions and 165 deletions

View File

@ -6,16 +6,12 @@ import org.springframework.dao.DataAccessException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator;
public class CustomSQLErrorCodeTranslator extends
SQLErrorCodeSQLExceptionTranslator {
public class CustomSQLErrorCodeTranslator extends SQLErrorCodeSQLExceptionTranslator {
@Override
protected DataAccessException customTranslate(final String task,
final String sql, final SQLException sqlException) {
protected DataAccessException customTranslate(final String task, final String sql, final SQLException sqlException) {
if (sqlException.getErrorCode() == -104) {
return new DuplicateKeyException(
"Custome Exception translator - Integrity contraint voilation.",
sqlException);
return new DuplicateKeyException("Custome Exception translator - Integrity contraint voilation.", sqlException);
}
return null;
}

View File

@ -35,24 +35,20 @@ public class EmployeeDAO {
jdbcTemplate.setExceptionTranslator(customSQLErrorCodeTranslator);
namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
simpleJdbcInsert = new SimpleJdbcInsert(dataSource)
.withTableName("EMPLOYEE");
simpleJdbcInsert = new SimpleJdbcInsert(dataSource).withTableName("EMPLOYEE");
}
public int getCountOfEmployees() {
return jdbcTemplate.queryForObject("SELECT COUNT(*) FROM EMPLOYEE",
Integer.class);
return jdbcTemplate.queryForObject("SELECT COUNT(*) FROM EMPLOYEE", Integer.class);
}
public List<Employee> getAllEmployees() {
return jdbcTemplate.query("SELECT * FROM EMPLOYEE",
new EmployeeRowMapper());
return jdbcTemplate.query("SELECT * FROM EMPLOYEE", new EmployeeRowMapper());
}
public int addEmplyee(final int id) {
return jdbcTemplate.update("INSERT INTO EMPLOYEE VALUES (?, ?, ?, ?)",
id, "Bill", "Gates", "USA");
return jdbcTemplate.update("INSERT INTO EMPLOYEE VALUES (?, ?, ?, ?)", id, "Bill", "Gates", "USA");
}
public int addEmplyeeUsingSimpelJdbcInsert(final Employee emp) {
@ -67,22 +63,17 @@ public class EmployeeDAO {
public Employee getEmployee(final int id) {
final String query = "SELECT * FROM EMPLOYEE WHERE ID = ?";
return jdbcTemplate.queryForObject(query, new Object[] { id },
new EmployeeRowMapper());
return jdbcTemplate.queryForObject(query, new Object[] { id }, new EmployeeRowMapper());
}
public void addEmplyeeUsingExecuteMethod() {
jdbcTemplate
.execute("INSERT INTO EMPLOYEE VALUES (6, 'Bill', 'Gates', 'USA')");
jdbcTemplate.execute("INSERT INTO EMPLOYEE VALUES (6, 'Bill', 'Gates', 'USA')");
}
public String getEmployeeUsingMapSqlParameterSource() {
final SqlParameterSource namedParameters = new MapSqlParameterSource()
.addValue("id", 1);
final SqlParameterSource namedParameters = new MapSqlParameterSource().addValue("id", 1);
return namedParameterJdbcTemplate.queryForObject(
"SELECT FIRST_NAME FROM EMPLOYEE WHERE ID = :id",
namedParameters, String.class);
return namedParameterJdbcTemplate.queryForObject("SELECT FIRST_NAME FROM EMPLOYEE WHERE ID = :id", namedParameters, String.class);
}
public int getEmployeeUsingBeanPropertySqlParameterSource() {
@ -91,21 +82,16 @@ public class EmployeeDAO {
final String SELECT_BY_ID = "SELECT COUNT(*) FROM EMPLOYEE WHERE FIRST_NAME = :firstName";
final SqlParameterSource namedParameters = new BeanPropertySqlParameterSource(
employee);
final SqlParameterSource namedParameters = new BeanPropertySqlParameterSource(employee);
return namedParameterJdbcTemplate.queryForObject(SELECT_BY_ID,
namedParameters, Integer.class);
return namedParameterJdbcTemplate.queryForObject(SELECT_BY_ID, namedParameters, Integer.class);
}
public int[] batchUpdateUsingJDBCTemplate(final List<Employee> employees) {
return jdbcTemplate.batchUpdate(
"INSERT INTO EMPLOYEE VALUES (?, ?, ?, ?)",
new BatchPreparedStatementSetter() {
return jdbcTemplate.batchUpdate("INSERT INTO EMPLOYEE VALUES (?, ?, ?, ?)", new BatchPreparedStatementSetter() {
@Override
public void setValues(final PreparedStatement ps,
final int i) throws SQLException {
public void setValues(final PreparedStatement ps, final int i) throws SQLException {
ps.setInt(1, employees.get(i).getId());
ps.setString(2, employees.get(i).getFirstName());
ps.setString(3, employees.get(i).getLastName());
@ -119,14 +105,9 @@ public class EmployeeDAO {
});
}
public int[] batchUpdateUsingNamedParameterJDBCTemplate(
final List<Employee> employees) {
final SqlParameterSource[] batch = SqlParameterSourceUtils
.createBatch(employees.toArray());
final int[] updateCounts = namedParameterJdbcTemplate
.batchUpdate(
"INSERT INTO EMPLOYEE VALUES (:id, :firstName, :lastName, :address)",
batch);
public int[] batchUpdateUsingNamedParameterJDBCTemplate(final List<Employee> employees) {
final SqlParameterSource[] batch = SqlParameterSourceUtils.createBatch(employees.toArray());
final int[] updateCounts = namedParameterJdbcTemplate.batchUpdate("INSERT INTO EMPLOYEE VALUES (:id, :firstName, :lastName, :address)", batch);
return updateCounts;
}
}

View File

@ -8,8 +8,7 @@ import org.springframework.jdbc.core.RowMapper;
public class EmployeeRowMapper implements RowMapper<Employee> {
@Override
public Employee mapRow(final ResultSet rs, final int rowNum)
throws SQLException {
public Employee mapRow(final ResultSet rs, final int rowNum) throws SQLException {
final Employee employee = new Employee();
employee.setId(rs.getInt("ID"));

View File

@ -15,9 +15,7 @@ public class SpringJdbcConfig {
@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.HSQL)
.addScript("classpath:jdbc/schema.sql")
.addScript("classpath:jdbc/test-data.sql").build();
return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.HSQL).addScript("classpath:jdbc/schema.sql").addScript("classpath:jdbc/test-data.sql").build();
}
// @Bean

View File

@ -53,14 +53,12 @@ public class EmployeeDAOTest {
@Test
public void testMapSqlParameterSource() {
Assert.assertEquals("James",
employeeDao.getEmployeeUsingMapSqlParameterSource());
Assert.assertEquals("James", employeeDao.getEmployeeUsingMapSqlParameterSource());
}
@Test
public void testBeanPropertySqlParameterSource() {
Assert.assertEquals(1,
employeeDao.getEmployeeUsingBeanPropertySqlParameterSource());
Assert.assertEquals(1, employeeDao.getEmployeeUsingBeanPropertySqlParameterSource());
}
@Test
@ -71,10 +69,7 @@ public class EmployeeDAOTest {
employeeDao.addEmplyee(7);
} catch (final DuplicateKeyException e) {
System.out.println(e.getMessage());
Assert.assertTrue(e
.getMessage()
.contains(
"Custome Exception translator - Integrity contraint voilation."));
Assert.assertTrue(e.getMessage().contains("Custome Exception translator - Integrity contraint voilation."));
}
}