We are using Java 21 and Spring Boot 3.4 to connect to Postgres using JPA. But we also need to pull data from a Snowflake database once a day in a scheduled job and write it to Postgres.
Using Spring Boot with multiple data sources is complex, and we don't want to pollute or create DB connection pools for Snowflake. So we are using plain old JDBC to get a Connection
. Then we can do this:
Connection con = getMyConnection();
PreparedStatement ps = con.prepareStatement("select * from customers where foo=?");
ps.setString(1, "bar");
ResultSet results = ps.executeQuery();
How to convert resultSet
into a list or array of Customer
s? Something like this:
List<Customer> jdbcTemplate.queryForObject(sql, Customer);
Except jdbcTemplate
only works with a DataSource
, not a Connection
, and it seems to be deprecated.
We could write something ourselves which uses introspection of the field names of Customer
and the column names in the result set, but we assume someone has already done this?
We can do it the manual way, iterating over the result set and setting each field manually, but some of the tables we are importing have hundreds of columns.