2 Star 0 Fork 0

BESTI.IS.JAVA2018/20165337

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
Example11_7.java 1.81 KB
一键复制 编辑 原始数据 按行查看 历史
yueyuan 提交于 7年前 . week7
import java.sql.*;
public class Example11_7{
public static void main(String args[]){
Connection con = null;
Statement sql;
ResultSet rs;
String sqlStr;
con = GetDBConnection.connectDB("students","root","");
if(con == null ) return;
try{ float n = 0.02f;
con.setAutoCommit(false); //关闭自动提交模式
sql = con.createStatement();
sqlStr = "select name,height from mess where number='R1001'";
rs = sql.executeQuery(sqlStr);
rs.next();
float h1 = rs.getFloat(2);
System.out.println("事务之前"+rs.getString(1)+"身高:"+h1);
sqlStr = "select name,height from mess where number='R1002'";
rs = sql.executeQuery(sqlStr);
rs.next();
float h2 = rs.getFloat(2);
System.out.println("事务之前"+rs.getString(1)+"身高:"+h2);
h1 = h1-n;
h2 = h2+n;
sqlStr = "update mess set height ="+h1+" where number='R1001'";
sql.executeUpdate(sqlStr);
sqlStr = "update mess set height ="+h2+" where number='R1002'";
sql.executeUpdate(sqlStr);
con.commit(); //开始事务处理,如果发生异常直接执行catch块
con.setAutoCommit(true); //恢复自动提交模式
String s = "select name,height from mess"+
" where number='R1001'or number='R1002'";
rs =
sql.executeQuery(s);
while(rs.next()){
System.out.println("事务后"+rs.getString(1)+
"身高:"+rs.getFloat(2));
}
con.close();
}
catch(SQLException e){
try{ con.rollback(); //撤销事务所做的操作
}
catch(SQLException exp){}
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/BESTI-IS-JAVA-2018/20165337.git
git@gitee.com:BESTI-IS-JAVA-2018/20165337.git
BESTI-IS-JAVA-2018
20165337
20165337
master

搜索帮助