Spring JDBC Tips

Version: 4.3.1

SimpleJdbcCall

For stored procedure, function

Cannot create one SimpleJdbcCall for many stored procedures/functions (once initialized, later change won’t work. For example, to call another stored procedure… )

@Autowired
public DataSource dataSource;

public Map<String, Object> callStoredProcedure(String procedureName, final Map<String, Object> params) {
 SimpleJdbcCall jdbcCall = new SimpleJdbcCall(dataSource).withProcedureName(procedureName);
 return jdbcCall.execute(params);
}

JdbcTemplate

For query, insert, delete, stored procedure, function…

@Resource(name = "jdbcTemplate")
public JdbcTemplate jdbcTemplate;

public List<MyObject> find(String id) {
 List<MyObject> list = jdbcTemplate.query(FIND_BY_ID_SQL,
  new RowMapper<MyObject>() {
   @Override
   public MyObject mapRow(ResultSet rs, int rowNum) throws SQLException {
    MyObject myObject = new MyObject();
    myObject.setName(rs.getString("NAME"));
    return myObject;
   }
  }, id);
...
}

http://docs.spring.io/spring/docs/current/spring-framework-reference/html/jdbc.html

https://lalitjc.wordpress.com/2013/07/02/different-ways-of-calling-stored-procedure-using-spring/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s