代码拉取完成,页面将自动刷新
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){}
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。