Thank you!

This was exactly what I was looking for. I had overlooked that the CallableStatement
actually implements the PreparedStatement interface.

There are many discussion threads about how to generate primary keys for ejb's, but 
most
of the solutions run into problem when it comes to clusted application servers, because
they ultimately needs the database to synchronize the generation of keys. I feel that
the solution below, where the database generates and returns the primary key in one 
call
is a very effecient solutions that also solves the clustering problem. Any comments on
that topic?


Again, thank you for the code example


// Dennis


Steve Muench wrote:

> | >     INSERT INTO yourtable(pk,y,z)
> | >     VALUES(yoursequence.nextval,?,?)
> | >     RETURNING pk INTO ?
>
> Dennis Djenfer wrote:
>
> | The SQL statement looks fine to me, but I don't know how to execute this statement
> | from JDBC.
>
> Here's an example program.
>
> import java.sql.Connection;
> import java.sql.Types;
> import java.sql.SQLException;
> import java.sql.CallableStatement;
> import java.sql.DriverManager;
> public class Example  {
>   private static final String STMT = "BEGIN INSERT INTO DEPT(deptno,dname,loc) "+
>                                      "VALUES(mydeptseq.nextval,?,?) "+
>                                      "RETURNING deptno INTO ?; END;";
>   public static void main(String[] args) {
>     Connection c  = null;
>     try {
>       Class.forName("oracle.jdbc.driver.OracleDriver");
>       c  = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL",
>                                        "scott","tiger");
>       CallableStatement s = c.prepareCall(STMT);
>       s.setString(1,"Example");
>       s.setString(2,"Sample");
>       s.registerOutParameter(3,Types.INTEGER);
>       s.execute();
>       int i = s.getInt(3);
>       System.out.println("Row inserted was assigned sequence number "+i);
>     }
>     catch(Throwable t) {
>       t.printStackTrace(System.err);
>     }
>     finally {
>       if (c!=null) try { c.close(); } catch(SQLException ignore){}
>     }
>   }
> }
>
> _____________________________________________________________________
> Steve Muench - Developer, Product Manager, XML Evangelist, Author
> "Building Oracle XML Applications" - www.oreilly.com/catalog/orxmlapp
>

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff EJB-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

Reply via email to