• 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

Using comments in a query

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found a peculiar problem when i tried retrieving values from a table using JDBC.

I had 2 parts of a queries separated by UNION. When i used the m$ sql comment "--" in the first part of the query, the code after union did not get executed. Say for example:-

sbSample.append("select 'abcd', 'efgh'");
sbSample.append("--order by 1");
union
sbSample.append("select 'ijkl', 'mnop'");

(sbSample is a StringBuffer type)
then executing the query using create statement to follow.

the result was
abcd
efgh (assume that i printed the result in some loop)

I have not tried any other combination yet.

Explanation/tips on how to observe this more detailed are highly welcome.
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So you are saying that SQL that has been commented out does not run?

This is no surprise, is it? Assuming sbSample is a StringBuffer what you've coded will produce one line of SQL.
 
Bartender
Posts: 2661
19
Netbeans IDE C++ Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It doesn't work. Everything in your statement ends up in one long string.
So everything after the -- would be seen as comment:

Also, union is not a valid java command. Your code will not compile.

select x from y -- order by x union select y from z

Using -- as comments might also make your code incompatible with other databases.
It might be better to put the comments in your javacode:Regards, Jan
 
Vishravars Ramasubramanian
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
eek that was a typo. union is part of the string .

and yes putting // comments will work fine and -- wont make sense in other db's.

Point needed is why the result is that way.
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by vishravars vishravars:

Point needed is why the result is that way.



That's because as Jan Cumps said the "--" causes the whole remaining portion of SQL to be commented.

Just do a System.out.println of your StringBuffer and take the output as it is and run it in the SQL editor. You will get to know.
 
Vishravars Ramasubramanian
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all. I am clear now. Later used profiler to find the commented part of the query. I felt hard to locate the bug because that one line comment was hidden in the dark. So got confused with the way the comments work.

Thanks all once again.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"vishravars",

Please check your private messages. You can see them by clicking "My Profile" at the top right of the page.
 
reply
    Bookmark Topic Watch Topic
  • New Topic