Unified hierarchical system of data access anomalies
Based on the basic JDBC Encapsulation DAO layer Access interface, the package is accessible, CSV file, relational database (RDBMS), LADP can be encapsulated into a DAO object for access
Throw a question
But for us to ignore a problem, that is, when the DAO layer occurs when an exception, we should be in the DAO layer processing, if so, then there is a problem, when the client calls the method, how to know what happened, the client for access to the data layer is unpredictable, So we need to throw the exception in DAO, that is, throw Exception on the class.
But there is a problem, DAO is we encapsulate access to data, but for the client, it does not care to access the above data, but the DAO layer needs to be encapsulated in different data layers, if it is JDBC, then the exception may be SQLException, but if it is LDAP, That may throw namingexception, if we change the data Access object, then the client will have to deal with the second exception, the client code has to be changed!!!
Solve the problem:
Uniform encapsulation of exceptions with RuntimeException, and then throws this one type of exception, here also need to be exception to the classification of the translation
That is, according to different databases, the extraction of information, and then packaged to throw, of course, a runtimeexception is certainly not enough, then we can design subclasses, to distinguish between different subclass of the exception object
Let's look at spring's anomaly system.
Here is the Spring wrapper exception, let's look at its corresponding DAO's package
best Practices for the JDBC API
Spring has 2 kinds of practices
1, using JdbcTemplate as the core, template-based JDBC use
2. Construct the JDBC based on the JdbcTemplate base on the Operation object
Before we start using JdbcTemplate, let's take a look at the template method pattern
Http://www.cnblogs.com/cutter-point/p/5263492.html
JdbcTemplate class relationships in spring
Five types of queries available in JdbcTemplate
There are five basic types:
Connectioncallback Direct use of connection
Statementcallback use of externally exposed statement objects
String SQL uses SQL directly, this is nothing to say ...
PreparedStatementCallback exposing PreparedStatement objects to use
CallableStatementCallback calls a stored procedure using the
Sping Revelation 18, using spring to access data