使用jdbc在Oracle中自动递增列
我想添加一个触发器,它将继续将自动增量列更新 1,因为我认为在 Oracle 中没有直接的方法来自动增量列。你必须使用序列,然后触发触发器,这将更新相应的列。所以我试图在 jdbc 中执行此操作。如何使用准备好的语句自动递增 jdbc 中的列。
String s1 = "create table crawler " +
"(id number NOT NULL PRIMARY KEY, " +
"url varchar(255) NOT NULL, " +
"urlHash varchar(255) NOT NULL, " +
"contentHash varchar(255), " +
"modDate varchar(50), " +
"contentLocation varchar(100), " +
"status integer, " +
"lastCrawlDate date) ";
String seq = "test_seq";
String s2 = "create sequence" + seq + " start with 1 increment by 1 nomaxvalue";
String s3 = "create or replace trigger inserttrigger before insert on test for each row begin select test_seq.nextval into :new.id from dual; end;";
stmt=conn.createStatement();
stmt.executeUpdate(s1);
stmt.executeUpdate(s2);
stmt.executeUpdate(s3);
ps = conn.prepareStatement (
"INSERT INTO crawler (id, url, urlHash, contentHash, modDate, contentLocation, status, lastCrawlDate) VALUES(?,?,?,?,?,?,?,?)");
//how to write this first value as I know you can increment the column using sequencename.nextval but how to do here in prepared statement.
ps.setString (1, seq.nextVal);
ps.setString (2, "http://www.google.com");
ps.setString (3, "swerrsdfsfdgfgrgthtyty");
ps.setString (4, "1a10407d9a7997531aabe53fb367efbc");
ps.setString (5, "1a10407d9a7997531aabe53fb367efbc");
ps.setString (6, "1a10407d9a7997531aabe53fb367efbc");
ps.setString (7, "302");
ps.setString (8, "2011-11-27");
I want to add a trigger that will keep on updating autoincrement column by 1 as I think there is no direct way to autoincrement the column in oracle. you have to use sequence and then fire the trigger and that will update the respective column.. So I was trying to do it in jdbc. How Can I autoincrement the column in jdbc using prepared statement.
String s1 = "create table crawler " +
"(id number NOT NULL PRIMARY KEY, " +
"url varchar(255) NOT NULL, " +
"urlHash varchar(255) NOT NULL, " +
"contentHash varchar(255), " +
"modDate varchar(50), " +
"contentLocation varchar(100), " +
"status integer, " +
"lastCrawlDate date) ";
String seq = "test_seq";
String s2 = "create sequence" + seq + " start with 1 increment by 1 nomaxvalue";
String s3 = "create or replace trigger inserttrigger before insert on test for each row begin select test_seq.nextval into :new.id from dual; end;";
stmt=conn.createStatement();
stmt.executeUpdate(s1);
stmt.executeUpdate(s2);
stmt.executeUpdate(s3);
ps = conn.prepareStatement (
"INSERT INTO crawler (id, url, urlHash, contentHash, modDate, contentLocation, status, lastCrawlDate) VALUES(?,?,?,?,?,?,?,?)");
//how to write this first value as I know you can increment the column using sequencename.nextval but how to do here in prepared statement.
ps.setString (1, seq.nextVal);
ps.setString (2, "http://www.google.com");
ps.setString (3, "swerrsdfsfdgfgrgthtyty");
ps.setString (4, "1a10407d9a7997531aabe53fb367efbc");
ps.setString (5, "1a10407d9a7997531aabe53fb367efbc");
ps.setString (6, "1a10407d9a7997531aabe53fb367efbc");
ps.setString (7, "302");
ps.setString (8, "2011-11-27");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不需要使用触发器。
在您准备好的语句中,sql 可以是:
因此您只需传递其余 7 个参数。
You don't need to use a trigger.
In your prepared statemen the sql can be:
So you only pass the remaining 7 parameters.
如果您创建触发器,则只需在
INSERT
中省略ID
列。或者,您可以跳过创建触发器并仅在 INSERT 中引用序列
If you create the trigger, you would simply omit the
ID
column from yourINSERT
Alternately, you can skip creating the trigger and simply reference the sequence in your INSERT