Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
![]() |
![]() |
Home -> Community -> Usenet -> c.d.o.misc -> Re: Transactions in JDBC with Oracle
[This followup was posted to comp.databases and a copy was sent to
scott_at_transactionengines.com if this address didn't require editing.]
In article <7sishk$3gp$1_at_bgtnsc02.worldnet.att.net>,
scott_at_transactionengines.com says...
> How do you specify a transaction in jdbc connecting to an oracle dbms?
> Let's say I want to have a unique number generated through sql. So, I have
> a table with a field called Count. I want to have one transaction with two
> sql statements. The first one is update count = count + 1 and the second
> one retrieves the count. This way every call gets a unique number.
>
> How do I specify this? I've tried resultSet =
> statement.executeQuery("UPDATE etc;SELECT etc");
> but this gives me an error..
>
> Thanks for any help with this. If anybody wants to become rich through a
> company with SOLID technology, we're located in downtown miami. Transaction
> Engines needs java programmers and oracle dbas. Very generous
> options/salary/benifiits for the right people!
>
int getCount(Connection con)
{
bool ac = con.getAutoCommit();
con.setAutoCommit(false);
try
{
Statement stmt = con.createStatement(); stmt.executeUpdate("update counttab set count = count + 1"); ResultSet rs = stmt.executeQuery("select count from counttab"); rs.next(); int cnt = rs.getInt(1); con.commit(); return(cnt);
con.rollback(); throw e;
con.setAutoCommit(ac);
}
}
Or if you don't mind being Oracle specific you could just use an Oracle sequence.
Regards,
Bob
--
Bob Withers Do or do not, there is no try bwit_at_pobox.com Yodahttp://www.pobox.com/~bwit
![]() |
![]() |