Skip to content

A library for generating SQL queries and reducing JDBC boilerplate

License

Notifications You must be signed in to change notification settings

saharNooby/query-lib

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

query-lib

Tired of writing endless try-catch blocks for Connections, PreparedStatements and ResultSets? Try this!

This very lightweight (27 KB) library with no dependencies allows you to:

  • generate SQL queries
  • wrap existing queries (for example, if they are complex)
  • execute an update
  • execute a query and then map single or all results
  • execute a batch

All of this without any try-catch blocks and with (usually) much shorter code. SQLExceptions are still checked.

The library is intended to be as easily integrated into the existing code as possible.

Example

Vanilla Java:

Optional<String> result;
String sql = "SELECT `value` FROM `table` WHERE `key` = ?";
try (Connection con = source.getConnection(); PreparedStatement p = source.getConnection().prepareStatement(sql)) {
    p.setInt(1, key);
    try (ResultSet set = p.executeQuery()) {
        result = set.next() ? Optional.ofNullable(set.getString(1)) : Optional.empty();
    }
}
result.ifPresent(System.out::print);

With query-lib:

Query.select("value")
        .from("table")
        .where("key", key)
        .queryAndMap(source, s -> s.getString(1))
        .ifPresent(System.out::print);

You can see more examples in EXAMPLES.md.

How to Use

Add as a dependency

This library is available in Maven Central.

Maven

<dependency>
    <groupId>me.saharnooby</groupId>
    <artifactId>query-lib</artifactId>
    <version>1.2.2</version>
</dependency>

Gradle

dependencies {
	implementation 'me.saharnooby:query-lib:1.2.2'
}

Other build systems

You can download prebuilt JARs from GitHub releases or build them yourself.

Usage

Use any of the static methods of me.saharnooby.lib.query.query.Query to obtain an AbstractQuery object, then you can specify some values and call update, query, queryAndMap or queryAndMapAll.

query returns a ResultSetWrapper which allows you to map the set using specified mapper functions.

To build and execute a batch of queries, create me.saharnooby.lib.query.batch.BatchBuilder, call add as many times as needed, then execute.

Building

You will need Git, Maven and JDK 8 or higher.

git clone https://github.com/saharNooby/query-lib.git
cd query-lib
mvn clean install

About

A library for generating SQL queries and reducing JDBC boilerplate

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages