When I wish to use SQL queries in Java, I usually hold them in final String variables. Now, when the string is too large, it goes out of page breadth, and we either have to manually break it (In eclipse, go to a particular place in the string, and place enter, and do it for each of the resulting smaller part), or we can set the formatter in Eclipse to allow only (say) 100 characters per line. But the string is not broken in a logical manner.
I can format a query nicely in SQL Developer (say), but if I paste that in Java, I will have to manually set all the end quotes, and +
symbols to make it a proper Java string. I just want to know a way to have a properly formatted SQL query copy-pasted directly into a Java file. I am using Eclipse.
Perhaps when a query is formatted like :
SELECT
*
FROM
something
WHERE
id=4;
then when its pasted inside a java string, we can have it like :
"SELECT" +
" *" +
" FROM" +
" something" +
" WHERE";
id=4;
With the file with the extension sql open, select "Format SQL" from the Edit menu. With the file with the extension sql open, select "Format SQL" from the context menu of the editor. Use Ctrl + Shift + L on Windows, Cmd + Shift + L on OS X to format currently active document.
Here's a creative solution if you don't mind the extra whitespace in generated SQL output:
Paste your formatted SQL statement verbatim into your Java file
Notice the highlighted button, the sixth from the left. That's the awesome "Block Selection Mode" (Alt-Shift-A on Windows). It lets you write opening quotes on each selected line at the same position
Apply the same technique for the closing quotes and the concatenation sign (+
)
No comment needed here.
In Eclipse, in Window > Preferences under Java > Editor > Typing check the "Escape text when pasting into a string literal" checkbox.
It will format the plain text:
line1 line2 line3
to:
private final String TEXT = "line1\r\n" + "line2\r\n" + "line3";
This is the output on Eclipse Helios. A similar question: Surround with quotation marks
When you are in SQLDeveloper
, select the query and use Ctrl + F7
to format it. Select it again and use Ctrl + Shift + F7
to advance format it, choose Clipboard
for output destination, output type as desired type and click Apply
. Now, paste it in Eclipse editor and see the difference.
I'm using Version 3.1.07 of SQLDeveloper.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With