"JDBC" implements JDBC to implement bank transfer transactions

Source: Internet
Author: User

Transactions in JDBC are committed by default, meaning that the data is written to disk every time PreparedStatement is executed. If you need to close the default commit, use void setautocommit (false) .

Db.properties

Driverclassname=oracle.jdbc.OracleDriverurl=jdbc:oracle:thin: @localhost: 1521: xeusername=  Systempassword=517839
db.properties

Jdbcutilproperties.java

 PackageCom.xdl.util;Importjava.io.IOException;ImportJava.io.InputStream;Importjava.sql.Connection;ImportJava.sql.DriverManager;ImportJava.sql.ResultSet;Importjava.sql.SQLException;Importjava.sql.Statement;Importjava.util.Properties; Public classjdbcutilproperties { Public  StaticString Driverclassname;  Public  StaticString URL;  Public  StaticString username;  Public  StaticString password; Static {        Try {            //read the Db.properties file, the path is relative to the project's pathInputStream InputStream = jdbcutilproperties.class. getClassLoader (). getResourceAsStream ("Com/xdl/util/db.properties"); /*Properties is a key value structure*/Properties Pro=NewProperties (); 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 blockE.printstacktrace ();        } class.forname (Driverclassname); } Catch(ClassNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); }    }      /*How to get a database connection*/     Public  StaticConnection getconnection () {Connection conn=NULL; Try{conn=drivermanager.getconnection (URL, username, password); } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); }        returnConn; }    /*ways to release resources*/     Public  Static  voidReleaseresource (Connection conn,statement st,resultset rs) {if(rs! =NULL){            Try{rs.close (); } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); }finally{RS=NULL; }        }        if(St! =NULL){            Try{st.close (); } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); }finally{St=NULL; }        }        if(Conn! =NULL){            Try{conn.close (); } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); }finally{conn=NULL; }        }    }}
Jdbcutilproperties.java

Test.java

 Packagecom.xdl.test;Importjava.sql.Connection;Importjava.sql.PreparedStatement;Importjava.sql.SQLException;Importcom.xdl.util.JDBCUtilProperties; Public classTest {/*Case of Transfer 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 voidTest () {//1. Get the connectionConnection conn =NULL; PreparedStatement PS=NULL; PreparedStatement PS2=NULL; Conn=jdbcutilproperties.getconnection (); Try {            //Turn off auto-commitConn.setautocommit (false); Doublem = 1000000; //deduct money from your name1 accountPS =Conn.preparestatement ("Update bankaccount set money=money-?" where id=1 and Name= ' name1 ' "); Ps.setdouble (1, M); //number of rows that are affected            introws =ps.executeupdate (); //add money to your name2 accountPS2 =Conn.preparestatement ("Update bankaccount set money=money+?" where id=2 and Name= ' NAMW2 ' "); Ps2.setdouble (1, M); //number of rows that are affected            intROWS2 =ps2.executeupdate (); //If two statements are affected by the number of rows that are 1, the COMMIT transaction            if(rows = = 1 && rows2 = = 1) {System.out.println ("Successful transfer");            Conn.commit (); }Else{                //if the condition is not met, return to two statementsSystem.out.println ("Transfer failed");            Conn.rollback (); }        } Catch(SQLException e) {//TODO auto-generated Catch block            Try{conn.rollback (); } Catch(SQLException E1) {//TODO auto-generated Catch blockE1.printstacktrace ();        } e.printstacktrace (); }finally{jdbcutilproperties.releaseresource (conn, PS,NULL); Jdbcutilproperties.releaseresource (conn, PS2,NULL); }    }     Public Static voidMain (string[] args) {test (); }}
Test.java

"JDBC" implements JDBC to implement bank transfer transactions

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.