实验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();
}
}
}
}

JAVA操作Oracle数据库中的事务的更多相关文章

  1. Java读取oracle数据库中blob字段数据文件保存到本地文件(转载)

    转自:https://www.cnblogs.com/forever2698/p/4747349.html package com.bo.test; import java.io.FileOutput ...

  2. Java操作Oracle数据库以及调用存储过程

    操作Oracle数据库 publicclass DBConnection {     //jdbc:oracle:thin:@localhost:1521:orcl     publicstaticf ...

  3. java向oracle数据库中插入当前时间

    public class Test{public static void main (String args []){ java.util.Date a = new java.util.Date(); ...

  4. Java 操作Oracle数据库

    import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import ...

  5. JAVA操作ORACLE数据库的存储过程

    一.任务提出 JAVA操作oracle11g存储过程实验需要完成以下几个实例: 1.调用没有返回参数的过程(插入记录.更新记录) 2.有返回参数的过程 3.返回列表的过程 4.返回带分页的列表的过程. ...

  6. Oracle 数据库中不同事务并发访问的问题

    现象 以SQL/Helper为例,打开不同的SQL窗口,对同一个表格进行操作,如下所示. 窗口1:当执行更新任务.紧接着执行查询时获得一组查询结果.结果是对的. 窗口2:而在另外一个SQL查询窗口中执 ...

  7. Oracle数据库中调用Java类开发存储过程、函数的方法

    Oracle数据库中调用Java类开发存储过程.函数的方法 时间:2014年12月24日  浏览:5538次 oracle数据库的开发非常灵活,不仅支持最基本的SQL,而且还提供了独有的PL/SQL, ...

  8. loadrunner 脚本开发-调用java jar文件远程操作Oracle数据库测试

    调用java jar文件远程操作Oracle数据库测试 by:授客 QQ:1033553122 测试环境 数据库:linux 下Oracle_11g_R2 Loadrunner:11 备注:想学ora ...

  9. Oracle 数据库中日期时间的插入操作

    Oracle 中如何插入日期时间类型的数据,首先为了演示, 新建数据表如下 create table t( mydate date); 插入日期时间 SQL> insert into t val ...

随机推荐

  1. 八数码问题(紫薯P199)

    #include <algorithm> #include <cstdio> #include <cstring> #include <iostream> ...

  2. 简单的angular表单验证指令

    <html ng-app="myApp"> <head> <meta charset="UTF-8"> <title& ...

  3. audio patch(10.9.3) [2.6.1]

    ; Basic Block Input Regs: rax -  Killed Regs: <nothing>0000000000048353 3D8419D411             ...

  4. eclipse安装springsource-tool-suite

    到http://spring.io/tools/sts/all找到安装的eclipse对应的springsource-tool-suite版本,复制下载的网址 然后在eclipse的install n ...

  5. JavaWeb学习笔记——访问静态HTML网页

    基础知识: 1.一个标准 的WEB应用,在根目录下必须有一个WEB-INF文件夹,文件夹下必须有一个WEB.xml文件. 2.classes文件夹用来存放编译的class文件. 3.lib文件夹用来存 ...

  6. Deep Learning in a Nutshell: History and Training

    Deep Learning in a Nutshell: History and Training This series of blog posts aims to provide an intui ...

  7. yourphp的sql语句

    1.插入单条数据 $data[0]['cardid'] = $_POST['cardid']; $data[0]['name'] = $_POST['name']; $data[0]['mobile' ...

  8. return columns.All(new Func<string, bool>(list.Contains));

    internal static bool VerifyColumns(SqlConnection conn, string table, params string[] columns)        ...

  9. shutdown命令用法

    首先我们先创建一个txt文件,添加shutdown -r -f -t 0 ,文件点击另存为,选择所有类型,保存格式为“重启.bat”文件. 说明:shutdown命令用法: /r         关闭 ...

  10. Spring的Bean之Bean的基本概念

    从前面我们知道spring其实就是一个大型的工厂,而Spring容器中的Bean就是该工厂的产品.对于Spring容器能够生产那些产品,则取决于配置文件中配置. 对于我们而言,我们使用Spring框架 ...