Home » Developer & Programmer » JDeveloper, Java & XML » Bind variable in select statement
Bind variable in select statement [message #172474] Tue, 16 May 2006 13:17 Go to next message
akho
Messages: 2
Registered: May 2006
Junior Member
Hi,

I have a prepared statement: Select tablename.nextval from dual. where tablename is passed in to the method so we can use this as a utility. I'm converting this to use a bind variable:
stmt = conn.prepareStatement("select ? from dual");
stmt.setString(1, field); where field is tablename.nextval and this does not work.

Is there anything special that we have to do to bind select clause?

Thanks!
Re: Bind variable in select statement [message #172505 is a reply to message #172474] Tue, 16 May 2006 22:15 Go to previous messageGo to next message
oliversalmon
Messages: 5
Registered: March 2006
Junior Member
Not sure if that's allowed in JDBC, why not just create the statement String dynamically and use that variable in the call i.e.

String stmt = 'a statement';

//Evaluate conditions....
//Construct String....

stmt = conn.prepareStatement(stmt);
Regards,
Oliver
Re: Bind variable in select statement [message #172512 is a reply to message #172505] Wed, 17 May 2006 00:48 Go to previous messageGo to next message
hobbes
Messages: 173
Registered: January 2006
Senior Member
It looks like you're trying to get the next value from a database sequence.

You can use bind variables in the WHERE clause, but you cannot bind arguments for names of Oracle schema objects. In this case, simply prepare the string and execute it; do without the bind variable.

Tip: You can use sequence.NEXTVAL directly in the INSERT statement; there's no need to select its value into a variable first. If you're only calling this utility to get a unique id for a subsequent insert, you might avoid it altogether.
Re: Bind variable in select statement [message #172729 is a reply to message #172474] Wed, 17 May 2006 15:52 Go to previous message
akho
Messages: 2
Registered: May 2006
Junior Member
after more testing, I found out what's happening.

stmt = conn.prepareStatement("select ? from dual");
stmt.setString(1, field); where field is tablename.nextval

the bind and execute does not cause an error. The problem is that this is equivalent to: select 'tablename.nextval' from dual, so the result is 'tablename.nextval' and not the next sequence value that I was expecting. Thus, I was getting an error in using getLong(1) to get the result.
Previous Topic: How to create a PDF from a JSP
Next Topic: Cannot convert between UTF8 and UCS2
Goto Forum:
  


Current Time: Fri Nov 22 05:08:30 CST 2024