日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

转账示例(二):service层面实现(本例采用QueryRunner来执行sql语句,数据源为C3P0)...

發(fā)布時(shí)間:2025/3/15 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 转账示例(二):service层面实现(本例采用QueryRunner来执行sql语句,数据源为C3P0)... 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

缺點(diǎn):Service層面把Dao層面的開啟事務(wù)操作完成了

1.自行創(chuàng)建C3P0Uti,account數(shù)據(jù)庫,導(dǎo)入Jar包


2.Dao層面

?接口:

package com.learning.dao;import com.learning.domain.Account;public interface AccountDao {/*** 轉(zhuǎn)賬* @param fromname 轉(zhuǎn)出用戶* @param toname 轉(zhuǎn)入用戶* @param money 轉(zhuǎn)賬金額*/@Deprecatedpublic void updateAccount(String fromname,String toname,double money)throws Exception;/*** 根據(jù)賬戶信息修改金額* @param accout*/public void updateAccout(Account accout) throws Exception;/*** 根據(jù)用戶名查找賬戶信息* @param name* @return* @throws Exception*/public Account findAccountByName(String name)throws Exception; }

實(shí)現(xiàn)類:

package com.learning.dao.impl;import java.sql.Connection; import java.sql.SQLException;import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanHandler;import com.learning.dao.AccountDao; import com.learning.domain.Account; import com.learning.util.C3P0Util;public class AccountDaoImpl implements AccountDao {private Connection conn;public AccountDaoImpl(Connection conn) {this.conn = conn;}public void updateAccount(String fromname, String toname, double money) throws Exception {//創(chuàng)建一個(gè)QueryRunner對(duì)象QueryRunner qr = new QueryRunner(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 void updateAccout(Account account) throws Exception {QueryRunner qr = new QueryRunner();qr.update(conn,"update account set money=? where name=?",account.getMoney(),account.getName());}public Account findAccountByName(String name) throws Exception {QueryRunner qr = new QueryRunner();return qr.query(conn,"select * from account where name=?", new BeanHandler<Account>(Account.class),name);}}

3.Service層面

接口:

package com.learning.service;public interface AccountService {/*** 轉(zhuǎn)賬* @param fromname 轉(zhuǎn)出用戶* @param toname 轉(zhuǎn)入用戶* @param money 轉(zhuǎn)賬金額*/public void transfer(String fromname,String toname,double money); }

實(shí)現(xiàn)類:

package com.learning.service.impl;import java.sql.Connection; import java.sql.SQLException;import com.learning.dao.AccountDao; import com.learning.dao.impl.AccountDaoImpl; import com.learning.domain.Account; import com.learning.service.AccountService; import com.learning.util.C3P0Util;public class AccountServiceImpl implements AccountService {public void transfer(String fromname, String toname, double money) {// ad.updateAccount(fromname, toname, money);Connection conn = C3P0Util.getConnection();AccountDao ad = new AccountDaoImpl(conn);try {conn.setAutoCommit(false);//begin//分別得到轉(zhuǎn)出和轉(zhuǎn)入賬戶對(duì)象Account fromAccount = ad.findAccountByName(fromname);Account toAccount = ad.findAccountByName(toname);//修改賬戶各自的金額fromAccount.setMoney(fromAccount.getMoney()-money);toAccount.setMoney(toAccount.getMoney()+money);//完成轉(zhuǎn)賬操作 ad.updateAccout(fromAccount); // int i = 10/0; ad.updateAccout(toAccount);conn.commit();//提交事務(wù)} catch (Exception e) {try {conn.rollback();//回滾事務(wù)} catch (SQLException e1) {e1.printStackTrace();} }finally{try {conn.close();} catch (SQLException e) {e.printStackTrace();}//關(guān)閉 }}}

?

轉(zhuǎn)載于:https://www.cnblogs.com/youwillsee/p/6664848.html

總結(jié)

以上是生活随笔為你收集整理的转账示例(二):service层面实现(本例采用QueryRunner来执行sql语句,数据源为C3P0)...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。