【JDBC】實現JDBC實現銀行的轉賬事務
阿新 • • 發佈:2017-05-28
str package b- exceptio hide use play key rgs
JDBC中的事務是默認提交的,也就是說每執行一次PreparedStatement,那麽數據就會被寫入到磁盤。如果需要關閉默認提交,使用 void setAutoCommit(false) .
db.properties
driverClassName=oracle.jdbc.OracleDriver url=jdbc:oracle:thin:@localhost:1521:xe username=system password=517839db.properties
JDBCUtilProperties.java
package com.xdl.util;JDBCUtilProperties.javaimport 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; } } } }
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實現銀行的轉賬事務