File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes INSERT INTO troubles Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "INSERT INTO troubles" Watch "INSERT INTO troubles" New topic
Author

INSERT INTO troubles

Joel Christophel
Ranch Hand

Joined: Apr 20, 2011
Posts: 237
    
    1

The below code doesn't seem to insert a row into the table 'students' as I'd like it to.
The SQLException getMessage() method returns the following:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Name, Last Name, School ID Number) VALUES('Joel', 'Christophel', '101059')' at line 1
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18675
    
    8

Did you really give your columns names which have spaces in them, like "First Name"? If so, then you're going to have to escape them to make the SQL which contains them into valid SQL.

I think that might require putting quotes around them, but I don't see anything about that in the MySQL documentation except for something which says that you escape reserved words by putting a back-tick before them.
Joel Christophel
Ranch Hand

Joined: Apr 20, 2011
Posts: 237
    
    1

Escaping quotation marks around the column names didn't work, so I just removed the spaces from the column names, which allowed me to insert the row. Thanks for your help!

EDIT: After further research, I found the using back ticks (as you mentioned) works for column names with spaces. http://stackoverflow.com/questions/6055505/blank-spaces-in-column-names-with-mysql
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18675
    
    8

Thanks for the feedback Joel!

(In my opinion, using standard column names -- no spaces -- is preferable to having to escape them with back-ticks every time you use them in a query.)
Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1107

Paul Clapham wrote:Thanks for the feedback Joel!

(In my opinion, using standard column names -- no spaces -- is preferable to having to escape them with back-ticks every time you use them in a query.)


I seriously dislike have spaces in column names, but they all seem to allow it these days.
chris webster
Bartender

Joined: Mar 01, 2009
Posts: 1777
    
  16

Paul Clapham wrote:(In my opinion, using standard column names -- no spaces -- is preferable to having to escape them with back-ticks every time you use them in a query.)

+1 for that. Putting spaces in your table/column names, or making them case-sensitive, just gives you another potential source of errors further down the line.


No more Blub for me, thank you, Vicar.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: INSERT INTO troubles