| > 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".