jdbc prepared statement :oracle sequence

Posted on 1999-06-22
Medium Priority
Last Modified: 2008-03-04
I am using a JDBC Prepared Statement to insert data into a table. I want to insert a sequnece value for a unique id using seq_tablename.nextval.
But it is taking this as a string and throwing an sql exception.
I tried by retrieving seq_tablename.nextval prior to the insertion only to face the following error:
ORA-01006 bind variable does not exist.
Help appreciated
Question by:maggari

Expert Comment

ID: 1086986
I'm sure JAVA experts will give you a straight answer. But, here are two workarounds:
- Instead of using insert in your JAVA program, write a stored procedure which will get the sequence value and use it for the inserted record
- write a BEFORE INSERT trigger that will use the sequence to assign the value to the inserted record's PK

Author Comment

ID: 1086987
In fact, I used a trigger and it worked fine. But I don't want to use a trigger.

Accepted Solution

gizmola earned 0 total points
ID: 1086988
Selecting from a sequence is no different than any other type of query, other than you need to specify the magic DUAL table, ala select seq_tablename.nextval from dual

I'm not java proficient, but figured you are already finding a way to bind result sets to variables at this point, and this issue would explain your problem.

Featured Post

Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and theā€¦
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
This video shows how to recover a database from a user managed backup
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question