Java用commit的原因,事务,配置文件,预处理DBUtils商品和用户交易金额【诗书画唱】

用commit的原因,和要用commit时的情况:
//当用了con1.setAutoCommit(false);
// 这个事务后,结尾要用上commit表示全部执行完后批量提交,
// 不加的话,SQL语句会执行,但不会改数据库的内容,
// 用了setAutoCommit(false);,结尾就要用commit();,
// 这两个是成对的。
// 不加【事务setAutoCommit(false);】时,就没必要加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{//类最先执行的地方
try {
//将创建的配置文件转化为字节流信息(类加载器读)
InputStream is=
DBUtils.class.getResourceAsStream("./database.properties");
Properties p=new Properties();
//字节流转为内容
p.load(is);
root=p.getProperty("root");
url=p.getProperty("url");
uname=p.getProperty("uname");
pwd=p.getProperty("pwd");
//1加载驱动
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("请执行商品购买操作,亲!\n");
System . out . println("用户编号为1的你请输入你花费的金额");
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);//执行购买的减少金额
DBUtils. ZSG(sql1,huafei);//执行购买的减少金额
ResultSet res1=DBUtils.Select("select * from yongHu");
ResultSet res2=DBUtils.Select("select * from shangDian");
if(res1.next()){
System. out. println("用户编号:"
+res1. getObject(1)+"\t 用户名:"
+res1. getObject(2)+" \t用户还拥有的钱:"
+res1. getObject(3)+"元");
}
if(res2.next()){
System. out. println("商店编号: "+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");
//————————————
//手动提交事务:
con. setAutoCommit(false);
//————————
// 就是加了con. setAutoCommit(false);
// 这句代码后,且ps1. executeUpdate();
//和ps2. executeUpdate() ;之间加入了
// System. out. println(10/0);
//这类会报错的句子时,
//ps1. executeUpdate();
//和ps2. executeUpdate() ;都不会执行。
// 之间不加入了System. out. println(10/0);
// 这类会报错的句子时,
// ps1. executeUpdate();
// 和ps2. executeUpdate() ;都会执行。
//如果不加con.setAutoCommit(false);
//这句代码,那么
//ps1. executeUpdate();
//和ps2. executeUpdate() ;之间加入了
// System. out. println(10/0);
//这类会报错的句子时,
//就只会执行ps1. executeUpdate();。
//这样就没有“一致性”(就是执行前和执行后,
//用户有的有的金额和商店有的金额和想等)
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();表示又执行了一次对应的SQL语句
// System. out . println("用户信息提交了");
// System. out . println(10/0);
// ps2. executeUpdate();
// System. out . println("商店信息提交了");
// ————————————————
con.commit();
}
}

自己总结的语法:
//XXX. setObject(第X个“?”, “?”的值);
当

时

当

且

时:

757没变:

540没变:

当

且

时

757减20变为737:

但540不变

两条数据不同时变和两条数据的和变了。

