Java用commit的原因,事務(wù),配置文件,預(yù)處理DBUtils商品和用戶交易金額【詩書畫唱】

用commit的原因,和要用commit時(shí)的情況:
//當(dāng)用了con1.setAutoCommit(false);
// 這個(gè)事務(wù)后,結(jié)尾要用上commit表示全部執(zhí)行完后批量提交,
// 不加的話,SQL語句會(huì)執(zhí)行,但不會(huì)改數(shù)據(jù)庫的內(nèi)容,
// 用了setAutoCommit(false);,結(jié)尾就要用commit();,
// 這兩個(gè)是成對(duì)的。
// 不加【事務(wù)setAutoCommit(false);】時(shí),就沒必要加commit。


商品和用戶交易金額的程序:
create table shangDian(
shangDianId int primary key identity(1, 1),
--shangDianMoney:商店原來就有的本錢
shangDianName? nvarchar (20) ,
shangDianMoney int
)
create table yongHu(
-- yongHuMoney:用戶原來就有的本錢
?yongHuId int primary key identity(1, 1),
?yongName? nvarchar (20) ,
?yongHuMoney int
)
insert into shangDian values('詩書畫唱商店',520)
insert into shangDian values('詩書江唯商店',1314)
insert into yongHu values('嘉怡',777)
insert into yongHu values('畫唱',888)
--drop table shangDian
--drop table yonghu
--select* from? shangDian
--select* from? yonghu




