-1

I decided to remove the intermediate link and as a whole to simplify the decision, having transferred everything to the java program. There is a code on С++, reading of database postgresql

#include <iostream>

//using namespace std;

#include <stdio.h>
#include "libpq-fe.h"
#include <string>
#include <cstdio>
#include <stdlib.h>


int     main() {
    PGconn          *conn;
    PGresult        *res;
    int             rec_count;
    int             row;
    int             col;
    FILE            *stream;

    conn = PQconnectdb("hostaddr=192.168.143.93 port=5432 connect_timeout=10 dbname=NexentaSearch user=postgres password=postgres");
    if (PQstatus(conn) == CONNECTION_BAD) {
        fprintf(stderr, "Connection to database failed: %s\n",PQerrorMessage(conn));
     puts("No connection");
        exit(0);
    }

    res = PQexec(conn, "select path from tasks order by id");

    if (PQresultStatus(res) != PGRES_TUPLES_OK) {
        printf("We didn't get the data");
        exit(0);
    }

    rec_count = PQntuples(res);

I want to execute the same request to postgresql to a database on Java and to write results in ArrayList . Help to make it please.

4

2 回答 2

0

看这个帖子:

  1. 连接 postgreqsl
  2. 完整示例
于 2013-04-21T15:13:00.063 回答
0

你有两个问题:

  1. 如何使用 Java 与 PostgreSQL 交互。
  2. 如何将查询结果集映射到集合。

第一个不难。从 DAO 的接口开始:

public interface PathDao {
    List<String> findAllPaths(); 
}

创建一个实现:

public class PathDaoImpl implements PathDao {
    private static final String FIND_ALL_PATHS_QUERY = "select path from tasks order by id"; 
    private DataSource dataSource;

    public PathDaoImpl(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    public List<String> findAllPaths() {
        List<String> paths = new ArrayList<String>();
        Statement st = null;
        ResultSet rs = null;
        try {
            st = this.dataSource.getConnection().createStatement(FIND_ALL_PATHS_QUERY);
            rs = st.executeQuery();
            paths = mapPathQuery(rs);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            DatabaseUtils.close(rs);
            DatabaseUtils.close(st);
        }    
        return paths;
    }

    private List<String> mapPathQuery(ResultSet rs) throws SQLException {
        List<String> paths = new ArrayList<String>();
        while (rs.next()) {
            paths.add(rs.getString("path");
        }
        return paths; 
    }
}

第二个非常简单,因为您要求的是单个 String 列值。

public class DatabaseUtils {
    private DatabaseUtils() {}

    public static void close(Statement st) {
        try {
            if (st != null) {
                st.close();
            }
        } catch (Exception e) {
            e.printStackTrace(); // logging is a better solution
        }
    }

    public static void close(ResultSet rs) {
        try {
            if (rs != null) {
                rs.close();
            }
        } catch (Exception e) {
            e.printStackTrace(); // logging is a better solution
        }
    }
}
于 2013-04-21T15:13:33.420 回答