Is there any way I can write (copy-paste) nicely-f

2020-02-23 05:53发布

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;

7条回答
forever°为你锁心
2楼-- · 2020-02-23 06:30

Here's a creative solution if you don't mind the extra whitespace in generated SQL output:

Step 1: Paste

enter image description here

Paste your formatted SQL statement verbatim into your Java file

Step 2: Write opening quotes

enter image description here

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

Step 3: Write closing quotes and concatenation

enter image description here

Apply the same technique for the closing quotes and the concatenation sign (+)

Step 4: Fix the semi-colon

enter image description here

No comment needed here.

查看更多
一纸荒年 Trace。
3楼-- · 2020-02-23 06:30

This is quite close: how to paste your SQL indented with leading whitespace: https://stackoverflow.com/a/121513/1665128

String sql =
    "SELECT\n" + 
    "  *\n" + 
    " FROM\n" + 
    "  something\n" + 
    " WHERE\n" + 
    "  id=4;";
查看更多
叼着烟拽天下
4楼-- · 2020-02-23 06:32

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.

查看更多
看我几分像从前
5楼-- · 2020-02-23 06:39

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

查看更多
乱世女痞
6楼-- · 2020-02-23 06:42

I know that I'm late to this party, but for anyone using large queries, the block mode in Eclipse can be very difficult to work with, because it slows way down if you have a query with more than 100 lines or so (depending on your machine).

I found this site and it is very simple and very fast. Post your code into a window, add a prefix (") and add a suffix (" +) and click save as. It will add the prefix and suffix to each line quite quickly.

Here's the online tool:

http://textmechanic.co/Add-Prefix-Suffix-to-Text.html

查看更多
【Aperson】
7楼-- · 2020-02-23 06:46

Actually a really good question, I often wonder about that as well. One tip I can give you is using the following:

//@formatter:off
    private static final String QUERY = 
    "SELECT t.* " +
      "FROM table t " +
     "WHERE t.age > 18";
//@formatter:on

It does not convert a SQL query into a Java String literal, but it keeps Eclipse from reformatting your String awkwardly unreadable.

查看更多
登录 后发表回答