我想添加一个触发器,它将继续将autoincrement列更新为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");
答案 0 :(得分:4)
您无需使用触发器。
在准备好的语句中,sql可以是:
String insert = "INSERT INTO crawler (id, url, urlHash, contentHash, modDate, contentLocation, status, lastCrawlDate)"
+ " VALUES(test_seq.nextval,?,?,?,?,?,?,?)";
所以你只传递剩余的7个参数。
答案 1 :(得分:2)
如果您创建了触发器,则只需省略ID
INSERT
列
ps = conn.prepareStatement (
"INSERT INTO crawler (url, urlHash, contentHash, modDate, contentLocation, status, lastCrawlDate) VALUES(?,?,?,?,?,?,?)");
ps.setString (1, "http://www.google.com");
ps.setString (2, "swerrsdfsfdgfgrgthtyty");
ps.setString (3, "1a10407d9a7997531aabe53fb367efbc");
ps.setString (4, "1a10407d9a7997531aabe53fb367efbc");
ps.setString (5, "1a10407d9a7997531aabe53fb367efbc");
ps.setString (6, "302");
ps.setString (7, "2011-11-27");
或者,您可以跳过创建触发器并简单地引用INSERT中的序列
ps = conn.prepareStatement (
"INSERT INTO crawler (id, url, urlHash, contentHash, modDate, contentLocation, status, lastCrawlDate) VALUES(test_seq.nextval,?,?,?,?,?,?,?)");
ps.setString (1, "http://www.google.com");
ps.setString (2, "swerrsdfsfdgfgrgthtyty");
ps.setString (3, "1a10407d9a7997531aabe53fb367efbc");
ps.setString (4, "1a10407d9a7997531aabe53fb367efbc");
ps.setString (5, "1a10407d9a7997531aabe53fb367efbc");
ps.setString (6, "302");
ps.setString (7, "2011-11-27");