arsenal
arsenal

Reputation: 24184

AutoIncrement the column in Oracle using jdbc

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

Upvotes: 0

Views: 3571

Answers (2)

Ernesto Campohermoso
Ernesto Campohermoso

Reputation: 7381

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.

Upvotes: 4

Justin Cave
Justin Cave

Reputation: 231801

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

Upvotes: 2

Related Questions