| Author |
Using comments in a query
|
Vishravars Ramasubramanian
Greenhorn
Joined: Apr 28, 2006
Posts: 7
|
|
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
Joined: Apr 14, 2004
Posts: 10336
|
|
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.
|
JavaRanch FAQ HowToAskQuestionsOnJavaRanch
|
 |
Jan Cumps
Bartender
Joined: Dec 20, 2006
Posts: 2343
|
|
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
|
OCUP UML fundamental
ITIL foundation
|
 |
Vishravars Ramasubramanian
Greenhorn
Joined: Apr 28, 2006
Posts: 7
|
|
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
Joined: Apr 20, 2006
Posts: 3327
|
|
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.
|
Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
|
 |
Vishravars Ramasubramanian
Greenhorn
Joined: Apr 28, 2006
Posts: 7
|
|
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
Bartender
Joined: Aug 16, 2005
Posts: 12911
|
|
"vishravars", Please check your private messages. You can see them by clicking "My Profile" at the top right of the page.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
 |
|
|
subject: Using comments in a query
|
|
|