- Framework
- Less error prone
- Pattern based
- No boilerplate
- Injected resources
- Uses Spring DI libraries
- ORM (like Hibernate and JPA, but simpler)
- Works well with existing databases
- Reduce complexity
- Design
- Testability
- Portability
- Business Focus
- No configuration
Regular JDBC:
public Car getById(String id) {
Connection con = null;
PreparedStatement stmt = null;
Resultset = null;
try {
String sql = "SELECT * FROM car WHERE id = ?";
con = DriverManager.getConnection("localhost:3306/cars);
stmt = con.prepareStatement(sql);
stmt.setString(1, id);
rs = stmt.executeQuery();
if (rs.next()) {
Car car = new Car();
car.setMake(rs.getString(1));
return car;
} else {
return null;
}
} catch (SQLException e) {
e.printStackTRrace();
} finally {
try {
if (rt != null && !rs.isClosed()) {
rs.close();
}
} catch (Exception e) {}
}
return null;
}
Spring JDBC:
public Car findCar(Strin id) {
return jdbcTemplate.queryForObject(sql, Car.class, id);
}
To download and run a docker container instance of My SQL run MySQL.sh.
Additionally, an instance of phpMyAdmin will be available on:
username: root
password: pass
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
- JdbcTemplate
- NamedJdbcTemplate & named parameters
- ExceptionHandler
- Transactions