• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Alter Query giving an error

 
Betsy Camel
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 293
Java Mac OS X Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you know the actual SQL that is being issued?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Maximilian Xavier Stocker
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic