首页 技术 正文
技术 2022年11月21日
0 收藏 663 点赞 3,881 浏览 3003 个字

实验1:

create table yggz(code int, salary number(7,2));
insert into yggz values(1, 1000);
insert into yggz values(2, 150);
commit;

完成任务:

如果1号员工的salary多余300元,则从1号员工的salary中减少300元,同时加到2号员工的salary上。

实验2:

create table yggz(code int, salary number(7,2));
insert into yggz values(1, 1000);
insert into yggz values(2, 150);
commit;

完成任务:

如果1号员工的salary 多余300元,则从1号员工的salary中减少300元,同时加到2号员工的salary上,但是还要确保转账后1号员工的salary多于转账后的2号员工的salary。

package com.oaj;import java.sql.*;public class TestJdbcOdbc {String driver="oracle.jdbc.driver.OracleDriver";
String strUrl="jdbc:oracle:thin:@localhost:1521:orcl";
Statement stmt=null;
ResultSet rs=null;
Connection conn=null;
CallableStatement cstmt=null;
float salary=0;
float salary2=0;
String sqlStr=null;
PreparedStatement ps=null;
public static void main(String[] args)
{
new TestJdbcOdbc().test2();
}
public void test1()
{
try
{
Class.forName(driver);
conn=DriverManager.getConnection(strUrl,"scott","scott");
conn.setAutoCommit(false);
//得到1号与昂工的工资
sqlStr="select salary from yggz where code=1";
ps=conn.prepareStatement(sqlStr);
rs=ps.executeQuery();
while(rs.next())
{
salary=rs.getFloat(1);
}
if(salary<300)
{
throw new RuntimeException("小于300元,不能转账");
}
sqlStr="update yggz set salary=salary-300 where code=1";
ps=conn.prepareStatement(sqlStr);
ps.executeUpdate(sqlStr);sqlStr="update yggz set salary=salary+300 where code=2";
ps=conn.prepareStatement(sqlStr);
ps.executeUpdate();conn.commit();
System.out.println("---成功!");}
catch(SQLException ex)
{
if(conn!=null)
{
try
{
conn.rollback();
System.out.println("失败");}
catch(Exception ex2)
{
ex2.printStackTrace();
}}
}
catch(Exception ex)
{
ex.printStackTrace();
}
finally
{
try
{
if(rs!=null)
{
rs.close();}
if(ps!=null)
{
ps.close();
}
if(conn!=null)
{
conn.close();
conn=null;
}
}
catch(SQLException ex)
{
ex.printStackTrace();
}
}
}
public void test2()
{
try
{
Class.forName(driver);
conn=DriverManager.getConnection(strUrl,"scott","scott");
conn.setAutoCommit(false);
//得到1号与昂工的工资
sqlStr="select salary from yggz where code=1";
ps=conn.prepareStatement(sqlStr);
rs=ps.executeQuery();
while(rs.next())
{
salary=rs.getFloat(1);
}
if(salary<300)
{
throw new RuntimeException("小于300元,不能转账");
}
//设置一个保存点
Savepoint point1=conn.setSavepoint("Point1");sqlStr="update yggz set salary=salary-300 where code=1";
ps=conn.prepareStatement(sqlStr);
ps.executeUpdate(sqlStr);sqlStr="update yggz set salary=salary+300 where code=2";
ps=conn.prepareStatement(sqlStr);
ps.executeUpdate();//再次取一号员工工资和二号员工的工资
sqlStr="select salary from yggz where code=1";
ps=conn.prepareStatement(sqlStr);
rs=ps.executeQuery();
while(rs.next())
{
salary=rs.getFloat(1);
}sqlStr="select salary from yggz where code=2";
ps=conn.prepareStatement(sqlStr);
rs=ps.executeQuery();
while(rs.next())
{
salary2=rs.getFloat(1);
}if(!(salary>salary2))
{
conn.rollback(point1);
System.out.println("转账失败!");
}
else
{
conn.commit();
System.out.println("---成功!");
}conn.commit();}
catch(SQLException ex)
{
if(conn!=null)
{
try
{
conn.rollback();
System.out.println("失败");}
catch(Exception ex2)
{
ex2.printStackTrace();
}}
}
catch(Exception ex)
{
ex.printStackTrace();
}
finally
{
try
{
if(rs!=null)
{
rs.close();}
if(ps!=null)
{
ps.close();
}
if(conn!=null)
{
conn.close();
conn=null;
}
}
catch(SQLException ex)
{
ex.printStackTrace();
}
}
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,992
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,506
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,349
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,134
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,767
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,844