最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - With JDBC, how to convert a ResultSet into objects? - Stack Overflow

programmeradmin4浏览0评论

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 Customers? 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.

发布评论

评论列表(0)

  1. 暂无评论