How to get the rowid when insert the data to Oracle database
2008-08-20 11:59
621 查看
In my request, I need to get the rowid after insert the data to the oracle database. Oracle has an insert with returning clause, the gramer is:
INSERT INTO <table_name>
(column_list)
VALUES
(values_list)
RETURNING <value_name>
INTO <variable_name>;
How to get the rowid when insert the data to the database?
In JDBC, can use the CallbackStatement to run the Procedure, so we can generate the CallbackStatement from Connection object, and execute the insert sql, then get the return code from the statement Object. The key point is how to write the insert statement? and how to call the statement and how to get the return code. The following is my test code.
create table FI_T_USER(
USER_ID varchar2(20) primary key,
USER_NAME varchar2(100)
);
package test.com.sinosoft.database;
import java.sql.*;
import oracle.jdbc.OracleTypes;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.sinosoft.database.DBConnectionPool;
import com.sinosoft.database.SqlQueryUtils;
import com.sinosoft.exception.SDBException;
The important code is specify the insertSQL, which is:
String insertSQL = "begin insert into FI_T_USER(USER_ID, USER_NAME) values(?,?) return rowid into ?;end;";
Then the key point is register the out parameter, and get the out parameter after execute the statement.
It's very useful, in some web site said that the this statement can only run start on Oracle 10, but in my tester database is Oracle 9.2, it can run the statement and get the return value.
INSERT INTO <table_name>
(column_list)
VALUES
(values_list)
RETURNING <value_name>
INTO <variable_name>;
How to get the rowid when insert the data to the database?
In JDBC, can use the CallbackStatement to run the Procedure, so we can generate the CallbackStatement from Connection object, and execute the insert sql, then get the return code from the statement Object. The key point is how to write the insert statement? and how to call the statement and how to get the return code. The following is my test code.
Create the test database
create an table FI_T_USER, which contain two columns, the first column is the primary key USER_ID, and the second column is USER_NAME. The create statement is as following:create table FI_T_USER(
USER_ID varchar2(20) primary key,
USER_NAME varchar2(100)
);
Write the test code
The following is my test code:package test.com.sinosoft.database;
import java.sql.*;
import oracle.jdbc.OracleTypes;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.sinosoft.database.DBConnectionPool;
import com.sinosoft.database.SqlQueryUtils;
import com.sinosoft.exception.SDBException;
The important code is specify the insertSQL, which is:
String insertSQL = "begin insert into FI_T_USER(USER_ID, USER_NAME) values(?,?) return rowid into ?;end;";
Then the key point is register the out parameter, and get the out parameter after execute the statement.
It's very useful, in some web site said that the this statement can only run start on Oracle 10, but in my tester database is Oracle 9.2, it can run the statement and get the return value.
相关文章推荐
- How to get the data from a cell when I click on the GridButtonColumn of the same row
- How to get data from Oracle DB in silverlight via WCF ?
- How to fix the problem that the Download rate is too Slow when doing "apt-get install"
- How to get the Diagnostic data and debug file of Sales Order pick release
- C++ How to avoid the enter key when using cin and getline
- How to get the query statement of LOV in Oracle Form
- *****How to scroll in the grid when editing data in a cell
- How to get a notification from Linux when the set of network interfaces changes
- Oracle table fragmentation how to calculate or get the actual used blocks of the table
- Tell you how to resolve Oracle 10g EM login problem: The database status is currently unavailable.
- How to Get the Frequency Table of a Categorical Variable as a Data Frame in R
- how to get the space size of some tables in one database?
- How to get the SQL Server name (Database server)?
- [WPF] How to bind to data when the DataContext is not inherited
- [eZ publish] When a ezxml embed an video, how to get the ezxml object in the video embed template?
- How to bind to data when the DataContext is not inherited【项目】
- How to display the data from database on JTable
- How to get the oracle server version?
- How to get past the “please insert the disc labele
- How to get the control inside the ControlTemplate/DataTemplate on run time.