package liZi;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
public class DBUtils {
public static Connection con=null;
public static ResultSet res=null;
public static PreparedStatement ps=null;
public static String uname,pwd,root,url;
static{//類最先執(zhí)行的地方
try {
//將創(chuàng)建的配置文件轉(zhuǎn)化為字節(jié)流信息(類加載器讀)
InputStream is=
DBUtils.class.getResourceAsStream("./database.properties");
Properties p=new Properties();
//字節(jié)流轉(zhuǎn)為內(nèi)容
p.load(is);
root=p.getProperty("root");
url=p.getProperty("url");
uname=p.getProperty("uname");
pwd=p.getProperty("pwd");
//1加載驅(qū)動(dòng)
Class.forName(root);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Connection getCon(){
if(con==null){
try {
con=DriverManager.getConnection(url,uname,pwd);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return con;
}
public static ResultSet Select(String sql,Object... o){
con=getCon();
try {
ps=con.prepareStatement(sql);
for(int i=0;i<o.length;i++){
ps.setObject(i+1,o[i]);
}
res=ps.executeQuery();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return res;
}
public static boolean ZSG(String sql,Object... o){
con=getCon();
boolean b=false;
try {
ps=con.prepareStatement(sql);
for(int i=0;i<o.length;i++){
ps.setObject(i+1,o[i]);
}
int num=ps.executeUpdate();
if(num>0){
b=true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return b;
}
}


package liZi;
import java.sql.ResultSet;
import java.util.*;
import java.io.*;
public class shopping{
public static void main(String[] args) throws Exception{
//
System.out . println("請(qǐng)執(zhí)行商品購買操作,親!\n");
System . out . println("用戶編號(hào)為1的你請(qǐng)輸入你花費(fèi)的金額");
Scanner s=new Scanner(System.in);
int huafei=s. nextInt();
//用戶減少金額
String sql="update yongHu set yongHuMoney-=?"
+ " where yongHuId=1";
//商店增加金額
String sql1="update shangDian set shangDianMoney+"
+ "=? where shangDianId=1";
DBUtils. ZSG(sql,huafei);//執(zhí)行購買的減少金額
DBUtils. ZSG(sql1,huafei);//執(zhí)行購買的減少金額
ResultSet res1=DBUtils.Select("select * from yongHu");
ResultSet res2=DBUtils.Select("select * from shangDian");
if(res1.next()){
System. out. println("用戶編號(hào):"
+res1. getObject(1)+"\t 用戶名:"
+res1. getObject(2)+" \t用戶還擁有的錢:"
+res1. getObject(3)+"元");
}
if(res2.next()){
System. out. println("商店編號(hào): "+res2. getObject(1)
+"\t商店名: "+res2. getObject(2)
+" \t商店還擁有的錢:"+res2. getObject(3)+"元");}
}
}


url=jdbc:sqlserver://localhost;databaseName=yonghu
uname=qqq
pwd=123
root=com.microsoft.sqlserver.jdbc.SQLServerDriver


————————————————————————

package liZi;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class shiWu {
public static void main(String[] args) throws Exception{
Class.forName("com.microsoft.sqlserver."
+ "jdbc.SQLServerDriver");
Connection con = DriverManager.getConnection(
"jdbc:sqlserver://localhost;databaseName=yonghu", "qqq",
"123");
//————————————
//手動(dòng)提交事務(wù):
con. setAutoCommit(false);
//————————
// 就是加了con. setAutoCommit(false);
// 這句代碼后,且ps1. executeUpdate();
//和ps2. executeUpdate() ;之間加入了
// System. out. println(10/0);
//這類會(huì)報(bào)錯(cuò)的句子時(shí),
//ps1. executeUpdate();
//和ps2. executeUpdate() ;都不會(huì)執(zhí)行。
// 之間不加入了System. out. println(10/0);
// 這類會(huì)報(bào)錯(cuò)的句子時(shí),
// ps1. executeUpdate();
// 和ps2. executeUpdate() ;都會(huì)執(zhí)行。
//如果不加con.setAutoCommit(false);
//這句代碼,那么
//ps1. executeUpdate();
//和ps2. executeUpdate() ;之間加入了
// System. out. println(10/0);
//這類會(huì)報(bào)錯(cuò)的句子時(shí),
//就只會(huì)執(zhí)行ps1. executeUpdate();。
//這樣就沒有“一致性”(就是執(zhí)行前和執(zhí)行后,
//用戶有的有的金額和商店有的金額和想等)
String updateSql1="update yongHu set yongHuMoney-=?"
+ " where yongHuId=?";
PreparedStatement ps1=con. prepareStatement(updateSql1);
String updateSql2="update shangDian set "
+ "shangDianMoney+=? where shangDianId=?";
PreparedStatement ps2=con.prepareStatement(updateSql2);
ps1. setObject(1, 20);ps1.setObject(2,1);
ps2. setObject(1, 20) ;ps2.setObject(2,1);
ps1. executeUpdate();
System . out . println("用戶信息修改了");
// System. out. println(10/0);
ps2. executeUpdate() ;
System . out . println("商店信息修改了");
String selectSql1="select * from yongHu";
String selectSql2="select * from shangDian";
PreparedStatement ps3=con.prepareStatement(selectSql1);
PreparedStatement ps4=con.prepareStatement(selectSql2);
ResultSet res1=ps3. executeQuery();
ResultSet res2=ps4.executeQuery();
if(res1. next()){
System.out.println(res1. getObject(1)+" "
+res1. getObject(2)+" "+res1. getObject(3));
}
if(res2.next()){
System.out.println(res2. getObject(1)+" "
+res2. getObject(2)+" "+res2. getObject(3));
}
//————————————————————
// ps1. executeUpdate();
//// XXX. executeUpdate();表示又執(zhí)行了一次對(duì)應(yīng)的SQL語句
// System. out . println("用戶信息提交了");
// System. out . println(10/0);
// ps2. executeUpdate();
// System. out . println("商店信息提交了");
// ————————————————
con.commit();
}
}

自己總結(jié)的語法:
//XXX. setObject(第X個(gè)“?”, “?”的值);
當(dāng)

時(shí)

當(dāng)

且

時(shí):

757沒變:

540沒變:

當(dāng)

且

時(shí)

757減20變?yōu)?37:

但540不變

兩條數(shù)據(jù)不同時(shí)變和兩條數(shù)據(jù)的和變了。

