Spring & JDBC
The Spring Framework makes JDBC development easier by using IoC to eliminate low-level code. If you don't use an ORM framework, this API is highly recommended. Now let's see a comparison sample between pure JDBC and JDBC with Spring JdbcTemplate class:
Example 1: Creating a list of objects with pure JDBC
-
PreparedStatement ps = connection.prepareStatement("Select name, email from t_users where status = ?");
-
-
List<User> users = new ArrayList<User>();
-
-
while (rs.next()) {
-
User user = new User();
-
user.setName(rs.getString("name"));
-
user.setEmail(rs.getString("email"));
-
users.add(user);
-
}
-
-
return users;
Besides, you have to control connections opening and closing.
Example 2: Creating a list of objects with Spring and JDBC:
We have to use the JdbcTemplate class that can be instantied by injecting your datasource (new JdbcTemplate(dataSource)). Then you should create a new class that implements the RowMapper interface for the objects creation. After that, you can get the list of objects with just one line of code:
RowMapper sample:
-
@Override
-
User user = new User();
-
user.setName(rs.getString("name"));
-
user.setEmail(rs.getString("email"));
-
-
return user;
-
}
-
}
Complete reference: http://static.springframework.org/spring/docs/2.0.x/reference/jdbc.html
Um comentário para “Spring & JDBC”
Parabéns pelo excelente blog!! Aproveitando a deixa, se vc tiver qualquer coisa pra vender ou estiver procurando carros, imóveis, etc, acesse o Noticie Classificados, abraço!

