Skip to content

Latest commit

 

History

History
100 lines (81 loc) · 1.94 KB

README.md

File metadata and controls

100 lines (81 loc) · 1.94 KB

Spring JDBC

  • 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

Why

  • Reduce complexity
  • Design
    • Testability
  • Portability
  • Business Focus
    • No configuration

Example

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);
}

My SQL & phpMyAdmin

To download and run a docker container instance of My SQL run MySQL.sh.

Additionally, an instance of phpMyAdmin will be available on:

http://localhost:8081

username: root
password: pass

Dependencies

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>

Example Contents

  • JdbcTemplate
  • NamedJdbcTemplate & named parameters
  • ExceptionHandler
  • Transactions

Some Interesting Articles

JDBC or Hibernate with/without Spring Data

Difference between Hibernate & Data JPA