This set of Java Spring Multiple Choice Questions & Answers (MCQs) focuses on “Using the Simple JDBC Template and Handling Exceptions”.
1. JdbcTemplate that takes advantage of Java 1.5 features such as autoboxing, generics, and variable-length arguments to simplify its usage.
a) org.springframework.jdbc.core.JdbcTemplate
b) org.springframework.jdbc.core.simple.SimpleJdbcTemplate
c) org.springframework.jdbc.*
d) none of the mentioned
View Answer
Explanation: org.springframework.jdbc.core.simple.SimpleJdbcTemplate is an evolution of JdbcTemplate that takes advantage of Java 1.5 features such as autoboxing, generics, and variable-length arguments to simplify its usage.
2. JdbcTemplate require statement parameters to be passed as an object array.
a) True
b) False
View Answer
Explanation: In SimpleJdbcTemplate, they can be passed as variable-length arguments; this saves you the trouble of wrapping them in an array.
3. To use SimpleJdbcTemplate:-
a) instantiate it directly
b) retrieve its instance by extending the SimpleJdbcDaoSupport class
c) all of the mentioned
d) none of the mentioned
View Answer
Explanation: To use SimpleJdbcTemplate, you can either instantiate it directly or retrieve its instance by extending the SimpleJdbcDaoSupport class.
4. SimpleJdbcTemplate offers a convenient batch update method in the form of:-
a) Vector
b) Set
c) Map
d) List
View Answer
Explanation: SimpleJdbcTemplate offers a convenient batch update method for you to specify a SQL statement and a batch of parameters in the form of List
5. Method has a warning from the Java compiler because of an unchecked conversion from List to List 6. The return type of the queryForObject() method will be determined by the class argument (e.g., String.class). 7. Named SQL parameters are specified by name (starting with a colon) rather than by position. 8. Named parameters are supported only in SimpleJdbcTemplate. 9. Implementations of the SqlParameterSource interface:- 10. The Spring framework offers a consistent data access exception-handling mechanism for its data access module. 11. In your DAO methods, you neither need to surround the code with a try/catch block nor declare throwing an exception in the method signature. 12. The direct parent class of DataAccessException is:- 13. Which concrete exception in the DataAccessException hierarchy should be thrown? 14. Error code for The statement was aborted because it would have caused a duplicate key value in a unique or primary key constraint. 15. How does the Spring JDBC framework know that state 23505 should be mapped to DuplicateKeyException? Sanfoundry Global Education & Learning Series – Java Spring.
a) findAll()
b) query()
c) update()
d) batchUpdate()
View Answer
Explanation: When using the classic JdbcTemplate, the findAll() method has a warning from the Java compiler because of an unchecked conversion from List to List
a) True
b) False
View Answer
Explanation: So, there’s no need for you to perform typecasting manually.
a) True
b) False
View Answer
Explanation: Another option when binding SQL parameters in the Spring JDBC framework is to use named parameters.
a) True
b) False
View Answer
Explanation: Named parameters are supported only in SimpleJdbcTemplate and NamedParameterJdbcTemplate.
a) MapSqlParameterSource
b) BeanPropertySqlParameterSource
c) none of the mentioned
d) all of the mentioned
View Answer
Explanation: There are three implementations of the SqlParameterSource interface. The basic one is MapSqlParameterSource, which wraps a map as its parameter source.
a) True
b) False
View Answer
Explanation: In general, all exceptions thrown by the Spring JDBC framework are subclasses of org.springframework.dao.DataAccessException, a type of RuntimeException that you are not forced to catch.
a) True
b) False
View Answer
Explanation: This is because DataAccessException (and therefore its subclasses, including DuplicateKeyException) is an unchecked exception that you are not forced to catch.
a) RuntimeException
b) NestedRuntimeException
c) Exception
d) Throwable
View Answer
Explanation: A core Spring exception class that wraps another exception in a RuntimeException.
a) errorCode
b) SQLState properties of the caught SQLException
c) All of the mentioned
d) None of the mentioned
View Answer
Explanation: As a DataAccessException wraps the underlying SQLException as the root cause, you can inspect the errorCode and SQLState properties with the following catch block.
a) -1
b) 0
c) 23505
d) 1
View Answer
Explanation: SQL State Message Text
23505 The statement was aborted because it would have caused a duplicate key value in a unique or primary.
a) error code
b) SQL state
c) all of the mentioned
d) none of the mentioned
View Answer
Explanation: The error code and SQL state are database specific, which means different database products may return different codes for the same kind of error.
To practice all areas of Java Spring, here is complete set of 1000+ Multiple Choice Questions and Answers.