Java SQL Formatting Library¶
Turn unreadable SQL into something you would actually want to review.
JSQLFormatter is a platform independent SQL formatter, beautifier and pretty printer by Manticore Projects. It is RDBMS agnostic, it preserves your comments, and it is driven by a real SQL grammar rather than by regular expressions – so it understands the structure of a statement instead of guessing at it.
Latest stable release: JSQLFormatter-5.3.jar
Development version: JSQLFormatter-5.4-SNAPSHOT.jar
GitHub Repository · Online Demo
What’s new in 5.4¶
Now licensed under Apache 2.0. Releases up to and including 5.3 were AGPL. From 5.4 onwards JSQLFormatter is Apache 2.0, which means you can embed it in commercial products, IDE plugins and internal tooling without the copyleft obligation.
A complete Visitor Pattern rewrite. The formatter used to be one long
procedural pass with roughly seventy instanceof checks deciding what to
render. Statements and expressions are now dispatched through JSQLParser’s own
visitor interfaces:
Component |
Responsibility |
|---|---|
|
Dispatches |
|
Renders the full expression tower – operators, functions, |
|
Shared spelling, indentation and keyword-casing primitives |
Two things follow from this. Supporting a new node type is now a single
visit(...) override instead of another branch in a growing if chain.
And every node type that is not explicitly handled still formats sensibly:
a keyword-aware fallback walks the node’s own output, applies your configured
keyword spelling to uppercase token runs, and passes quoted literals through
untouched. No more raw, unformatted islands in the middle of an otherwise
clean statement.
Built on JSQLParser 5.3.242, which brings massive parsing performance improvements.
Note
The EBNF and Railroad Diagram pages have been retired. The authoritative grammar reference now lives with JSQLParser itself.
See it work¶
update cfe.calendar set year_offset=?, settlement_shift=to_char(?),
friday_is_holiday=?, saturday_is_holiday=?, sunday_is_holiday=?
where id_calendar=?;
UPDATE cfe.calendar
SET year_offset = ?
, settlement_shift = To_Char( ? )
, friday_is_holiday = ?
, saturday_is_holiday = ?
, sunday_is_holiday = ?
WHERE id_calendar = ?
;
import com.manticore.jsqlformatter.JSQLFormatter;
String formattedSql = JSQLFormatter.format("SELECT * FROM table1");
java -jar JSQLFormatterCLI.jar -i queries.sql --ansi
Features¶
Grammar driven |
Built on JSQLParser. The SQL is genuinely parsed into an AST before anything is printed. |
RDBMS agnostic |
Oracle, SQL Server, MySQL and MariaDB, PostgreSQL, H2, DuckDB, BigQuery, Redshift, Databricks and Snowflake, from one grammar. |
Broad coverage |
Complex |
Comments survive |
Comments are anchored to the node they belong to, including comments wedged between keywords or inside join conditions. String literals that merely look like comments are left alone. |
Syntax highlighting |
ANSI for the terminal, HTML and RTF for documents and wikis. |
Round-trips through Java |
Import a SQL string out of Java source preserving variables, then export
it back as a |
Configurable |
Indent width, comma placement and keyword, function and object casing – set per invocation, per file, or per statement. |
Platform¶
Target |
Notes |
|---|---|
Java library |
JAR from Maven Central |
Command line |
Executable JAR |
Native binary |
Static binary or shared library for Linux, Windows and macOS |
NetBeans plugin |
Eclipse, JEdit, Squirrel SQL and DBeaver planned |