首页 > 代码库 > 【JDBC】实现JDBC实现银行的转账事务

【JDBC】实现JDBC实现银行的转账事务

JDBC中的事务是默认提交的,也就是说每执行一次PreparedStatement,那么数据就会被写入到磁盘。如果需要关闭默认提交,使用  void setAutoCommit(false.

db.properties

技术分享
driverClassName=oracle.jdbc.OracleDriverurl=jdbc:oracle:thin:@localhost:1521:xeusername=systempassword=517839
db.properties

JDBCUtilProperties.java

技术分享
package com.xdl.util;import java.io.IOException;import java.io.InputStream;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.Properties;public class JDBCUtilProperties {    public  static   String   driverClassName;    public  static   String   url;    public  static   String   username;    public  static   String   password;    static {        try {            // 读取 db.properties 文件 ,路径是相对于项目的路径            InputStream  inputStream = JDBCUtilProperties.class.getClassLoader().                getResourceAsStream("com/xdl/util/db.properties");            /* Properties  就是一个key value 结构  */            Properties  pro = new Properties();            try {                pro.load(inputStream);                driverClassName = pro.getProperty("driverClassName");                url = pro.getProperty("url");                username = pro.getProperty("username");                password = pro.getProperty("password");            } catch (IOException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }            Class.forName(driverClassName);        } catch (ClassNotFoundException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }      /* 获取数据库 连接的方法  */    public  static  Connection   getConnection(){        Connection  conn  = null;        try {            conn = DriverManager.getConnection(url, username, password);        } catch (SQLException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        return  conn;    }    /* 释放资源的方法  */    public  static  void  releaseResource(Connection conn,Statement st,ResultSet rs){        if(rs != null){            try {                rs.close();            } catch (SQLException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }finally{                rs = null;            }        }        if(st != null){            try {                st.close();            } catch (SQLException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }finally{                st = null;            }        }        if(conn != null){            try {                conn.close();            } catch (SQLException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }finally{                conn = null;            }        }    }}
JDBCUtilProperties.java

Test.java

技术分享
package com.xdl.test;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.SQLException;import com.xdl.util.JDBCUtilProperties;public class Test {    /* 转账的案例         create  table   bankAccount(           id   number  primary key,           name  varchar2(30),           money   number);       insert  into  bankAccount values(1,‘name1‘,10000000);       insert  into  bankAccount values(2,‘name2‘,10000000);       commit;     */    public  static void   test(){        // 1.获取连接         Connection   conn  = null;        PreparedStatement   ps = null;        PreparedStatement   ps2 = null;        conn = JDBCUtilProperties.getConnection();        try {            //关闭自动提交            conn.setAutoCommit(false);            double  m = 1000000;            //从name1账户中扣钱            ps = conn.prepareStatement               ("update bankaccount set money=money-? where id=1 and name=‘name1‘");            ps.setDouble(1, m);            //得到影响的行数            int rows = ps.executeUpdate();            //往name2账户中加钱            ps2 = conn.prepareStatement                ("update bankaccount set money=money+? where id=2 and name=‘namw2‘");            ps2.setDouble(1, m);            //得到影响的行数            int rows2 = ps2.executeUpdate();            //如果两个语句得到影响行数都是1,那么提交事务            if(rows == 1 && rows2 == 1 ){                System.out.println("转账成功");                conn.commit();            }else{                //若不满足条件,则对两个语句回归                System.out.println("转账失败");                conn.rollback();            }        } catch (SQLException e) {            // TODO Auto-generated catch block            try {                conn.rollback();            } catch (SQLException e1) {                // TODO Auto-generated catch block                e1.printStackTrace();            }            e.printStackTrace();        }finally {            JDBCUtilProperties.releaseResource(conn, ps, null);            JDBCUtilProperties.releaseResource(conn, ps2, null);        }    }    public static void main(String[] args) {        test();    }}
Test.java

 

【JDBC】实现JDBC实现银行的转账事务