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

缺点:Service层面把Dao层面的开启事务操作完成了

1.自行创建C3P0Uti,account数据库,导入Jar包

2.Dao层面

接口:

package com.learning.dao;

import com.learning.domain.Account;

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

    /**
     * 根据账户信息修改金额
     * @param accout
     */
    public void updateAccout(Account accout) throws Exception;

    /**
     * 根据用户名查找账户信息
     * @param name
     * @return
     * @throws Exception
     */
    public Account findAccountByName(String name)throws Exception;
}

实现类:

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 {
        //创建一个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);
    }

    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 {
    /**
     * 转账
     * @param fromname 转出用户
     * @param toname  转入用户
     * @param money  转账金额
     */
    public void transfer(String fromname,String toname,double money);
}

实现类:

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
            //分别得到转出和转入账户对象
            Account fromAccount = ad.findAccountByName(fromname);
            Account toAccount = ad.findAccountByName(toname);

            //修改账户各自的金额
            fromAccount.setMoney(fromAccount.getMoney()-money);
            toAccount.setMoney(toAccount.getMoney()+money);

            //完成转账操作
            ad.updateAccout(fromAccount);
//            int i = 10/0;
            ad.updateAccout(toAccount);

            conn.commit();//提交事务
        } catch (Exception e) {
            try {
                conn.rollback();//回滚事务
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        }finally{
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }//关闭
        }
    }

}
时间: 2024-07-30 03:04:38

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

转账示例(四):service层面实现(线程管理Connection,AOP思想,动态代理)(本例采用QueryRunner来执行sql语句,数据源为C3P0)

用了AOP(面向切面编程),实现动态代理,service层面隐藏了开启事务. 1.自行创建C3P0Uti,account数据库,导入Jar包 2.Dao层面 接口: package com.learning.dao; import com.learning.domain.Account; public interface AccountDao { /** * 转账 * @param fromname 转出用户 * @param toname 转入用户 * @param money 转账金额 */

转账示例(三):service层面实现(线程管理Connection)(本例采用QueryRunner来执行sql语句,数据源为C3P0)

缺点:Service层面还是不应该出现关于事务的操作 1.自行创建C3P0Uti,account数据库,导入Jar包 2.Dao层面 接口: package com.learning.dao; import com.learning.domain.Account; public interface AccountDao { /** * 转账 * @param fromname 转出用户 * @param toname 转入用户 * @param money 转账金额 */ @Deprecated

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

缺点: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 转账金额 *

C#参数化执行SQL语句,防止漏洞攻击本文以MySql为例【20151108非查询操作】

为什么要参数化执行SQL语句呢? 一个作用就是可以防止用户注入漏洞. 简单举个列子吧. 比如账号密码登入,如果不用参数, 写的简单点吧,就写从数据库查找到id和pw与用户输入一样的数据吧 sql:select id,pw where id='inputID' and pw='inputPW'; 一般情况没什么问题,但如果用户输入的id或PW带 ‘ ,这是可能就会出现漏洞,bug了 比如用户输入的id是: 1‘ or ’1‘=‘1 这是sql语句执行的是:select id,pw where id

C#与数据库访问技术总结(六)之Command对象创建SQl语句代码示例

Command对象创建SQl语句代码示例 说明:前面介绍了 Command 对象的方法和一些属性,回顾一下 Command对象主要用来执行SQL语句.利用Command对象,可以查询数据和修改数据. 在下面这段代码里,首先根据连接字符串创建一个SqlConnecdon连接对象,并用此对象连接数据源:然后创建一个SqlCommand对象,并用此对象的ExecuteNonQuery方法执行不带返回结果集的SQL语句. 1 //连接字符串 2 3 private static string strCo

python数据库连接之pyMysql -(二):使用变量向SQL语句中传递参数

使用MySQLdb连接数据库执行sql语句时,有以下几种传递参数的方法: 一.通过自定义参数传递: import pymysql import types dbinfo={"host":"192.168.6.41", "user":"lrtsaudio", "password":"2&Ty3DW75i!(vgo.l3Odp1fgWgEG", "db":&quo

Android清理设备内存详细完整示例(二)

MainActivity如下: package cc.c; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.List; import android.app.Activity; import android.app.ActivityManager; import android.app.ActivityManager.MemoryInfo;

Android图片旋转,缩放,位移,倾斜,对称完整示例(二)——Bitmap.createBitmap()和Matrix

MainActivity如下: package cc.c; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.Matrix; import android.graphics.drawable.BitmapDrawable; import android.os.Bundle; import android.widget.ImageView; /** * Demo描述: * 利用B

Android利用Volley异步加载数据完整详细示例(二)

MainActivity如下: package cc.y; import android.app.Activity; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Bitmap.Config; import android.os.Bundle; import android.util.LruCache; import android.widget.ImageView;