开发

修改 PostgreSQL 的连接配置

  1. 修改 PostgreSQL 的监听地址

    编辑 /opt/postgresql/data/postgresql.conf 文件,将

    1. listen_addresses = 'localhost'

    改为:

    1. listen_addresses = '0.0.0.0'
  2. 修改信任的机器列表

    编辑 /opt/postgresql/data/pg_hba.conf 文件,将

    1. # IPv4 local connections:
    2. host all all 127.0.0.1/32 trust

    改为:

    1. # IPv4 local connections:
    2. host all all 127.0.0.1/32 trust
    3. host all all 0.0.0.0/0 trust
  3. 重启 PostgreSQL

    1. $ bin/pg_ctl stop -s -D pg_data/ -m fast
    2. $ bin/postgres -D pg_data/ >> logfile 2>&1 &

JDBC连接程序

  1. package com.sequoiadb.sample;
  2. import java.sql.*;
  3. public class postgresql_sample {
  4. static{
  5. try {
  6. Class.forName("org.postgresql.Driver");
  7. } catch (ClassNotFoundException e) {
  8. e.printStackTrace();
  9. }
  10. }
  11. public static void main( String[] args ) throws SQLException{
  12. String pghost = "192.168.30.182";
  13. String port = "5432";
  14. String databaseName = "foo";
  15. // postgresql process is running in which user
  16. String pgUser = "sdbadmin";
  17. String url = "jdbc:postgresql://"+pghost+":"+port+"/" + databaseName;
  18. Connection conn = DriverManager.getConnection(url, pgUser, null);
  19. Statement stmt = conn.createStatement();
  20. String sql = "select * from sdb_upcase_field ";
  21. ResultSet rs = stmt.executeQuery(sql);
  22. boolean isHeaderPrint = false;
  23. while (rs.next()) {
  24. ResultSetMetaData md = rs.getMetaData();
  25. int col_num = md.getColumnCount();
  26. if (!isHeaderPrint){
  27. for (int i = 1; i <= col_num; i++) {
  28. System.out.print(md.getColumnName(i) + "|");
  29. isHeaderPrint = true;
  30. }
  31. }
  32. for (int i = 1; i <= col_num; i++) {
  33. System.out.print(rs.getString(i) + "|");
  34. }
  35. System.out.println();
  36. }
  37. stmt.close();
  38. conn.close();
  39. }
  40. }