File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes Alter Query giving an error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Alter Query giving an error" Watch "Alter Query giving an error" New topic

Alter Query giving an error

Betsy Camel
Ranch Hand

Joined: Oct 02, 2003
Posts: 119
public static String alterQuery(String ColumnName) throws SQLException
String msg = "Added a Column Successfully";
stmt.executeUpdate("alter table Machine_Details add '"+ColumnName+"' varchar(10)");
return msg;

this is being accessed in my jsp like

String message = myclass.alterQuery(request.getParameter("namecolumn"));

where namecolumn is the textbox name.
this is giving an error ..
java.sql.SQLException: [Microsoft][ODBC SQL Server Driver][SQL Server]Line 1: Incorrect syntax near 'Machine_length'.

here machine _length is the value given in the text box...
plz help
Dave Salter
Ranch Hand

Joined: Jul 20, 2005
Posts: 293

Do you know the actual SQL that is being issued?
Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

Double check the alter table syntax for SQL Server (here). You don't put quotes round new columns.

As an aside, I'm guessing you have your reasons, but its not usually a good idea to allow DDL statements from a Web App.
[ September 22, 2005: Message edited by: Paul Sturrock ]

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Maximilian Xavier Stocker
Ranch Hand

Joined: Sep 20, 2005
Posts: 381
I believe the syntax should be

ALTER TABLE tablename ADD COLUMN columnname datatype

Because you could be adding an index too. I am not sure about the quotes around the name either but at least try specifiying that you are in fact adding a COLUMN.
I agree. Here's the link:
subject: Alter Query giving an error
It's not a secret anymore!