简体   繁体   中英

How to connect to postgres and execute query using r2dbc

I'm trying to write a simple function which will connect to postgres and execute a select statement.

PostgresqlConnectionFactory connectionFactory = new PostgresqlConnectionFactory(
        PostgresqlConnectionConfiguration.builder()
            .host("localhost")
            .port(5432)
            .database("MyDB")
            .username("username")
            .password("password").build());



DatabaseClient client = DatabaseClient.create(connectionFactory);

Flux<Map<String, Object>> result = client.execute("select * from table").fetch().all();

result.map(s -> {
  System.out.println(s);
  return null;
});

The above piece of code isn't printing anything. There is no error as well. I can connect to DB using the same credentials. What is missing in the code to stream data from DB?

Create the configuration class which is similar to the below code to connect to the PostgreSQL database

@Configuration
@EnableR2dbcRepositories
public class DatabaseConfig extends AbstractR2dbcConfiguration {

    @Override
    public ConnectionFactory connectionFactory() {
        return ConnectionFactories.get("r2dbc:postgresql://localhost:5432/DATABASE_NAME");
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM