Spring JDBC Abstract Framework simplifies web database development

Source: Internet
Author: User
Tags abstract error code error handling requires web database

I. INTRODUCTION

Spring is a lightweight application framework. In many cases, spring can be a good substitute for the traditional services provided by Java EE application servers. Spring is both comprehensive and modular. Based on its layered architecture, it enables developers to flexibly use any part of their own. Spring is made up of many modules, such as IOC containers, AOP,MVC, Persistence, DAO, and remoting. These modules are fairly loosely coupled: some modules are not used at all to require additional modules. In the past, there was no such thing as a spring application: You could choose to build your application with some, most, or all of the components supported by the spring framework.

The JDBC support provided by the spring framework is not tightly coupled with the other spring parts, which is extremely advantageous to the maintainability of the code. This article will show you how to benefit from spring for any application that directly uses JDBC (i.e., using JDBC without some of the O/R mapping framework itself).

Two. Traditional type JDBC

Traditional JDBC has many positive aspects that make it important in the development of many J2SE and Java-EE applications. However, there are some features that make it difficult to use:

Developers need to deal with a large number of complex tasks and infrastructure, such as a large number of try-catch-finally-try-catch blocks.

The application requires complex error handling to determine that the connection is properly closed after use, which makes the code verbose, bloated, and repetitive.

A SqlException exception with a very ambiguous nature is used in JDBC.

JDBC does not introduce a specific exception subclass hierarchy mechanism.

As with any error, it simply throws a SqlException exception-whether it comes from the JDBC driver or from the database-which makes it difficult for programmers to understand exactly where the error actually occurred. For example, if the SQL object is invalid or has been locked, a SqlException exception is thrown. Debugging such an exception requires a certain amount of time to check the SQL state value and error code. Furthermore, the meaning of the SQL state value and the error code is somewhat different between the various databases.

It turns out that writing JDBC code is not an easy task-there's a lot of repetitive work to do. To illustrate the problem, here is an example of using traditional JDBC to get a list of available tasks from the database.

Package Com.spring.jdbc
Import java.sql.Connection;
import Java.sql.DriverManager;
Import Java.sql.PreparedStatement;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import Java.util.Vector;   
public class Traditionaljdbc {
public Vector gettasksnames () {
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
Vector task = new vector ();    
try {
con = getconnection ();
pstmt = Con.preparestatement ("Select TaskName from Tasks");
rs = Pstmt.executequery ();    
while (Rs.next ()) {
Task.add (rs.getstring (1));
  
} catch (SQLException e) {
System.out.println (e);
     Finally {
try {
Rs.close ();
Pstmt.close ();
Con.close ();    
} catch (SQLException E1) {
System.out.println (E1);
 
}
Return task;

Private Connection getconnection () throws Sqlexception {
Try {
Drivermanager.registerdriver (new Oracle.jdbc.driver.OracleDriver ());
   Return Drivermanager.getconnection ("Jdbc:oracle:thin: @localhost: 1521:orcl",
"Scott", "Tiger");
    The catch (SQLException Sqle) {
System.out.println (sqle);
return null;   
}
}
public static void Main (string[] args) {
Traditionaljdbc obj = new Traditionaljdbc ();
Vector task = Obj.gettasksnames ();   
for (int i = 0; i < task.size (); i++) {
System.out.println (Task.elementat (i));
}
}

In addition to the actual query database SQL code, the above example requires a large number of routine code. The Getconnection () method has nothing to do with our task, and even the Gettasksnames () method contains only two lines of code that are specific to the current task. All that remains is some common, complex task code.

Many of the positive aspects of JDBC make it still important in many J2SE and Java EE applications. However, as you can see, there are some features that make it more difficult to use than we might imagine. These tedious and sometimes frustrating features of JDBC have led to the emergence of many public available JDBC abstraction frameworks (such as Sqlexecutor and Apache Jakarta Commons dbutils), as well as countless home-grown JDBC application frameworks. A common, available JDBC Abstraction Framework is the JDBC abstraction of the spring framework.

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.