有什么方法可以使用 Eclipse 在 Java 字符串文字中编写(复制粘贴)格式良好的 SQL 查询?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12948860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 19:13:13  来源:igfitidea点击:

Is there any way I can write (copy-paste) nicely-formatted SQL queries in Java string literals using Eclipse?

javaeclipseformatting

提问by Daud

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.

当我希望在 Java 中使用 SQL 查询时,我通常将它们保存在最终的 String 变量中。现在,当字符串太大时,它超出了页面宽度,我们要么必须手动断开它(在 eclipse 中,转到字符串中的特定位置,然后输入 enter,然后对每个生成的较小的部分),或者我们可以将 Eclipse 中的格式化程序设置为每行仅允许(例如)100 个字符。但是字符串并没有以合乎逻辑的方式断开。

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.

我可以在 SQL Developer 中很好地格式化查询(比如),但是如果我将它粘贴到 Java 中,我将不得不手动设置所有结束引号和+符号以使其成为正确的 Java 字符串。我只想知道一种将格式正确的 SQL 查询直接复制粘贴到 Java 文件中的方法。我正在使用 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 :

然后当它粘贴到一个 java 字符串中时,我们可以像这样:

"SELECT" +
  " *" +
 " FROM" +
  " something" + 
 " WHERE";
  id=4;

采纳答案by yiannis

In Eclipse, in Window > Preferences under Java > Editor > Typing check the "Escape text when pasting into a string literal" checkbox.

在 Eclipse 中,在 Java > Editor > Typing 下的 Window > Preferences 中,选中“粘贴到字符串文字时转义文本”复选框。

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

这是 Eclipse Helios 上的输出。一个类似的问题:用引号括起来

回答by Lukas Eder

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

如果您不介意生成的 SQL 输出中的额外空格,这里有一个创造性的解决方案:

Step 1: Paste

第 1 步:粘贴

enter image description here

在此处输入图片说明

Paste your formatted SQL statement verbatim into your Java file

将格式化的 SQL 语句逐字粘贴到 Java 文件中

Step 2: Write opening quotes

第 2 步:写开场白

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

注意突出显示的按钮,左起第六个。这就是很棒的“块选择模式”(Windows 上的 Alt-Shift-A)。它允许您在每个选定行的同一位置写开引号

Step 3: Write closing quotes and concatenation

第 3 步:编写结束引号和连接

enter image description here

在此处输入图片说明

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

对结束引号和连接符号 ( +)应用相同的技术

Step 4: Fix the semi-colon

第 4 步:修复分号

enter image description here

在此处输入图片说明

No comment needed here.

这里不需要评论。

回答by James Jithin

When you are in SQLDeveloper, select the query and use Ctrl + F7to format it. Select it again and use Ctrl + Shift + F7to advance format it, choose Clipboardfor output destination, output type as desired type and click Apply. Now, paste it in Eclipse editor and see the difference.

在 中时SQLDeveloper,选择查询并使用Ctrl + F7它来设置格式。再次选择它并使用它Ctrl + Shift + F7进行高级格式化,选择Clipboard输出目的地,输出类型为所需类型,然后单击Apply。现在,将其粘贴到 Eclipse 编辑器中并查看不同之处。

I'm using Version 3.1.07 of SQLDeveloper.

我使用的是 SQLDeveloper 3.1.07 版。

回答by James Jithin

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.

它不会将 SQL 查询转换为 Java 字符串文字,但它可以防止 Eclipse 重新格式化您的字符串,使其难以阅读。

回答by thebbk

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).

我知道我迟到了,但是对于任何使用大型查询的人来说,Eclipse 中的块模式可能很难使用,因为如果您的查询超过 100 行左右(取决于在您的机器上)。

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

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

回答by full.stack.ex

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

这非常接近:如何粘贴用前导空格缩进的 SQL:https: //stackoverflow.com/a/121513/1665128

String sql =
    "SELECT\n" + 
    "  *\n" + 
    " FROM\n" + 
    "  something\n" + 
    " WHERE\n" + 
    "  id=4;";

回答by Drogba

For DbVisualizer, you may use Ctrl + Alt + Kto format your sql to desired pattern.

对于DbVisualizer,您可以使用Ctrl + Alt + K将 sql 格式化为所需的模式。