使用jdbc在Oracle中自动递增列

发布于 2024-12-11 15:30:04 字数 1937 浏览 0 评论 0原文

我想添加一个触发器,它将继续将自动增量列更新 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白况 2024-12-18 15:30:04

您不需要使用触发器。

在您准备好的语句中,sql 可以是:

String insert = "INSERT INTO crawler (id, url, urlHash, contentHash, modDate, contentLocation, status, lastCrawlDate)" 
+ " VALUES(test_seq.nextval,?,?,?,?,?,?,?)";

因此您只需传递其余 7 个参数。

You don't need to use a trigger.

In your prepared statemen the sql can be:

String insert = "INSERT INTO crawler (id, url, urlHash, contentHash, modDate, contentLocation, status, lastCrawlDate)" 
+ " VALUES(test_seq.nextval,?,?,?,?,?,?,?)";

So you only pass the remaining 7 parameters.

清眉祭 2024-12-18 15:30:04

如果您创建触发器,则只需在 INSERT 中省略 ID 列。

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");

If you create the trigger, you would simply omit the ID column from your 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");

Alternately, you can skip creating the trigger and simply reference the sequence in your 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");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文