001 | import java.sql.Connection; |
002 | import java.sql.ResultSet; |
003 | import java.sql.SQLException; |
004 | import com.mchange.v2.c3p0.ComboPooledDataSource; |
005 | import java.sql.PreparedStatement; |
006 | public 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 | } |