当向Oracle数据库中插入数据时,如何获取行ID
2010-06-07 10:50
519 查看
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)
);
/**//*
* File name: TestInsertReturnRowId.java
*
* Version: v1.0
*
* Created on Aug 20, 2008 8:56:07 AM
*
* Designed by Stephen
*
* (c)Copyright 2008
*/
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;
/** *//**
* @author Stephen
*
* 测试调用JDBC,往Oracle中插入数据,返回对应的ROWID
*/
public class TestInsertReturnRowId
{
private static final Log log = LogFactory
.getLog(TestInsertReturnRowId.class);
public static void main(String[] args)
{
TestInsertReturnRowId tester = new TestInsertReturnRowId();
String rowId = tester.insertUser("Stephen", "liwp");
System.out.println("The rowId is:" + rowId);
}
public String insertUser(String userId, String userName)
{
if (StringUtils.isEmpty(userId) || StringUtils.isEmpty(userName))
{
log.error("Please specify the userId and userName");
return null;
}
// check whether the user has already in the database
String querySQL = "select count(1) as cnt from FI_T_USER where USER_ID = '"
+ userId + "'";
// insert statement
String insertSQL = "begin insert into FI_T_USER(USER_ID, USER_NAME) values(?,?) return rowid into ?;end;";
Connection con = DBConnectionPool.getConnection("test");
if (con == null)
{
log.error("Error on get the connection!");
return null;
}
try
{
int rowCount = SqlQueryUtils.getIntValue(querySQL, con);
if (rowCount != 0)
{
log.error("User with userId = " + userId + " already exists!");
return null;
}
// insert the data to the database
CallableStatement cs = con.prepareCall(insertSQL);
cs.setString(1, userId);
cs.setString(2, userName);
cs.registerOutParameter(3, OracleTypes.VARCHAR);
cs.execute();
String rowId = cs.getString(3);
return rowId;
} catch (SQLException e)
{
e.printStackTrace();
} catch (SDBException e)
{
e.printStackTrace();
} finally
{
if (con != null)
{
try
{
con.close();
} catch (SQLException e)
{
e.printStackTrace();
}
}
}
return null;
}
}
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:/**//*
* File name: TestInsertReturnRowId.java
*
* Version: v1.0
*
* Created on Aug 20, 2008 8:56:07 AM
*
* Designed by Stephen
*
* (c)Copyright 2008
*/
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;
/** *//**
* @author Stephen
*
* 测试调用JDBC,往Oracle中插入数据,返回对应的ROWID
*/
public class TestInsertReturnRowId
{
private static final Log log = LogFactory
.getLog(TestInsertReturnRowId.class);
public static void main(String[] args)
{
TestInsertReturnRowId tester = new TestInsertReturnRowId();
String rowId = tester.insertUser("Stephen", "liwp");
System.out.println("The rowId is:" + rowId);
}
public String insertUser(String userId, String userName)
{
if (StringUtils.isEmpty(userId) || StringUtils.isEmpty(userName))
{
log.error("Please specify the userId and userName");
return null;
}
// check whether the user has already in the database
String querySQL = "select count(1) as cnt from FI_T_USER where USER_ID = '"
+ userId + "'";
// insert statement
String insertSQL = "begin insert into FI_T_USER(USER_ID, USER_NAME) values(?,?) return rowid into ?;end;";
Connection con = DBConnectionPool.getConnection("test");
if (con == null)
{
log.error("Error on get the connection!");
return null;
}
try
{
int rowCount = SqlQueryUtils.getIntValue(querySQL, con);
if (rowCount != 0)
{
log.error("User with userId = " + userId + " already exists!");
return null;
}
// insert the data to the database
CallableStatement cs = con.prepareCall(insertSQL);
cs.setString(1, userId);
cs.setString(2, userName);
cs.registerOutParameter(3, OracleTypes.VARCHAR);
cs.execute();
String rowId = cs.getString(3);
return rowId;
} catch (SQLException e)
{
e.printStackTrace();
} catch (SDBException e)
{
e.printStackTrace();
} finally
{
if (con != null)
{
try
{
con.close();
} catch (SQLException e)
{
e.printStackTrace();
}
}
}
return null;
}
}
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.
相关文章推荐
- mybatis如何获取oracle新插入数据记录的主键?
- PHP如何获取最后插入数据的auto_id
- SQL server插入数据后,如何获取自增长字段的值?
- .net实现oracle数据库中获取新插入数据的id的方法
- mybatis 在oracle数据库中插入数据时获取自增ID
- 如何获取自增长列(标识列)的ID,并写入另一张表(多对多关系插入数据示例)
- SQLServer 如何获取刚插入数据的Id号
- 如何往oracle数据库中插入100w条数据【方案一:hibernate批量处理】和hibernate框架的学习网址
- oracle数据库表中,插入数据的时候如何产生一个 字母+数字 编号?
- mybatis 在oracle数据库中插入数据时获取自增ID
- SQLServer 如何获取刚插入数据的Id号
- 如何获取数据库新插入数据的主键id
- .net实现oracle数据库中获取新插入数据的id的方法
- (原) ODP.NET 演示如何从DataSet中获取LOB数据 附:.NET 环境下连接Oracle数据库
- 如何获取自增长列(标识列)的ID,并写入另一张表(多对多关系插入数据示例)
- SQLServer 如何获取刚插入数据的Id号
- jsp中如何将表单中获取的内容通过自己声明的变量插入到数据表中
- SQL server插入数据后,如何获取自增长字段的值
- mybatis如何获取oracle新插入数据记录的主键?