GeeCON Prague 2014*
The moose likes JDBC and the fly likes Adding column to the table in sql using preprepareStatement. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Databases » JDBC
Bookmark "Adding column to the table in sql using preprepareStatement." Watch "Adding column to the table in sql using preprepareStatement." New topic
Author

Adding column to the table in sql using preprepareStatement.

Ashwani Singh
Greenhorn

Joined: May 10, 2013
Posts: 29
I want to add a new new column in already existing table named "Forumtable".I want to give the name of the column dyanamically using setString() method or so.My code is:


ps3=con.prepareStatement("alter table forumtable add (? varchar2(500),? varchar2(20) )");
ps3.setString(1,newans);
ps3.setString(2,newanswerer);
ps3.executeQuery();

here i wish to add two columns to the table.By this code i am getting sqlexception as ORA-00904: : invalid identifier. How to do it?Please help its urgent.
Saif Asif
Ranch Hand

Joined: Aug 11, 2011
Posts: 440

Greetings Ashwani and welcome to the ranch,

I think you have an error in your SQL. shouldnt it be something like this


Muhammad Saif Asif Mirza
OCJA(5/6) OCJP(6) OCJWCD(6)
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3610
    
  60

Welcome to the Ranch, Ashwani! I've moved your question to a better suited forum, the Ranch Office forum is just for discussions about this site.

Saif Asif wrote:I think you have an error in your SQL. shouldnt it be something like this...

No, that was not the problem. The COLUMN keyword needs only be specified when adding just one column, without the brackets. When the columns are put into the brackets, the COLUMN keyword must not be specified.

The problem is that column names cannot be parametrized. You need to specify the column names right in the text of the statement, no question marks there. Only literals (text, date or numeric) can be parametrized. That should fix it.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11351
    
  16

Putting in that "this is urgent" doesn't really help around here. First, it isn't our problem. Next, a lot of folks will say "hmmm...this was posted a while ago and it's urgent, so i'm probably too late. I'll go help someone else".

I would strongly suggest you read our FAQ on HowToAskQuestionsOnJavaRanch.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
GeeCON Prague 2014
 
subject: Adding column to the table in sql using preprepareStatement.