使用java将插入的行获取到Oracle
我正在构建一个java程序,将数据插入oracle数据库。 我的问题是,我需要插入到两个表中,并在序列中的insert get next val之前,达到我在表中使用的唯一行。 但我需要表_B的相同id进行连接。 (我无法获取getval,因为如果其他用户使用该程序会怎么样…) 所以我需要知道,当我使用executeql(sql)命令作为回报时,我看到了我提交的内容 现在我使用我有名字和日期的id,所以我选择名字和日期是刚刚插入的id。 但这不是最好的,因为一天之内我可以插入更多的名字。所以现在这不是唯一的。 比如: 此处的id按顺序自动递增 而不是另一个sql运行:使用java将插入的行获取到Oracle,java,sql,oracle,Java,Sql,Oracle,我正在构建一个java程序,将数据插入oracle数据库。 我的问题是,我需要插入到两个表中,并在序列中的insert get next val之前,达到我在表中使用的唯一行。 但我需要表_B的相同id进行连接。 (我无法获取getval,因为如果其他用户使用该程序会怎么样…) 所以我需要知道,当我使用executeql(sql)命令作为回报时,我看到了我提交的内容 现在我使用我有名字和日期的id,所以我选择名字和日期是刚刚插入的id。 但这不是最好的,因为一天之内我可以插入更多的名字。所以现在
inert into table_b ( id,someval) val ( select id from table_a where
name ='Ryan', date='2014.01.01, 23)
所以我需要像这样的东西:
system.out.println(smtp.executesql(sql).whatIinsertednow())
*than console:* '1 row insered (id,name,date) : ( 1, Ryan, 2014.01.01)
您可以通过在INSERT语句中使用子句来实现这一点:
INSERT INTO table_a ( name,date) val ( 'Ryan','2014.01.01') RETURNING id INTO ?
您可以通过在INSERT语句中使用子句来实现这一点:
INSERT INTO table_a ( name,date) val ( 'Ryan','2014.01.01') RETURNING id INTO ?
PreparedStatement prepareStatement=connection.prepareStatement(“插入…” 我已经找到了答案,这是完美的作品。我可以插入从JAVA和它的返回键 完整版本:
CREATE TABLE STUDENTS
(
STUDENT_ID NUMBER NOT NULL PRIMARY KEY,
NAME VARCHAR2 (50 BYTE),
EMAIL VARCHAR2 (50 BYTE),
BIRTH_DATE DATE
);
CREATE SEQUENCE STUDENT_SEQ
START WITH 0
MAXVALUE 9999999999999999999999999999
MINVALUE 0;
还有Java代码
String QUERY = "INSERT INTO students "+
" VALUES (student_seq.NEXTVAL,"+
" 'Harry', 'harry@hogwarts.edu', '31-July-1980')";
// load oracle driver
Class.forName("oracle.jdbc.driver.OracleDriver");
// get database connection from connection string
Connection connection = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:sample", "scott", "tiger");
// prepare statement to execute insert query
// note the 2nd argument passed to prepareStatement() method
// pass name of primary key column, in this case student_id is
// generated from sequence
PreparedStatement ps = connection.prepareStatement(QUERY,
new String[] { "student_id" });
// local variable to hold auto generated student id
Long studentId = null;
// execute the insert statement, if success get the primary key value
if (ps.executeUpdate() > 0) {
// getGeneratedKeys() returns result set of keys that were auto
// generated
// in our case student_id column
ResultSet generatedKeys = ps.getGeneratedKeys();
// if resultset has data, get the primary key value
// of last inserted record
if (null != generatedKeys && generatedKeys.next()) {
// voila! we got student id which was generated from sequence
studentId = generatedKeys.getLong(1);
}
}
来源:PreparedStatement prepareStatement=connection.prepareStatement(“插入…”) 我已经找到了答案,这是完美的作品。我可以插入从JAVA和它的返回键 完整版本:
CREATE TABLE STUDENTS
(
STUDENT_ID NUMBER NOT NULL PRIMARY KEY,
NAME VARCHAR2 (50 BYTE),
EMAIL VARCHAR2 (50 BYTE),
BIRTH_DATE DATE
);
CREATE SEQUENCE STUDENT_SEQ
START WITH 0
MAXVALUE 9999999999999999999999999999
MINVALUE 0;
还有Java代码
String QUERY = "INSERT INTO students "+
" VALUES (student_seq.NEXTVAL,"+
" 'Harry', 'harry@hogwarts.edu', '31-July-1980')";
// load oracle driver
Class.forName("oracle.jdbc.driver.OracleDriver");
// get database connection from connection string
Connection connection = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:sample", "scott", "tiger");
// prepare statement to execute insert query
// note the 2nd argument passed to prepareStatement() method
// pass name of primary key column, in this case student_id is
// generated from sequence
PreparedStatement ps = connection.prepareStatement(QUERY,
new String[] { "student_id" });
// local variable to hold auto generated student id
Long studentId = null;
// execute the insert statement, if success get the primary key value
if (ps.executeUpdate() > 0) {
// getGeneratedKeys() returns result set of keys that were auto
// generated
// in our case student_id column
ResultSet generatedKeys = ps.getGeneratedKeys();
// if resultset has data, get the primary key value
// of last inserted record
if (null != generatedKeys && generatedKeys.next()) {
// voila! we got student id which was generated from sequence
studentId = generatedKeys.getLong(1);
}
}
来源:您可以取回生成的ID值;您已经有了剩余的值?您可以取回生成的ID值;您已经有了剩余的值?但是我如何在java中获得它?(JDBC)看看,但是我如何在java中获得它?(JDBC)看看