• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using comments in a query

 
Vishravars Ramasubramanian
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • 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.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • 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.
 
Jan Cumps
Bartender
Posts: 2588
11
C++ Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15356
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic