實驗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();
}
}
}
}
