26-JDBC Flashcards
What is the basic exception if there’s anything wrong with JDBC?
SQLException
Arguments in JdbcTemplate.query to query and map row to the entity?
JdbcTemplate.query(String, RowMapper)
T RowMapper.mapRow(ResultSet, int row) throws SQL Exception {}
How to create JdbcTemplate?
new JdbcTemplate(DataSource)
Can JdbcTemplate be shared among threads?
Yes, it’s thread-safe
Using JdbcTemplate to count the number of users?
jdbcTemplate.queryForObject(“…”, Integer.class)
Using JdbcTemplate to update age by user-id
jdbcTemplate.update(“UPDATE … age = ? WHERE id = ?”, age, id)
JdbcTemplate.queryForList vs. JdbcTemplate.queryForMap
queryForMap: expecting a single row
queryForList: expecting multiple rows, each is a Map
What exception does the RowMapper.mapRow for JdbcTemplate.query returns?
SQLException
With JdbcTemplate, how to handle the result set manually?
jdbcTemplate.query(String, ResultSetExtractor)
ResultSetExtractor.extracData(ResultSet) throws SQLException, DataAccessException
JdbcTemplate: RowCallbackHandler vs. RowMapper
RowCallbackHandler.processRow(ResultSet) won’t return record (i.e. return void)
RowMapper.mapRow(ResultSet, row) returns record
What kind of exception does Spring throw?
Spring always throws Runtime (unchecked) Exceptions
What exception class does Spring create to replace SQLException?
DataAccessException
+ Hides whether you are using JPA, Hibernate, JDBC …
+ Consistent across all supported Data Access technologies