在《java下OracleHelper包》 一文中,FuWaer给出了一个针对java访问oracle数据库的封装工具包,本篇文章通过若干实例来介绍如何应用这个工具包。文本所给出的实例的编译运行环境如下:
jdk1.5.0_12,eclipse-jee-europa-win32,oracle8.1.7
实例所使用的数据库表结构定义:
create
table
FRIEND ( SERIALNO
NUMBER
(
10
)
not
null
, FRIEND_NAME
VARCHAR2
(
50
)
not
null
, FRIEND_AGE
NUMBER
(
10
)
not
null
)
执行文本性sql语句
import
ey.db.type.
*
;
import
ey.db.oracle.
*
;
public
class
MyClient
...
{ public static void main(String[] args) ... { try ... { // 数据库连接参数 String connectionStr = " jdbc:oracle:thin:@192.168.1.192:1521:itdevdb;htecp;000000 " ; // 文本型sql语句 String commandText = " select * from friend " ; // 通过OracleHelper的函数执行sql语句 DataSet ds = OracleHelper.ExecuteDataSet(connectionStr, CommandType.Text, commandText); // 从结果集中获取结果数据 for ( int i = 0 ;i < ds.Tables[ 0 ].Rows.length;i ++ ) ... { int serialno = Integer.parseInt( ds.Tables[ 0 ].Rows[i].Columns[ 0 ].colValue.toString() ); String name = ds.Tables[ 0 ].Rows[i].Columns[ 1 ].colValue.toString(); int age = Integer.parseInt( ds.Tables[ 0 ].Rows[i].Columns[ 2 ].colValue.toString() ); System.out.println( " 编号: " + serialno + " 姓名: " + name + " 年龄: " + age); } } catch (Exception ex) ... { ex.printStackTrace(); } } }
运行结果:
编号:1 姓名:FuWaer1 年龄:10 编号:2 姓名:FuWaer2 年龄:20 编号:3 姓名:FuWaer3 年龄:30 编号:4 姓名:FuWaer4 年龄:40
执行存储过程
所调用的存储过程定义如下:
procedure
SplitFriendByAge( p_split_age
number
, p_down_friends out p_cursor, p_uper_friends out p_cursor )
is
begin
open
p_down_friends
for
select
*
from
friend
where
friend_age
<
p_split_age;
open
p_uper_friends
for
select
*
from
friend
where
friend_age
>
p_split_age;
end
;
其中,一个输入参数p_split_age,两个输出参数分别是p_down_friends、p_uper_friends,两个输出参数的类型p_cursor定义为:type p_cursor is ref cursor;
测试程序如下:
import
ey.db.type.
*
;
import
ey.db.oracle.
*
;
public
class
MyClient
...
{ public static void main(String[] args) ... { try ... { // 数据库连接参数 String connectionStr = " jdbc:oracle:thin:@192.168.1.192:1521:itdevdb;htecp;000000 " ; // 需要调用的存储过程 String commandText = " pac_log.SplitFriendByAge " ; // 为存储过程准备参数 Parameter p_split_age = new Parameter( " p_split_age " ,oracle.jdbc.OracleTypes.NUMBER, 25 ); Parameter p_down_friend = new Parameter( " p_down_friend " ,oracle.jdbc.OracleTypes.CURSOR, null ,ParameterDirection.OUT); Parameter p_uper_friend = new Parameter( " p_uper_friend " ,oracle.jdbc.OracleTypes.CURSOR, null ,ParameterDirection.OUT); // 通过OracleHelper的函数执行存储过程,传递参数 DataSet ds = OracleHelper.ExecuteDataSet( connectionStr, CommandType.StoreProcedure, commandText, p_split_age,p_down_friend,p_uper_friend); // 从结果集中获取结果数据 System.out.println( " 年龄<25的朋友列表 " ); DataTable dt_down_friend = ds.Tables[ 0 ]; // 第一个结果集 PrintResult(dt_down_friend); System.out.println( " 年龄>25的朋友列表 " ); DataTable dt_uper_friend = ds.Tables[ 1 ]; // 第二个结果集 PrintResult(dt_uper_friend); } catch (Exception ex) ... { ex.printStackTrace(); } } private static void PrintResult(DataTable dt) ... { for ( int i = 0 ;i < dt.Rows.length;i ++ ) ... { int serialno = Integer.parseInt( dt.Rows[i].Columns[ 0 ].colValue.toString() ); String name = dt.Rows[i].Columns[ 1 ].colValue.toString(); int age = Integer.parseInt( dt.Rows[i].Columns[ 2 ].colValue.toString() ); System.out.println( " 编号: " + serialno + " 姓名: " + name + " 年龄: " + age); } } }
运行结果:
年龄<25的朋友列表 编号:1 姓名:FuWaer1 年龄:10 编号:2 姓名:FuWaer2 年龄:20 年龄>25的朋友列表 编号:3 姓名:FuWaer3 年龄:30 编号:4 姓名:FuWaer4 年龄:40
事务控制
import
ey.db.type.
*
;
import
ey.db.oracle.
*
;
public
class
MyClient
...
{ public static void main(String[] args) ... { try ... { // 数据库连接参数 String connectionStr = " jdbc:oracle:thin:@192.168.1.192:1521:itdevdb;htecp;000000 " ; // 需要执行的sql语句 String commandText1 = " update friend set friend_age = friend_age +1 where friend_age < 25 " ; String commandText2 = " update friend set friend_age = friend_age +2 where friend_age > 25 " ; // 构建OracleConnection实例 OracleConnection connection = new OracleConnection(connectionStr); // 构建OracleTransaction实例 OracleTransaction transaction = connection.BeginTransaction(); try ... { OracleHelper.ExecuteNonQuery(transaction, CommandType.Text, commandText1); OracleHelper.ExecuteNonQuery(transaction, CommandType.Text, commandText2); // 提交事务 transaction.Commit(); } catch (Exception ex) ... { // 回滚事务 transaction.Rollback(); ex.printStackTrace(); } } catch (Exception ex) ... { ex.printStackTrace(); } } }