Java - How to format an SQL statement without getting hit by Java's String immutability?

StackOverflow https://stackoverflow.com/questions/23531261

  •  17-07-2023
  •  | 
  •  

Question

For example I want to create the following SQL SELECT statement for readability in JDBC:

ResultSet result = statement.executeQuery(
        "SELECT " +
            "table1.*, " +
            "table2.someColumn " +
        "FROM " +
            "table1 " +
        "LEFT JOIN " +
            "table2 " +
        "ON " +
            "table1.column1 = table2.column2 " +
        "WHERE " +
            "date > today");

Basically by formatting the SQL command, especially longer ones, it makes it easier to read when coding. The downside is that each line is an instance of an immutable String and this will have a performance and memory penalty. Yes I could use StringBuffer but this makes the SQL code once again unreadable, the same as if I had one big long SQL line.

And yes in this case the cost is only once, but in some cases I have parameters that need to be included. Adding "?" is great, and again minimizes the String immutable issue, but in some cases the SQL command is generated dynamically and this will result in slightly different SQL commands (where parameters are not possible).

Is there a way to write this code within Java and keep the code formatted without paying the String immutability penalty (and without using StringBuffer).

Note: I also can't write it to a file because of other reasons which I won't go into here.

Was it helpful?

Solution

I think that behind the scenes, you're getting a StringBuilder / StringBuffer when you use + anyway. Per the official Javadoc on String:

String concatenation is implemented through the StringBuilder(or StringBuffer) class and its append method.

OTHER TIPS

The compiler should take care of that but if you want you can make the query string a constant and it will be calculated at compile time with little to no overhead:

private static final String QUERY_STRING = "SELECT " +
            "table1.*, " +
            "table2.someColumn " +
        "FROM " +
            "table1 " +
        "LEFT JOIN " +
            "table2 " +
        "ON " +
            "table1.column1 = table2.column2 " +
        "WHERE " +
            "date > today";

...

ResultSet result = statement.executeQuery(QUERY_STRING);

Use a stringbuffer/stringBuilder and use \n logic to hold formatted string and then use the toString methods to get a String.At the end use a replaceAll to get the string without the line delimiter

StringBuffer strBuf=new StringBuffer("");
strBuf.append("SELECT );
strBuf.append("* );
..

To display, use strBugf.toString();

When using in the query

String Finalquery =strBuf.toString().replaceAll("\n","");;

Alternatively , you can also have a list of Strings but I would prefer a strinBuilder/Buffer over a list of Strings.

I really do not understand why you are apprehensive to idea of strigbuffer/ stringBuilder

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top