2015-11-19 42 views

回答

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); 
    } 
} 
相关问题