views:

1170

answers:

11

I am using Java. Inside a Java program I am writing a SQL query:

string query = "select * from tableA" ;

However sometimes the query is really large. In those cases I want to write the query in different lines e.g.:

string query = "select blah1, blah2
                       blah3, blah4
                       blah5, blah6
                from tableA";

Tthis gives a syntax error in the Java code.

Is it possible or not?

+6  A: 

Java does not support this. You can, however, do this:

String query = "select blah1, blah2 " + 
                      "blah3, blah4 " + 
                      "blah5, blah6 " + 
                      "from tableA";
Thomas Lötzer
which may lead you to unexpected problems if you miss that little space before the end of the string. `blah6from table` This is not the case ( the code posted by Thomas ) but eventually it bites you ( usually and by some Law of Morphy reason, when the code is already in production ) I would recommend you to use SQuirreL to avoid this.http://www.squirrelsql.org/
OscarRyz
+1  A: 

It's not possible. Unfortunately Java has no multi-line String constant or heredoc syntax so you're stuck doing:

String s = "one " + 
           "two " + 
           "three";
cletus
+1  A: 

No, you'd have to write:

String query = "select blah1, blah2 "
             + "       blah3, blah4 "
             + "       blah5, blah6 "
             + "from tableA";

Note the capitalization of String, btw.

This would end up with a single line string. If you want the string itself to use multiple lines, you should work out what kind of line separator you want, and then change the declaration to something like:

String query = "select blah1, blah2\n"
             + "       blah3, blah4\n"
             + "       blah5, blah6\n"
             + "from tableA";

Note that all the concatenation happens at compile-time, so you don't need to worry about whether a StringBuilder would be more efficient. (It would actually be less efficient.)

Jon Skeet
I tend to put the `+` at the front of the line too, but if you are going to do that why not put the `\n`/space there too?
Tom Hawtin - tackline
@Tom: I tend to think of the line separator as living at the end of the line - and I find it less distracting there. I don't feel particularly passionately either way though :)
Jon Skeet
A: 

You need to do like this:

string query = "select blah1, blah2" + 
                       "blah3, blah4" +
                       "blah5, blah6" +
                       "from tableA";
Suraj Chandran
note: missing some spaces (for SQL-Query), the resulting string will be `"select blah1, blah2blah3, blah4blah5, blah6from tableA"`
Carlos Heuberger
A: 
string query = "select blah1, blah2" +
               "       blah3, blah4" +
               "       blah5, blah6" +
               " from tableA";

And that's what eclipse does automatically on my workstation btw.

Gregory Pakosz
A: 

It is not.

You have to concatenate them :(

// btw it shold be String with upper case
String query = "select a, b\n"+ 
               "       c,d \n"+
               " from sometable";

I use SQuirreL SQL client which has this "scape/unscape" feature that put's the string concatenation and removes it for you.

From the description:

SQL Entry Area Enhancements Plugin by Gerd Wagner

Installer Category: Standard

This plugin provides adding and removing Java quoting around SQL and formatting of SQL in the SQL editor.

So the next time you have to test that query, you copy from the IDE/editor and paste it into SQuirreL and do right click "Remove quotes" and it will leave you:

 String query = select a,b
                       c,d
                 from table

When you're done with your query you do the inverse, right click and select "quote query" and it will add the string quotes + and \n needed, leaving you ready to paste it back into your source code.

OscarRyz
A: 

Every answer above is correct but I'd prefer to use the StringBuilder class. If the query is built in a method and not declared as a member -> then I'd go for:

final String query = "select ...";

For a StringBuilder example take a look at this.

Gambrinus
const? What kind of Java is it?
PhiLho
sorry - final - too much .net during the last months ;)
Gambrinus
A: 

The Java programming language does not permit literal strings to span lines in source files, so you must use the + concatenation operator at the end of each line in a multi-line string.

valli
A: 

IIRC, it was amongst the proposals for Java 7 (multi-lines strings) but I don't know if it have been selected.

I suppose the argument against that is that such resource should belong to... a resource file, not to source code.
I don't necessarily agree. :)

It was interesting to see the answers, as a kind of informal (and not significant) poll: it looks like, from this random sample of developers, that there is a trend to cut a line after an operator rather than before.
I am, too, on this side, but both sides can have good arguments (a bit like tabs vs. spaces, brace position, etc.).
I like them at the end because when you read code, it is a warning: "This line doesn't finish here". Although it is more valid with languages not finishing their lines with semi-colon... But I try and keep similar coding conventions across languages, when possible.
Putting them at the start of the continued line is also a clear signal: "These are continuation lines".
Deep philosophical thoughts...

PhiLho
A: 

Rather then hard code the string in the source you could use a properties file and a ResourceBundle to get at it. This has the advantage of being able to change the query without having to recompile the code (depending on the change of course... if you are selecting different columns for example things will need to change). You can also specify multi-line strings in the properties file...

package test;

import java.util.ResourceBundle;

public class Main
{
    public static void main(String[] args) 
    {
        final ResourceBundle bundle;

        bundle = ResourceBundle.getBundle("test.foo");
        System.out.println(bundle.getString("text"));
    }
}

and a file called "foo.properties" in the same directory:

text = some really long text goes here \
       you can even make it span \
       multiple lines!
TofuBeer
A: 

You could use String.format to specify the structure of the query and then the specific parameters, viz:

final String TEMPLATE = "SELECT %s FROM %s";
String query = String.format(TEMPLATE,
    "blah1 blah2 blah3 blah4 blah5 blah6",
    "tableA");

Which allows you to split the string up, as you are allowed newlines between parameters.

jhumble
If I ever see someone on my team doing this, I'm going to punch them. No need to make simple things complicated.
Milan Ramaiya
Thank-you for taking the time to comment, Reverend. I'm sure that your parish is a very happy place with you wandering around punching people for ideas that you don't like. I notice that you didn't offer an answer of your own though - I suppose you prefer just to wander this forum abusing others for their ideas. Great work.
jhumble