As is generally understood from my previous Java questions, I do not fully know, so please treat with understanding. There is a C ++ code that reads from the postgresql database

 #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 query to the postgresql database in Java and write the results to ArrayList<String> . Please help me do it.

    2 answers 2

    First you need to connect to the database:

     Connection con = DriverManager.getConnection(url, user, pass); ArrayList<String> arrayString = new ArrayList<String>(); String query = "тут запрос"; ResultSet res = con.createStatement.executeQuery(query); while (res.next) { arrayString.add(res.getString("1")) } 
    • @ G1yyK sorry for being incomprehensible, but it is very necessary, so I’ll clarify: where is the name of the host, port (for postgresql ), the database and in what format, and which library should I include? - ivan89
    • String url = "jdbc: postgresql: //192.168.143.93: 5432 / NexentaSearch You still need to download the jdbc.postgresql.org/index.html driver and connect it to your code via -classpath. - G1yyK

    In C / C ++, the connection is made via ODBC, in Java through JDBC - in a particular case through the JDBC-ODBC bridge. Examples here