Java mysql multi-transaction simulation based on exchange rate transfer, and storage of the transfer information layer to complete the dao layer service layer client layer connection pool using C3p0 write warehouse using DBUtils,

Source: Internet
Author: User

Java mysql multi-transaction simulation based on exchange rate transfer, and storage of the transfer information layer to complete the dao layer service layer client layer connection pool using C3p0 write warehouse using DBUtils,

Jar package usage and layer division

 

C3p0-config.xml

<? Xml version = "1.0" encoding = "UTF-8"?> <C3p0-config> <! -- Default configuration. If this parameter is not specified, we recommend that you use the default configuration to start with the most common configuration. It's okay to put it elsewhere --> <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" & gt; 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;/* ). private and static member variables: ComboPooledDataSource and create objects; load the default configuration of the configuration file. B ). public, static member method: public static DataSource getDataSource (). This method returns the C3P0 connection pool object; c ). public and static member Methods: public static Connection getConnection (). This method returns the Connection object obtained through the C3P0 Connection pool; use the local thread ThreadLocal */public class JDBCUtils {private static combooleddatasource dataSource = new ComboPooledDataSource (); private static ThreadLocal <Connection> local = new ThreadLocal <Connection> (); public static DataSource getDataSource () {retu Rn 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. getC Onnection (); 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 ("insufficient balance! "); System. exit (0);} else {System. out. println (" the balance is enough! ") ;}} 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. querySen DerBalance (cardid1, money); int chu = dao. zhuanchu (cardid1, money); int ru = dao. zhuanru (cardid2, money); con. commit (); if (chu> 0 & ru> 0) {System. out. println ("transfer successful. ");} Date date = new Date (); SimpleDateFormat sdf = new SimpleDateFormat (" yyyy-MM-dd HH: mm: ss "); String tradate = sdf. format (date); dao. writeInfo (cardid1, "transfer out", money, tradate); dao. writeInfo (cardid2, "transfer", money, tradate); con. commit ();} catch (Exception e) {System. out. println ("prepare to roll back when an exception occurs. "); 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);            }}

 

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.