DBMNG数据库管理与应用

书籍是全世界的营养品。生活里没有书籍,就好像没有阳光;智慧里没有书籍,就好像鸟儿没有翅膀。
当前位置:首页 > MySQL > 基础知识

Java中SQL用事务处理转账操作示例

这里以MySQL(InnoDB引擎)为例:


001import java.sql.Connection;
002import java.sql.ResultSet;
003import java.sql.SQLException;
004import com.mchange.v2.c3p0.ComboPooledDataSource;
005import java.sql.PreparedStatement;
006public class testcommit
007{
008    /**
009    * @param args
010    * @throws SQLException
011    */
012    public static void main(String[] args) throws SQLException
013    {
014        ComboPooledDataSource dataSource = new ComboPooledDataSource();
015        Connection connection = null;
016        // Statement statement=null;
017        // String sql="";
018        try
019        {
020            connection = dataSource.getConnection();
021            // statement=connection.createStatement();
022            connection.setAutoCommit(false);
023            // sql="insert into t1(a,b)values('345','gdf')";
024            // statement.executeUpdate(sql);
025            // sql="insert into t2(c,d)values('43243','dddddddd')";
026            // statement.executeUpdate(sql);
027            // System.out.println(statement.executeBatch());
028            if (zhuanchu("aaa", 10.00, connection) == 0)
029            throw new SQLException("转出操作失败");
030            if (zhuanru("bbb", 10.00, connection) == 0)
031            throw new SQLException("转入操作失败");
032            connection.commit();
033            connection.setAutoCommit(true);
034            connection.close();
035            System.out.println("转账操作成功!");
036        } catch (SQLException e)
037        {
038        connection.rollback();
039        connection.setAutoCommit(true);
040        connection.close();
041        e.printStackTrace();
042        System.out.println(e);
043        }
044    }
045    public static int zhuanchu(String zhanghao, double jine, Connection conn)
046    {
047        int rtnVal = 1;
048        PreparedStatement pStatement = null;
049        ResultSet rs = null;
050        try
051        {
052            pStatement = conn.prepareStatement("select yue from t1 where yonghuming=?");
053            pStatement.setString(1, zhanghao);
054            rs = pStatement.executeQuery();
055            if (rs.next())
056            {
057            if (rs.getDouble("yue") - jine < 0)
058            {
059            System.out.println(zhanghao + "的余额不足");
060            rtnVal = 0;
061            } else
062            {
063            pStatement = conn.prepareStatement("update t1 set yue=yue-? where yonghuming=?");
064            pStatement.setDouble(1, jine);
065            pStatement.setString(2, zhanghao);
066            if (pStatement.executeUpdate() == 0)
067            rtnVal = 0;
068            }
069            } else
070            {
071            rtnVal = 0;
072            }
073            pStatement.close();
074        } catch (Exception e)
075        {
076            System.out.println(e);
077            rtnVal = 0;
078        }
079        return rtnVal;
080    }
081    public static int zhuanru(String zhanghao, double jine, Connection conn)
082    {
083        int rtnVal = 1;
084        PreparedStatement pStatement = null;
085        try
086        {
087        pStatement = conn.prepareStatement("update t1 set yue=yue+? where yonghuming=?");
088        pStatement.setDouble(1, jine);
089        pStatement.setString(2, zhanghao);
090        if (pStatement.executeUpdate() == 0)
091        rtnVal = 0;
092        pStatement.close();
093        } catch (Exception e)
094        {
095        System.out.println(e);
096        rtnVal = 0;
097        }
098        return rtnVal;
099    }
100}


这里再重点推荐一篇更加详细的解释Java操作MySQL使用事务实现银行转账的示例文章java操作MySQL事务的并发和隔离-银行金融账户转账示例

本站文章内容,部分来自于互联网,若侵犯了您的权益,请致邮件chuanghui423#sohu.com(请将#换为@)联系,我们会尽快核实后删除。
Copyright © 2006-2023 DBMNG.COM All Rights Reserved. Powered by DEVSOARTECH            豫ICP备11002312号-2

豫公网安备 41010502002439号