Transfer Example (ii): Service level implementation (this example uses Queryrunner to execute SQL statements with Data source C3P0)

Source: Internet
Author: User

Cons: The service plane completes the DAO-level Open transaction operation

1. Create your own C3p0uti,account database and import the jar package


2.Dao level

Interface:

 Packagecom.learning. DAO;ImportCom.learning.domain.Account; Public InterfaceAccountdao {/*** Transfer *@paramfromname Transfer out user *@paramtoname Transfer to User *@paramMoney transfer Amount*/@Deprecated Public voidUpdateaccount (String fromname,string ToName,DoubleMoneythrowsException; /*** Change Amount according to account information *@paramaccout*/     Public voidUpdateaccout (account Accout)throwsException; /*** Find account information by user name *@paramname *@return     * @throwsException*/     PublicAccount Findaccountbyname (String name)throwsException;}

Implementation class:

 Packagecom.learning. Dao.impl;Importjava.sql.Connection;Importjava.sql.SQLException;ImportOrg.apache.commons.dbutils.QueryRunner;ImportOrg.apache.commons.dbutils.handlers.BeanHandler;Importcom.learning. Dao. Accountdao;Importcom.learning. Domain. account;Importcom.learning. util. C3p0util; Public classAccountdaoimplImplementsAccountdao {PrivateConnection Conn;  PublicAccountdaoimpl (Connection conn) { This. conn =Conn; }     Public voidUpdateaccount (String fromname, String toname,DoubleMoneythrowsException {//Create a Queryrunner objectQueryrunner QR =NewQueryrunner (C3p0util.getdatasource ()); Qr.update ("Update account set money=money-?" where Name=? ", Money,fromname); Qr.update ("Update account set money=money+?" where Name=? ", Money,toname); }     Public voidUpdateaccout (account account)throwsException {queryrunner qr=NewQueryrunner (); Qr.update (Conn,"Update account set money=?" where Name=? ", Account.getmoney (), Account.getname ()); }     PublicAccount Findaccountbyname (String name)throwsException {queryrunner qr=NewQueryrunner (); returnQr.query (conn, "select * from account where name=?",NewBeanhandler<account> (account.class), name); }}

3.Service level

Interface:

 Package com.learning. Service;  Public Interface Accountservice {    /**     * transfer      @param  fromname transfer out of the user      @param  toname  transferred to     user @param   money transfer     amount  *    /publicvoid transfer (String fromname,string toname,double  Money );}

Implementation class:

 Packagecom.learning. Service.impl;Importjava.sql.Connection;Importjava.sql.SQLException;Importcom.learning. Dao. Accountdao;Importcom.learning. Dao.impl.AccountDaoImpl;Importcom.learning. Domain. account;Importcom.learning. Service. Accountservice;Importcom.learning. util. C3p0util; Public classAccountserviceimplImplementsAccountservice { Public voidTransfer (String fromname, String toname,DoubleMoney ) {    //Ad.updateaccount (FromName, ToName, money);Connection conn =c3p0util.getconnection (); Accountdao AD=NewACCOUNTDAOIMPL (conn); Try{conn.setautocommit (false);//begin//get transferred out and transferred to the account object separatelyAccount Fromaccount =Ad.findaccountbyname (FromName); Account Toaccount=Ad.findaccountbyname (ToName); //Modify the respective amount of the accountFromaccount.setmoney (Fromaccount.getmoney ()-Money ); Toaccount.setmoney (Toaccount.getmoney ()+Money ); //Complete the transfer operationad.updateaccout (fromaccount);//int i = 10/0;ad.updateaccout (Toaccount); Conn.commit ();//Commit a transaction}Catch(Exception e) {Try{conn.rollback ();//rolling back a transaction}Catch(SQLException E1) {e1.printstacktrace (); }         }finally{            Try{conn.close (); } Catch(SQLException e) {e.printstacktrace (); }//Close        }    }}

Transfer Example (ii): Service level implementation (this example uses Queryrunner to execute SQL statements with Data source C3P0)

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.