File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes Using comments in a query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Using comments in a query" Watch "Using comments in a query" New topic

Using comments in a query

Vishravars Ramasubramanian

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");
sbSample.append("select 'ijkl', 'mnop'");

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

the result was
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

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

Joined: Dec 20, 2006
Posts: 2565

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 and ITIL foundation
Vishravars Ramasubramanian

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: 3381

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

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
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15081


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 8 API documentation
I agree. Here's the link:
subject: Using comments in a query
It's not a secret anymore!