如何使用Java在控制台中打印任何PostgreSQL查询结果?

时间:2015-11-19 21:32:16

标签: java postgresql jdbc

如果您使用Java作为编程语言而PostgreSQL作为DBMS,那么您可能需要在控制台中检查任何查询的输出以进行测试。

如何在控制台中打印任何查询的结果?

1 个答案:

答案 0 :(得分:1)

只需使用Java中的PostgreSQL标准JDBC驱动程序。

以下是用于打印任何SQL查询的实用程序类的简单示例:

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;
        }
    }
}

以下是如何使用它的示例:

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);
    }
}