Spring Questions and Answers – Using the Simple JDBC Template and Handling Exceptions

This set of Java Spring Quiz 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

Answer: b
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

Answer: a
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

Answer: c
Explanation: To use SimpleJdbcTemplate, you can either instantiate it directly or retrieve its instance by extending the SimpleJdbcDaoSupport class.
advertisement
advertisement

4. SimpleJdbcTemplate offers a convenient batch update method in the form of:-
a) Vector
b) Set
c) Map
d) List
View Answer

Answer: d
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 so that you don’t need to implement the BatchPreparedStatementSetter interface.

5. Method has a warning from the Java compiler because of an unchecked conversion from List to List.
a) findAll()
b) query()
c) update()
d) batchUpdate()
View Answer

Answer: a
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. This is because the return type of the query() method is List rather than the type-safe List.
Note: Join free Sanfoundry classes at Telegram or Youtube

6. The return type of the queryForObject() method will be determined by the class argument (e.g., String.class).
a) True
b) False
View Answer

Answer: a
Explanation: So, there’s no need for you to perform typecasting manually.

7. Named SQL parameters are specified by name (starting with a colon) rather than by position.
a) True
b) False
View Answer

Answer: a
Explanation: Another option when binding SQL parameters in the Spring JDBC framework is to use named parameters.
advertisement

8. Named parameters are supported only in SimpleJdbcTemplate.
a) True
b) False
View Answer

Answer: a
Explanation: Named parameters are supported only in SimpleJdbcTemplate and NamedParameterJdbcTemplate.

9. Implementations of the SqlParameterSource interface:-
a) MapSqlParameterSource
b) BeanPropertySqlParameterSource
c) none of the mentioned
d) all of the mentioned
View Answer

Answer: d
Explanation: There are three implementations of the SqlParameterSource interface. The basic one is MapSqlParameterSource, which wraps a map as its parameter source.
advertisement

10. The Spring framework offers a consistent data access exception-handling mechanism for its data access module.
a) True
b) False
View Answer

Answer: a
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.

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.
a) True
b) False
View Answer

Answer: a
Explanation: This is because DataAccessException (and therefore its subclasses, including DuplicateKeyException) is an unchecked exception that you are not forced to catch.

12. The direct parent class of DataAccessException is:-
a) RuntimeException
b) NestedRuntimeException
c) Exception
d) Throwable
View Answer

Answer: b
Explanation: A core Spring exception class that wraps another exception in a RuntimeException.

13. Which concrete exception in the DataAccessException hierarchy should be thrown?
a) errorCode
b) SQLState properties of the caught SQLException
c) All of the mentioned
d) None of the mentioned
View Answer

Answer: c
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.

14. Error code for The statement was aborted because it would have caused a duplicate key value in a unique or primary key constraint.
a) -1
b) 0
c) 23505
d) 1
View Answer

Answer: a
Explanation: SQL State Message Text
23505 The statement was aborted because it would have caused a duplicate key value in a unique or primary.

15. How does the Spring JDBC framework know that state 23505 should be mapped to DuplicateKeyException?
a) error code
b) SQL state
c) all of the mentioned
d) none of the mentioned
View Answer

Answer: c
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.

Sanfoundry Global Education & Learning Series – Java Spring.
To practice all areas of Java Spring for quizzes, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.