Spring's development was designed to mitigate the complexity of enterprise-level development, as is its support for database access, and the following benefits of using spring to Access databases:
1.1 Simplified Code
Using native JDBC to access the database, you typically always perform the following steps:
1) Access to database resources, such as connections;
2) Prepare and execute SQL and process the returned results
3) Releasing database resources
4) Handle all of the above steps, and also catch exceptions in the process of handling exceptions.
The typical code structure is as follows:
Public Testobj querytestobj (long ID)
{
Final String sql_select_obj= "SELECT * from Testobj where id=?";
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs=null;
Try
{
conn = Datasource.getconnection ();
stmt = Conn.preparestatement (sql_select_obj);
Stmt.setlong (1, id);
rs = Stmt.executequery ();
Testobj obj=null;
if (Rs.next ())
{
obj = new testobj ();
Set the queried properties to the Testobj object
}
return obj;
}
Catch (SQLException ex)
{
Handling Exceptions
}
finally
{
if (rs!=null)
{
Try
{
Rs.close ();
}
Catch (SQLException ex)
{
}
}
if (stmt!=null)
{
Try
{
Stmt.close ();
}
Catch (SQLException ex)
{
}
}
if (conn!=null)
{
Try
{
Conn.close ();
}
Catch (SQLException ex)
{
}
}
}
return null;
}
As you can see from the sample code, the real business-related parts take up less than 20% of the code, while most of the other code is doing business-agnostic, but these "redundant code" are essential and required for every database processing, based on the principle of "split responsibility", The solution to this problem is obvious: bring these "redundant code" together. "The big steps are the same, and the processing details of some steps are different", which is a typical scenario for "template mode".
Spring has been done for us.
Looking at the example first, using the JDBC template provided by spring, the code will look like this:
Public Testobj querytestobjbysping (long ID)
{
Final String sql_select_obj= "SELECT * from Testobj where id=?";
return JdbcTemplate. queryForObject (Sql_select_obj,
New Parameterizedrowmapper<testobj> ()
{
@Override
Public Testobj Maprow (ResultSet arg0, int arg1)
throws SQLException
{
Testobj obj = new testobj ();
Set the queried properties to the Testobj object
return obj;
}
}, ID);
}
1.2 Refinement of the anomaly system
Native JDBC Database operation exception classes include Batchupdateexception, DataTruncation, SQLException, sqlwarning Four, and it is difficult to understand that these exceptions are checked exceptions, which means that They must be explicitly captured in the code, but if these exceptions occur, they are generally fatal and run-time unrecoverable errors that make it difficult to have other meaningful operations outside of the print stack in the exception handling block.
Spring has made two improvements: (1) Refine the exception to facilitate the analysis of the problem, and (2) modify the exception to the unchecked type.
The spring refinement exceptions are as follows
Simplifying the JDBC operations database with spring