Spring + jdbctemplate + jdbcdaosupport examples

Source: Internet
Author: User

In Spring JDBC development, you can use JdbcTemplate and JdbcDaoSupport classes to simplify the overall database operation processes.

In this tutorial, we'll reuse the last Spring + JDBC example, to see the different between a before (No jdbctemplate sup Port) and after (with JdbcTemplate support) example.

1. Example without JdbcTemplate

Witout JdbcTemplate, you has to create many redundant codes (create connection, close connection, handle exception) in All the DAO database operation Methods–insert, update and delete. It just not efficient, ugly, error prone and tedious.

Private DataSource DataSource; public void Setdatasource (DataSource DataSource) {this.datasource = DataSource;} public void Insert (Customer customer) {S Tring sql = "INSERT into CUSTOMER" + "(cust_id, NAME, age) VALUES (?,?,?)"; Connection conn = null; try {conn = datasource.getconnection (); PreparedStatement PS = conn.preparestatement (sql);p s.setint (1, Customer.getcustid ());p s.setstring (2, Customer.getname ());p S.setint (3, Customer.getage ());p s.executeupdate ();p s.close (); } catch (SQLException e) {throw new RuntimeException (e);} finally {if (conn! = null) {try {conn.close ()} catch (Sqlexcep tion e) {}}}}
2. Example with JdbcTemplate

With JdbcTemplate, you save a lot of typing on the redundant codes, becuase JdbcTemplate would handle it automatically.

Private DataSource datasource;private JdbcTemplate jdbctemplate; public void Setdatasource (DataSource DataSource) {this.datasource = DataSource;} public void Insert (Customer customer) {S Tring sql = "INSERT into CUSTOMER" + "(cust_id, NAME, age) VALUES (?,?,?)"; JdbcTemplate = new JdbcTemplate (DataSource); Jdbctemplate.update (SQL, new object[] {Customer.getcustid (), Customer.getname (), Customer.getage ()  });}

See the different?

3. Example with Jdbcdaosupport

By extended the Jdbcdaosupport, set the DataSource and JdbcTemplate in your class are no longer required, you just need to Inject the correct datasource into Jdbccustomerdao. And you can get the JdbcTemplate by using a getjdbctemplate () method.

public class Jdbccustomerdao extends Jdbcdaosupport implements customerdao{//no need to set DATASOURC e here public void insert (Customer customer) { string sql = ' INSERT into Customer ' + ' (cust_id, NAME, age) VALUES (? , ?, ?)";  getjdbctemplate (). Update (SQL, new object[] {Customer.getcustid (), Customer.getname (), Customer.getage ()});  } 
<beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001 /xmlschema-instance "xsi:schemalocation=" http://www.springframework.org/schema/beanshttp:// Www.springframework.org/schema/beans/spring-beans-2.5.xsd "> <bean id=" DataSource "class=" ORG.SPRINGFR Amework.jdbc.datasource.DriverManagerDataSource "> <property name=" Driverclassname "value=" Com.mysql.jdbc.Driver "/><property name=" url "value=" Jdbc:mysql://localhost:3306/mkyongjava "/>< Property name= "username" value= "root"/><property name= "password" value= "password"/></bean>  </beans> 
<beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "xsi:schemalocation=" http://www.springframework.org/schema/beanshttp:// Www.springframework.org/schema/beans/spring-beans-2.5.xsd "> <bean id=" Customerdao "class=" Com.mkyong.customer.dao.impl.JdbcCustomerDAO "><property name=" DataSource "ref=" DataSource "/></bean > </beans>
Note
In Spring JDBC development, it's always recommended to use JdbcTemplateand JdbcDaoSupport, instead of coding JDBC code yourself. Download Source codedownload it–spring-jdbc-example.zip (KB)

Spring + jdbctemplate + jdbcdaosupport examples

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.