java mysql多次事务 模拟依据汇率转账,并存储转…
2018-06-18 02:36:41来源:未知 阅读 ()
Jar包使用,及层的划分
c3p0-config.xml
<?xml version="1.0" encoding="UTF-8"?> <c3p0-config> <!-- 默认配置,如果没有指定则使用这个配置 默认配置最常用 建议放在最开头。 放其他地方也没问题 --> <default-config> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://127.0.0.1:3306/test2</property> <property name="user">root</property> <property name="password">root</property> <property name="checkoutTimeout">30000</property> <property name="idleConnectionTestPeriod">30</property> <property name="initialPoolSize">10</property> <property name="maxIdleTime">30</property> <property name="maxPoolSize">100</property> <property name="minPoolSize">10</property> <property name="maxStatements">200</property> <user-overrides user="test-user"> <property name="maxPoolSize">10</property> <property name="minPoolSize">1</property> <property name="maxStatements">0</property> </user-overrides> </default-config> </c3p0-config>
JDBCUtils
package cn.itcast.tools; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.sql.DataSource; import com.mchange.v2.c3p0.ComboPooledDataSource; /*a). 私有,静态成员变量:ComboPooledDataSource 并创建对象;加载配置文 件的默认配置。 b). 公有,静态成员方法:public static DataSource getDataSource(),此方法返 回C3P0连接池对象; c). 公有,静态成员方法:public static Connection getConnection(),此方法返回 通过C3P0连接池获取的Connection对象;使用本地线程 ThreadLocal*/ public class JDBCUtils { private static ComboPooledDataSource dataSource =new ComboPooledDataSource(); private static ThreadLocal<Connection> local=new ThreadLocal<Connection>(); public static DataSource getDataSource() { return dataSource; } public static Connection getConnection(){ Connection con=local.get(); if(con==null) { try { con=dataSource.getConnection(); local.set(con); } catch (SQLException e) { e.printStackTrace(); } con=local.get(); } return con; } public static void close(Connection con,Statement st,ResultSet rs) { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (st != null) { try { st.close(); } catch (SQLException e) { e.printStackTrace(); } } if (con != null) { try { con.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
dao
package com.swift.dao; import java.sql.Connection; import java.sql.SQLException; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanHandler; import org.apache.commons.dbutils.handlers.ScalarHandler; import cn.itcast.domain.Account; import cn.itcast.tools.JDBCUtils; public class Dao { private QueryRunner qr = new QueryRunner(); private Connection con=JDBCUtils.getConnection(); public void querySenderBalance(String cardid1, double money) throws SQLException { String sql = "SELECT * FROM account WHERE cardid=?;"; BeanHandler<Account> rsh = new BeanHandler<Account>(Account.class); Account bean = qr.query(con, sql, rsh, cardid1); System.out.println(bean); double balance = bean.getBalance(); System.out.println(balance); if (balance < money) { System.out.println("余额不足!"); System.exit(0); } else { System.out.println("余额足够了!"); } } public int zhuanchu(String cardid, double money) throws SQLException { String chaxun = "select moneyRate from account where cardid=?"; double rate=(double) qr.query(con,chaxun, new ScalarHandler(),cardid); double moneyRate=money*rate/100; System.out.println(moneyRate); String shengyu = "UPDATE account SET balance =balance-?-? WHERE cardid=?;"; return qr.update(con, shengyu, money,moneyRate,cardid); } public int zhuanru(String cardid, double money) throws SQLException { String zengjia = "UPDATE account SET balance =balance+? WHERE cardid=?;"; return qr.update(con, zengjia, money,cardid); } public int writeInfo(String cardid, String tratype,double money,String tradate) throws SQLException { String info = "insert into transaction(cardid,tratype,tramoney,tradate) values(?,?,?,?);"; return qr.update(con, info, cardid,tratype,money,tradate); } }
Service
package com.swift.service; import java.sql.Connection; import java.sql.SQLException; import java.text.SimpleDateFormat; import java.util.Date; import com.swift.dao.Dao; import cn.itcast.tools.JDBCUtils; public class Service { private Connection con=JDBCUtils.getConnection(); public void transfer(String cardid1, String cardid2, double money) { try { con.setAutoCommit(false); Dao dao=new Dao(); dao.querySenderBalance(cardid1, money); int chu=dao.zhuanchu(cardid1, money); int ru=dao.zhuanru(cardid2, money); con.commit(); if(chu>0&&ru>0) { System.out.println("转账成功."); } Date date=new Date(); SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String tradate=sdf.format(date); dao.writeInfo(cardid1, "转出", money, tradate); dao.writeInfo(cardid2, "转入", money, tradate); con.commit(); } catch (Exception e) { System.out.println("出现异常准备回滚."); e.printStackTrace(); try { con.rollback(); } catch (SQLException e1) { e1.printStackTrace(); } } finally { if (con != null) { try { con.close(); } catch (SQLException e) { e.printStackTrace(); } } } } }
client
package com.swift.client; import com.swift.service.Service; public class Client { public static void main(String[] args) { String cardid1 = "6212999999999"; String cardid2 = "6212888888888"; double money = 600; Service s=new Service(); s.transfer(cardid1, cardid2, money); } }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
上一篇:Hibernate的关联映射
- 国外程序员整理的Java资源大全(全部是干货) 2020-06-12
- 2020年深圳中国平安各部门Java中级面试真题合集(附答案) 2020-06-11
- 2020年java就业前景 2020-06-11
- 04.Java基础语法 2020-06-11
- Java--反射(框架设计的灵魂)案例 2020-06-11
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash