"Sping Revelation" 18, using spring to access data

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.