Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java-pretty print sql query

Tags:

java

Does someone have pretty_print function for print SQL query in java?

given string like:

SELECT person_table.name FROM person_table JOIN person_person ON person_table.name=person_person WHERE person_table.name=dan

The function will print:

SELECT person_table.name

FROM person_table

JOIN person_person

ON person_table.name=person_person

WHERE person_table.name=dan

or better?

Thanks in advance!

like image 999
gran33 Avatar asked Feb 28 '13 16:02

gran33


People also ask

How do I make a pretty print in SQL?

String sql = pretty. select( AUTHOR. LAST_NAME, count().

How to format Sql query in Java?

All SQL statements in pureQuery code are embedded in quotation marks. The SQL statements are formatted by keyword and indentation. Each SQL keyword starts on a new line. To remain consistent with Eclipse Java formatting, after the SQL is separated into multiple lines, each string is embedded in quotation marks.

How to format Sql query in eclipse?

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.


2 Answers

You can take a look at org.apache.openjpa.lib.jdbc.SQLFormatter.

like image 154
Farlan Avatar answered Oct 25 '22 15:10

Farlan


If you work with Hibernate try org.hibernate.jdbc.util.FormatStyle.BASIC.getFormatter().format(yourNonFormattedSqlQueryString);

like image 29
Alexandr Dolgov Avatar answered Oct 25 '22 17:10

Alexandr Dolgov