• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JDBC Exception

 
Ranch Hand
Posts: 79
1
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I have a treeSet that I iterate through and am trying to insert it into a table but I get an error that the syntax is incorrect near ONE value? What could be the problem?

Here is the code piece


And this is the exception
Exception in thread "main" com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near 'MP903'.

Thank you for your help
 
Saloon Keeper
Posts: 10732
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your empId is a String which SQL requires to be enclosed in single quotes.
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you had used a PreparedStatement, though, you wouldn't have to concern yourself with that sort of details.
 
Pat Watson
Ranch Hand
Posts: 79
1
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Your empId is a String which SQL requires to be enclosed in single quotes.



Yes forgot about that part. Thank you
 
Pat Watson
Ranch Hand
Posts: 79
1
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:If you had used a PreparedStatement, though, you wouldn't have to concern yourself with that sort of details.



I haven't used PreparedStatements much but I will look into it
 
Paul Clapham
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If your query is constant, e.g. SELECT that, that, theother from SOMETABLE, then a Statement is fine. But if your query requires parameters, like in your example, you should use PreparedStatement. That not only means you don't have to know how to format Strings and Dates and Times and so on for your database, it also protects you from SQL injection attacks.

Even formatting Strings isn't as simple as you might guess. Yes, you have to put quotes around them to make valid SQL, but if the String contains a quote (like a user named O'Brien) you have to escape that quote in the way your database wants it escaped. This can get to be a real pain, but PreparedStatement takes care of all that tedious stuff.
 
reply
    Bookmark Topic Watch Topic
  • New Topic