I've searched the site but haven't found exactly what I need. I have an SQL statement
String unformattedSQL = "select id, name, address, zip from schema123.person where name like '%stu%'";
I'm looking for a method like formatSQL(sqlString)
that gives me the formatted version of my original SQL statement, such that
String formattedSQL = formatSQL(unformatted);
System.out.println(formattedSQL);
will give me the following text (with correct indentation)
SELECT id, name,
address, zip
FROM schema123.person
WHERE name LIKE '%stu%'
The original SQL statement is actually very long and is dynamically generated by another method (I cannot change this method), so there's no way I can put the SQL in an external file. After the SQL is executed, I need to put it in a log file. I want the SQL statement to be formatted in the log file.
What is the best way to do this? And to generalize the problem, how to do this for another syntax like XML, C#, etc. Maybe something similar to Google Code Prettify but for Java ...
In a SQL document, right-click the SQL script, click Active Format Profile, and select the required format. 2. To apply the selected format profile, select the SQL document or the fragment of the code and press Ctrl+K, then Ctrl+F. In addition, you can create a custom format profile based on the available profiles.
In java, String format() method returns a formatted string using the given locale, specified format string, and arguments. We can concatenate the strings using this method and at the same time, we can format the output concatenated string.
STEP 1: Allocate a Connection object, for connecting to the database server. STEP 2: Allocate a Statement object, under the Connection created earlier, for holding a SQL command. STEP 3: Write a SQL query and execute the query, via the Statement and Connection created. STEP 4: Process the query result.
In SQL Server, the FORMAT() function enables you to format date/time and number values as a formatted string by passing in a “format string” as the second argument (the first argument is the value that's being formatted). Here's an example of this function in action: FORMAT(@date, 'dd/MM/yyyy');
You can go for the SQLFormatter provided by apache openJPA project
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