JDBC getgeneratedKeys

Besides the getgeneratedKeys method, the returning clause of insert method can also be accessed using JDBC.
For reference I'm providing the following code :
/******************************************/

CREATE TABLE TTTTT
(
id NUMBER,
description VARCHAR2(50)
)

ALTER TABLE TTTTT ADD (
PRIMARY KEY (id));

<Create a trigger for generating PK>

CREATE OR REPLACE TRIGGER tttttt
BEFORE INSERT
ON TTTTT
REFERENCING NEW AS New OLD AS Old
FOR EACH ROW
DECLARE
tmpVar NUMBER;

BEGIN
tmpVar := 0;

SELECT MySeq.NEXTVAL INTO tmpVar FROM dual;
:NEW.id:= tmpVar;

EXCEPTION
WHEN OTHERS THEN
-- Consider logging the error and then re-raise
RAISE;
END tttttt;

----------------------------------------------------------
<Java Code >

<Using returning clause of insert statement >

String sql="begin insert into t1(description) values (?) returning id into ? ; end; ";
Connection con= <get Ur connection>;
CallableStatement pstmt = con.prepareCall(sql);
pstmt.setString(1,"Some description");
pstmt.registerOutParameter(2,Types.VARCHAR);
succ = pstmt.executeUpdate()>0 ;
// pstmt.execute();
int generatedkey = pstmt.getInt(2) ;

<Using getGeneratedKeys().....>

String sql="insert into t1(description) values (?) ";
Connection con= <get Ur connection>;
PreparedStatement pstmt = con.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
pstmt.setString(1,"Some description");
succ = pstmt.executeUpdate()>0 ;
// pstmt.execute();
ResultSet rset = pstmt.getGeneratedKeys();
ResultSetMetaData rsmd=rset.getMetaData();
int i1=0;
for (;i1<rsmd.getColumnCount();i1++) {
System.out.print(rsmd.getColumnName(i1+1)+"/t");
}
System.out.println();
while (rset.next()) {
for(int j=0;j<i1;j++) {
// System.out.print( rset.getString((j+1)) + "/t ");

Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(" select id,description from TTTTT where rowid='"+rset.getString((1))+"'");

while (rs.next()) {
System.out.println(rs.getInt(1));
}

}

------------------------------------------------------------------------
Hope that will help if somebody has a similar requirement.

P.S. Don't forget to use ojdbc14.jar ver ( 10.2.0.something)
The above code doesn't work for 10.1 version

Also, You can check your version and getGeneratedkeys support thru' the foll. code:
DatabaseMetaData dbmd = con.getMetaData();

System.out.println("===== Database info =====");
System.out.println("DatabaseProductName: " + dbmd.getDatabaseProductName() );
System.out.println("DatabaseProductVersion: " + dbmd.getDatabaseProductVersion() );
System.out.println("DatabaseMajorVersion: " + dbmd.getDatabaseMajorVersion() );
System.out.println("DatabaseMinorVersion: " + dbmd.getDatabaseMinorVersion() );
System.out.println("===== Driver info =====");
System.out.println("DriverName: " + dbmd.getDriverName() );
System.out.println("DriverVersion: " + dbmd.getDriverVersion() );
System.out.println("DriverMajorVersion: " + dbmd.getDriverMajorVersion() );
System.out.println("DriverMinorVersion: " + dbmd.getDriverMinorVersion() );
System.out.println("===== JDBC/DB attributes =====");
System.out.print("Supports getGeneratedKeys(): "+dbmd.supportsGetGeneratedKeys()+"/n");

--------------------------------------------------------------

Message was edited by:
coomar

 

 

http://www.oracle.com/technology/software/tech/java/sqlj_jdbc/htdocs/jdbc_10201.html

Weblogic Server Lib path    bea/weblogic81/server/lib

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值