JDBC has been able to meet the needs of most users to wipe the European OH database, but in the use JDBC , the app must manage the database resources on its own. Spring provides good support for database operational requirements, and builds an abstraction layer on the basis of the original JDBC , which provides many uses of JDBC Templates and drive modules provide greater convenience for Spring applications to operate relational databases.
Spring packaged templates, encapsulating the basic process of database access, user-friendly.
First, the template method
Spring JdbcTemplate from the name, this is a template, indeed, it does implement the template pattern in the involved pattern. as follows:
JdbcTemplateinherits the base classJdbcAccessorand Interface Classesjdbcoperation. In the base classJdbcAccessorIn the design, theDataSourcedata sources for management and configuration. In thejdbcoperationinterface, defined by theJdbcthe basic operation method of the database operation, andJdbcTemplateprovides implementations of these interface methods, such asExecutemethods,Querymethods,Updatemethods and so on.
Second, useJdbcTemplate
JdbcTemplate temp = new JdbcTemplate (datasource); Class Executestatementcallback implements statementcallback<object>,sqlprovider{public object Doinstatement (Statement stmt) throws SQLException {//spring encapsulate database operation Stmt.execute (); return null; } Public String GetSQL () { return SQL; } } Temp.sexecute (New Executestatemnetcallback ());
Iii. JdbcTemplate Implementation of Execute
in Execute For example:
by seeing that,theExecute method encapsulates the operation of the database, first obtains the database connection Connection, According to the application to database operation needs to create the database Statement, the database operation callback, processing the database exception, and finally the database Connection shut down.
Code:
public void execute (final String sql) throws dataaccessexception{if (logger.isdebugenabled ()) {Logger.debug ("executing SQL statement ["+ SQL +"] "); } class Executestatementcallback implements statementcallback<object>,sqlprovider{public Object doinstatement ( Statement stmt) throws sqlexception{Stmt.execute (SQL); return null; } public String GetSQL () {return SQL; }} execute (new Executestatementcallback ()); }//Use java.sql.Statement to handle static SQL statements public <T> T execute (statementcallback<t> action) throws Dataaccessexcep tion{Assert.notnull (Action, "Callback object must not is null"); This obtains the database Connection, the Connection of this database is already under the spring transaction management Connection con = datasourceutils.getconnection (getdatasource ()); Statement stmt = null; try {Connection contouse = con;if (this.nativejdbcextractor! = null && This.nativeJdbcExtractor.isNativeConnectionNecessaryForNativeStatements ()) {Contouse = This.nativeJdbcExtractor.getNativeConnection (con);}//Create statementstmt = Contouse.createstatement (); applystatementsettings (stmt); Statement stmttouse = stmt;if (this.nativejdbcextractor! = null) {Stmttouse = This.nativeJdbcExtractor.getNativeStatement (stmt);} Here call the callback function t result = action.doinstatement (stmttouse); handlewarnings (stmt); return result;} catch (SQLException ex) {//If an exception is caught, the database connection is freed, and a spring-converted spring Database exception jdbcutils.closestatement (stmt) is thrown out; stmt = null ;D atasourceutils.releaseconnection (Con, Getdatasource ()); con = Null;throw getexceptiontranslator (). Translate (" Statementcallback ", GetSQL (Action), ex);} finally{jdbcutils.closestatement (stmt); Release database link datasourceutils.releaseconnection (Con, Getdatasource ()); } }
Iv. Summary
through this , on the one hand, improves the efficiency of application development, on the other hand provides flexibility for application development. In addition spring established jdbc jdbc template, this implementation is more like a simple orm tool provides another option for your application.
Here 's a continuation of the dynamic build table using Spring jdbctemplate .
Spring JdbcTemplate FRAME (1)--Fundamentals