If you're using Java as your programming language and PostgreSQL as your DBMS then you could want to check the Output of any query in the console for testing purposes.
How could you print the results of any query in the console?
Just use the standard JDBC Driver for PostgreSQL in Java.
Here is a simple example of an utility class for printing any SQL Query:
import java.nio.charset.StandardCharsets;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
public class PostgreSQLConnection {
public String host;
public String port;
public String username;
public String password;
public String database;
private Connection connection;
public PostgreSQLConnection(String host, String port, String username, String password, String database) {
super();
this.host = host;
this.port = port;
this.username = username;
this.password = password;
this.database = database;
}
public void checkDemo(String table, String pkColumn) {
try {
this.connect();
Statement stmt = null;
String query = "SELECT * FROM " + table;
stmt = this.connection.createStatement();
ResultSet rs = stmt.executeQuery(query);
System.out.println("Column " + pkColumn);
while (rs.next()) {
String id = new String(rs.getBytes(pkColumn), StandardCharsets.UTF_8);
System.out.println("| Column " + id + " |");
}
this.disconnect();
} catch (Exception e) {
e.printStackTrace(System.out);
}
}
public String getResults(String sqlQuery) {
try {
String result = "";
this.connect();
Statement stmt = null;
stmt = this.connection.createStatement();
ResultSet rs = stmt.executeQuery(sqlQuery);
ResultSetMetaData rsMeta = rs.getMetaData();
int count = rsMeta.getColumnCount();
int i, j = 1;
result += "\n| ";
while (j <= count) {
String format = "%1$-" + rsMeta.getColumnDisplaySize(j) + "s";
String formatedValue = String.format(format, rsMeta.getColumnLabel(j));
result += formatedValue + "| ";
j++;
}
result += "\n" + new String(new char[result.length()]).replace("\0", "-");
while (rs.next()) {
i = 1;
result += "\n| ";
while (i <= count) {
String format = "%1$-" + rsMeta.getColumnDisplaySize(i) + "s";
String formatedValue = String.format(format, new String(rs.getBytes(i), StandardCharsets.UTF_8));
result += formatedValue + "| ";
i++;
}
}
this.disconnect();
return result;
} catch (Exception e) {
e.printStackTrace(System.out);
return "";
}
}
private void connect() throws Exception {
Class.forName("org.postgresql.Driver");
this.connection = null;
this.connection = DriverManager.getConnection(
"jdbc:postgresql://" + this.host + ":" + this.port + "/" + this.database, this.username, this.password);
}
private void disconnect() throws Exception {
if (this.connection != null) {
this.connection.close();
this.connection = null;
}
}
}
Here is an example of how to use it:
package com.peoplewalking.psql.demo;
public class MainDemo {
public static void main(String[] args) {
String host = "localhost";
String port = "5432";
String user = "postgres";
String pass = "postgres";
String database = "your_database_name";
PostgreSQLConnection psqlc = new PostgreSQLConnection(host, port, user, pass, database);
String prettyConsoleOutput = psqlc.getResults("SELECT Id, Name FROM Person");
System.out.println(prettyConsoleOutput);
}
}