码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2017-04-04 14:20:24      阅读:254      评论:0      收藏:0      [点我收藏+]

标签:row   set   sql语句   class   接口   示例   query   ring   ram   

缺点:Dao层面把Service层面的操作完成了,不利于后期的代码修改和重构

1.自行创建C3P0Util

account数据库

技术分享

 

2.jar包

技术分享

3.Dao层面

接口:

package com.learning.dao;

import com.learning.domain.Account;

public interface AccountDao {
    /**
     * 转账
     * @param fromname 转出用户
     * @param toname  转入用户
     * @param money  转账金额
     */
    public void updateAccount(String fromname,String toname,double money)throws Exception;
}

 实现类:

package com.learning.dao.impl;

import java.sql.SQLException;

import org.apache.commons.dbutils.QueryRunner;

import com.learning.dao.AccountDao;
import com.learning.util.C3P0Util;

public class AccountDaoImpl implements AccountDao {

    public void updateAccount(String fromname, String toname, double money) throws Exception {
        //创建一个QueryRunner对象
        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);
    }

}

 

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

标签:row   set   sql语句   class   接口   示例   query   ring   ram   

原文地址:http://www.cnblogs.com/youwillsee/p/6664836.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!