{"id":15044327,"url":"https://github.com/juliomarcopineda/jdbc-stream","last_synced_at":"2025-04-10T00:43:01.708Z","repository":{"id":57720643,"uuid":"183265347","full_name":"juliomarcopineda/jdbc-stream","owner":"juliomarcopineda","description":"Light-weight library to wrap JDBC ResultSet to Java Stream API","archived":false,"fork":false,"pushed_at":"2019-05-06T18:43:00.000Z","size":126,"stargazers_count":9,"open_issues_count":1,"forks_count":3,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-10T00:42:56.773Z","etag":null,"topics":["java","java-stream-api","java-streams","java8","java8-stream","jdbc","lambda","sql"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/juliomarcopineda.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-04-24T16:18:06.000Z","updated_at":"2024-09-27T03:00:42.000Z","dependencies_parsed_at":"2022-08-28T05:21:39.133Z","dependency_job_id":null,"html_url":"https://github.com/juliomarcopineda/jdbc-stream","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/juliomarcopineda%2Fjdbc-stream","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/juliomarcopineda%2Fjdbc-stream/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/juliomarcopineda%2Fjdbc-stream/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/juliomarcopineda%2Fjdbc-stream/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/juliomarcopineda","download_url":"https://codeload.github.com/juliomarcopineda/jdbc-stream/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248137998,"owners_count":21053775,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["java","java-stream-api","java-streams","java8","java8-stream","jdbc","lambda","sql"],"created_at":"2024-09-24T20:50:27.113Z","updated_at":"2025-04-10T00:43:01.688Z","avatar_url":"https://github.com/juliomarcopineda.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# jdbc-stream\n[![Build Status](https://travis-ci.org/juliomarcopineda/jdbc-stream.svg?branch=master)](https://travis-ci.org/juliomarcopineda/jdbc-stream)\n[![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.github.juliomarcopineda/jdbc-stream/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.github.juliomarcopineda/jdbc-stream)\n[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)\n\nLight-weight library to wrap JDBC ResultSet to Java Stream API\n\n# Overview\njdbc-stream is a light-weight convenience library that wraps any JDBC ResultSet into a Java 8 Stream to take advantage of the Stream API and functional programming in Java.\n\n## Add jdbc-stream to your build\njdbc-stream can be found at MavenCentral with group ID `com.github.juliomarcopineda` and artifact ID `jdbc-stream`.\n\n#### Maven\n\n```xml\n\u003cdependency\u003e\n  \u003cgroupId\u003ecom.github.juliomarcopineda\u003c/groupId\u003e\n  \u003cartifactId\u003ejdbc-stream\u003c/artifactId\u003e\n  \u003cversion\u003e0.1.1\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n#### Gradle (Groovy DSL)\n\n```groovy\nimplementation 'com.github.juliomarcopineda:jdbc-stream:0.1.1'\n```\n\n#### Gradle (Kotlin DSL)\n\n```kotlin\ncompile(\"com.github.juliomarcopineda:jdbc-stream:0.1.1\")\n```\n\n## Example Usage\nFor this example, we will assume that we have a SQLite instance with the [Iris Data Set](https://archive.ics.uci.edu/ml/datasets/iris) in the table `iris` with the following schema:\n\n```sql\nCREATE TABLE iris (SepalLength real, \n                   SepalWidth real, \n                   PetalLength real, \n                   PetalWidth real, \n                   IrisClass varchar(255))\n```\n\n#### 1) Create a connection to our SQL database. \nFor this example we will use an in-memory SQLite server and the `java.sql` library\n\n```java\nConnection conn = DriverManager.getConnection(\"jdbc:sqlite::memory\")\n```\n\n#### 2) Execute SQL query to get ResultSet\n\n```java\nString sql = \"SELECT * FROM iris\"\nPreparedStatement ps = conn.prepareStatement(sql)\nResultSet resultSet = ps.executeQuery()\n```\n\n#### 3) Use jdbc-stream library to utilize Java 8 Stream API\nThe following are limited examples of what you can do with jdbc-stream and Java 8 Stream API\n\n##### a) Get number of records from SQL query\n\n```java\nlong count = JdbcStream.stream(resultSet).count()\n```\n\n##### b) Get average Sepal Length of all Iris records\n\n```java\ndouble averageSepalLength = JdbcStream.stream(resultSet)\n  .mapToDouble(rs -\u003e {\n    double sepalLength = 0;\n    try {\n      sepalLength = rs.getDouble(\"SepalLength\");\n    }\n    catch (SQLException e) {\n      // Handle exception\n    }\n    return sepalLength;\n  })\n  .average()\n  .getAsDouble();\n```\n\n##### c) Get Iris species with the widest Petal Width\n\n- Get `Map\u003cString, List\u003cDouble\u003e\u003e` where each `key` is the Iris species and each `value` is the `List\u003cDouble\u003e` of all their possible petal widths\n\n```java\nMap\u003cString, List\u003cDouble\u003e\u003e petalWidths = JdbcStream.stream(resultSet)\n  .map(rs -\u003e {\n    String irisClass = \"\";\n    double petalWidth = 0;\n    try {\n      irisClass = rs.getString(\"IrisClass\");\n      petalWidth = rs.getDouble(\"PetalWidth\");\n    }\n    catch (SQLException e) {\n    \t// Handle exception\n    }\n    return new AbstractMap.SimpleEntry\u003c\u003e(irisClass, petalWidth);\n    })\n  .collect(Collectors.toMap(Map.Entry::getKey,\n                            e -\u003e {\n                              List\u003cDouble\u003e list = new ArrayList\u003c\u003e();\n                              list.add(e.getValue());\n                              return list;\n                              },\n                            (l1, l2) -\u003e Stream.of(l1, l2)\n                                          .flatMap(Collection::stream)\n                                          .collect(Collectors.toList())))\n```\n- Iterate over entries of the Map to get the Iris species with the widest petal\n\n```java\nString irisWidestPetal = petalWidths.entrySet()\n  .stream()\n  .max((e1, e2) -\u003e Collections.max(e1.getValue()) \u003e Collections.max(e2.getValue()) ? 1 : -1)\n  .get()\n  .getKey()\n```\n\n##### d) Use a custom Mapper from ResultSet to client-defined Java class\njdbc-stream can handle the mapping between ResultSet rows to a Java class as long as the client provides a Mapper. This Mapper must extend the Mapper interface that is included in the library.\n\nExample usage of this Mapper as follows:\n\n- First let us define a Sepal class.\n\n```java\npublic class Sepal {\n  private int width;\n  private int length;\n  \n  public double getArea() {\n    return this.width * this.length;\n  }\n  \n  // getters and setters\n}\n```\n\n- Then, let's define a Mapper from ResultSet row to a Sepal.\n\n```java\npublic class SepalMapper\u003cSepal\u003e implements Mapper\u003cSepal\u003e {\n  \n  @Override\n  public Sepal map(ResultSet resultSet) {\n    Sepal sepal = new Sepal();\n    \n    try {\n      sepal.setWidth(resultSet.get(SepalWidth));\n      sepal.setLength(resultSet.get(SepalLength));\n    }\n    catch (SQLException e) {\n      // Handle exception\n    }\n    \n    return sepal;\n  }\n}\n```\n\n- Now, we can use both the ResultSet and the custom Mapper to take advantage of Java Stream API. Suppose we want to get the average sepal area:\n\n```java\nSepalMapper mapper = new SepalMapper();\n\ndouble averageSepalArea = JdbcStream(resultSet, mapper)\n  .map(Sepal::getArea)\n  .mapToDouble(i -\u003e i)\n  .average()\n  .getAsDouble();\n```\n\n- Note: we could have used an anonymous class to define the custom mapper as follows:\n\n```java\nMapper\u003cSepal\u003e mapper = new Mapper\u003cSepal\u003e() {\n  @Override\n  public Sepal map(ResultSet resultSet) {\n    Sepal sepal = new Sepal();\n    \n    try {\n      sepal.setWidth(resultSet.get(SepalWidth));\n      sepal.setLength(resultSet.get(SepalLength));\n    }\n    catch (SQLException e) {\n      // Handle exception\n    }\n    \n    return sepal;\n  }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjuliomarcopineda%2Fjdbc-stream","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjuliomarcopineda%2Fjdbc-stream","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjuliomarcopineda%2Fjdbc-stream/lists"}