|
| 1 | +package org.baeldung.jdbc; |
| 2 | + |
| 3 | +import java.sql.PreparedStatement; |
| 4 | +import java.sql.SQLException; |
| 5 | +import java.util.HashMap; |
| 6 | +import java.util.List; |
| 7 | +import java.util.Map; |
| 8 | + |
| 9 | +import javax.sql.DataSource; |
| 10 | + |
| 11 | +import org.springframework.beans.factory.annotation.Autowired; |
| 12 | +import org.springframework.jdbc.core.BatchPreparedStatementSetter; |
| 13 | +import org.springframework.jdbc.core.JdbcTemplate; |
| 14 | +import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource; |
| 15 | +import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; |
| 16 | +import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; |
| 17 | +import org.springframework.jdbc.core.namedparam.SqlParameterSource; |
| 18 | +import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils; |
| 19 | +import org.springframework.jdbc.core.simple.SimpleJdbcInsert; |
| 20 | +import org.springframework.stereotype.Repository; |
| 21 | + |
| 22 | +@Repository |
| 23 | +public class EmployeeDAO { |
| 24 | + |
| 25 | + private JdbcTemplate jdbcTemplate; |
| 26 | + |
| 27 | + private NamedParameterJdbcTemplate namedParameterJdbcTemplate; |
| 28 | + |
| 29 | + private SimpleJdbcInsert simpleJdbcInsert; |
| 30 | + |
| 31 | + @Autowired |
| 32 | + public void setDataSource(final DataSource dataSource) { |
| 33 | + jdbcTemplate = new JdbcTemplate(dataSource); |
| 34 | + final CustomSQLErrorCodeTranslator customSQLErrorCodeTranslator = new CustomSQLErrorCodeTranslator(); |
| 35 | + jdbcTemplate.setExceptionTranslator(customSQLErrorCodeTranslator); |
| 36 | + |
| 37 | + namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource); |
| 38 | + simpleJdbcInsert = new SimpleJdbcInsert(dataSource) |
| 39 | + .withTableName("EMPLOYEE"); |
| 40 | + |
| 41 | + } |
| 42 | + |
| 43 | + public int getCountOfEmployees() { |
| 44 | + return jdbcTemplate.queryForObject("SELECT COUNT(*) FROM EMPLOYEE", |
| 45 | + Integer.class); |
| 46 | + } |
| 47 | + |
| 48 | + public List<Employee> getAllEmployees() { |
| 49 | + return jdbcTemplate.query("SELECT * FROM EMPLOYEE", |
| 50 | + new EmployeeRowMapper()); |
| 51 | + } |
| 52 | + |
| 53 | + public int addEmplyee(final int id) { |
| 54 | + return jdbcTemplate.update("INSERT INTO EMPLOYEE VALUES (?, ?, ?, ?)", |
| 55 | + id, "Bill", "Gates", "USA"); |
| 56 | + } |
| 57 | + |
| 58 | + public int addEmplyeeUsingSimpelJdbcInsert(final Employee emp) { |
| 59 | + final Map<String, Object> parameters = new HashMap<String, Object>(); |
| 60 | + parameters.put("ID", emp.getId()); |
| 61 | + parameters.put("FIRST_NAME", emp.getFirstName()); |
| 62 | + parameters.put("LAST_NAME", emp.getLastName()); |
| 63 | + parameters.put("ADDRESS", emp.getAddress()); |
| 64 | + |
| 65 | + return simpleJdbcInsert.execute(parameters); |
| 66 | + } |
| 67 | + |
| 68 | + public Employee getEmployee(final int id) { |
| 69 | + final String query = "SELECT * FROM EMPLOYEE WHERE ID = ?"; |
| 70 | + return jdbcTemplate.queryForObject(query, new Object[] { id }, |
| 71 | + new EmployeeRowMapper()); |
| 72 | + } |
| 73 | + |
| 74 | + public void addEmplyeeUsingExecuteMethod() { |
| 75 | + jdbcTemplate |
| 76 | + .execute("INSERT INTO EMPLOYEE VALUES (6, 'Bill', 'Gates', 'USA')"); |
| 77 | + } |
| 78 | + |
| 79 | + public String getEmployeeUsingMapSqlParameterSource() { |
| 80 | + final SqlParameterSource namedParameters = new MapSqlParameterSource() |
| 81 | + .addValue("id", 1); |
| 82 | + |
| 83 | + return namedParameterJdbcTemplate.queryForObject( |
| 84 | + "SELECT FIRST_NAME FROM EMPLOYEE WHERE ID = :id", |
| 85 | + namedParameters, String.class); |
| 86 | + } |
| 87 | + |
| 88 | + public int getEmployeeUsingBeanPropertySqlParameterSource() { |
| 89 | + final Employee employee = new Employee(); |
| 90 | + employee.setFirstName("James"); |
| 91 | + |
| 92 | + final String SELECT_BY_ID = "SELECT COUNT(*) FROM EMPLOYEE WHERE FIRST_NAME = :firstName"; |
| 93 | + |
| 94 | + final SqlParameterSource namedParameters = new BeanPropertySqlParameterSource( |
| 95 | + employee); |
| 96 | + |
| 97 | + return namedParameterJdbcTemplate.queryForObject(SELECT_BY_ID, |
| 98 | + namedParameters, Integer.class); |
| 99 | + } |
| 100 | + |
| 101 | + public int[] batchUpdateUsingJDBCTemplate(final List<Employee> employees) { |
| 102 | + return jdbcTemplate.batchUpdate( |
| 103 | + "INSERT INTO EMPLOYEE VALUES (?, ?, ?, ?)", |
| 104 | + new BatchPreparedStatementSetter() { |
| 105 | + |
| 106 | + @Override |
| 107 | + public void setValues(final PreparedStatement ps, |
| 108 | + final int i) throws SQLException { |
| 109 | + ps.setInt(1, employees.get(i).getId()); |
| 110 | + ps.setString(2, employees.get(i).getFirstName()); |
| 111 | + ps.setString(3, employees.get(i).getLastName()); |
| 112 | + ps.setString(4, employees.get(i).getAddress()); |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public int getBatchSize() { |
| 117 | + return 3; |
| 118 | + } |
| 119 | + }); |
| 120 | + } |
| 121 | + |
| 122 | + public int[] batchUpdateUsingNamedParameterJDBCTemplate( |
| 123 | + final List<Employee> employees) { |
| 124 | + final SqlParameterSource[] batch = SqlParameterSourceUtils |
| 125 | + .createBatch(employees.toArray()); |
| 126 | + final int[] updateCounts = namedParameterJdbcTemplate |
| 127 | + .batchUpdate( |
| 128 | + "INSERT INTO EMPLOYEE VALUES (:id, :firstName, :lastName, :address)", |
| 129 | + batch); |
| 130 | + return updateCounts; |
| 131 | + } |
| 132 | +} |
0 commit comments