liveBook · Manning

JDBC

As explained in Data-Oriented Programming: Reduce software complexity

JDBC is the standard Java API for connecting to databases, executing database operations, and processing returned results.

JDBC is Java’s database connectivity API. It can expose a ResultSet as a list of maps, letting downstream data-oriented code pass values along without immediately committing to concrete types. section 10.1

A practical JDBC-to-data flow is:

  • Execute a database query and receive a ResultSet. section 10.1
  • Read column labels and values through ResultSetMetaData and getObject. section 10.1
  • Store each row in a Map<String, Object>, then pass the collection toward JSON serialization. section 10.1

Converting rows without elaborate mapping

A small conversion function loops through the ResultSet, uses each column label as a map key, obtains each value as an Object, and returns a list of maps. Type handling can wait until serialization. section 10.1

Listing 10.5 Converting a JDBC result set into a list of hash maps
List<Map<String, Object>> convertJDBCResultSetToListOfMaps(ResultSet rs) {
  List<Map<String, Object>> listOfMaps = 
    new ArrayList<Map<String, Object>>();
  ResultSetMetaData meta = rs.getMetaData();
  while (rs.next()) {
    Map map = new HashMap();
    for (int i = 1; i <= meta.getColumnCount(); i++) {
      String key = meta.getColumnLabel(i);
      Object value = rs.getObject(i);
      map.put(key, value);
    }
    listOfMaps.add(map);
  }
  return listOfMaps;
}

Inspect the complete ResultSet-to-list-of-maps implementation. section 10.1

How Data-Oriented Programming: Reduce software complexity covers it

How deep it goes
Shows a concrete ResultSet conversion utility but stops short of JDBC driver or API internals.
Examples
Uses a book-search query and converts its returned rows into maps containing titles, ISBNs, and publication years.
What it assumes
Assumes Java and database-query familiarity while teaching ResultSet-to-map conversion directly.
What you can do after
Apply JDBC to convert database result sets into flexible collections for later JSON serialization.
How it connects
Connects database queries, ResultSet, SQL, maps, late binding, and JSON serialization.

Also explained in

Compare how 2 books explain JDBC →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Data-Oriented Programming: Reduce software complexity. The section links point to the parts of the book it draws on.