import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;

import com.github.fjdbc.Fjdbc;
import com.github.fjdbc.PreparedStatementBinder;
import com.github.fjdbc.SingleConnectionProvider;
import com.github.fjdbc.op.DbOperation;
import com.github.fjdbc.query.SingleRowExtractor;

public class Examples {
	public static void main(String[] args) throws SQLException {
		final Connection connection = DriverManager.getConnection("jdbc/url/to/database");
		final SingleConnectionProvider cnxProvider = new SingleConnectionProvider(connection);
		final Fjdbc fjdbc = new Fjdbc(cnxProvider);

		// -------------------------------------------------------------------------------------------------------------
		// Query the database
		{
			final String sql = "select name from user";
			final SingleRowExtractor<String> extractor = (rs) -> rs.getString("name");
			final List<String> names = fjdbc.query(sql, extractor).toList();
			System.out.println(names);
		}

		// -------------------------------------------------------------------------------------------------------------
		// Query the database using a prepared statement
		{
			final String sql = "select name from user where role = ?";
			final PreparedStatementBinder binder = (ps, seq) -> ps.setString(seq.next(), "grunt");
			final SingleRowExtractor<String> extractor = rs -> rs.getString("name");
			final List<String> names = fjdbc.query(sql, binder, extractor).toList();
			System.out.println(names);
		}

		// -------------------------------------------------------------------------------------------------------------
		// Execute a statement (update, delete, insert, etc)
		{
			final String sql = "update user set name='jack' where name='henri'";
			final int nRows = fjdbc.statement(sql).executeAndCommit();
			System.out.println(nRows + " rows changed");
		}

		// -------------------------------------------------------------------------------------------------------------
		// Execute a prepared statement
		{
			final String sql = "update user set name=? where name=?";
			final PreparedStatementBinder binder = (ps, paramIndex) -> {
				ps.setString(paramIndex.next(), "jack");
				ps.setString(paramIndex.next(), "henri");
			};
			final int nRows = fjdbc.statement(sql, binder).executeAndCommit();
			System.out.println(nRows + " rows changed");
		}

		// -------------------------------------------------------------------------------------------------------------
		// Execute a sequence of statements (in a single transaction)
		{
			final PreparedStatementBinder binder = (ps, paramIndex) -> {
				ps.setString(paramIndex.next(), "jack");
				ps.setString(paramIndex.next(), "henri");
			};
			final DbOperation updateName = fjdbc.statement("update user set name=? where name=?", binder);

			final PreparedStatementBinder binder2 = (ps, paramIndex) -> {
				ps.setString(paramIndex.next(), "manager");
			};
			final DbOperation deleteManagers = fjdbc.statement("delete from user where role=?", binder2);

			final int nRows = fjdbc.composite(updateName, deleteManagers).executeAndCommit();
			System.out.println(nRows + " rows changed");
		}
	}
}

@@ -997,39 +997,8 @@
 );%0D%0A
-%09%09%09System.out.println(names);%0D%0A
 %09%09%7D%0D
@@ -1485,39 +1485,8 @@
 );%0D%0A
-%09%09%09System.out.println(names);%0D%0A
 %09%09%7D%0D
@@ -1805,57 +1805,8 @@
 );%0D%0A
-%09%09%09System.out.println(nRows + %22 rows changed%22);%0D%0A
 %09%09%7D%0D
@@ -2069,34 +2069,27 @@
 nder = (ps, 
-paramIndex
+seq
 ) -%3E %7B%0D%0A%09%09%09%09
@@ -2097,34 +2097,27 @@
 s.setString(
-paramIndex
+seq
 .next(), %22ja
@@ -2136,34 +2136,27 @@
 s.setString(
-paramIndex
+seq
 .next(), %22he
@@ -2245,57 +2245,8 @@
 );%0D%0A
-%09%09%09System.out.println(nRows + %22 rows changed%22);%0D%0A
 %09%09%7D%0D
@@ -2477,34 +2477,27 @@
 nder = (ps, 
-paramIndex
+seq
 ) -%3E %7B%0D%0A%09%09%09%09
@@ -2505,34 +2505,27 @@
 s.setString(
-paramIndex
+seq
 .next(), %22ja
@@ -2544,34 +2544,27 @@
 s.setString(
-paramIndex
+seq
 .next(), %22he
@@ -2727,26 +2727,19 @@
  = (ps, 
-paramIndex
+seq
 ) -%3E %7B%0D%0A
@@ -2759,18 +2759,11 @@
 ing(
-paramIndex
+seq
 .nex
@@ -2977,57 +2977,8 @@
 );%0D%0A
-%09%09%09System.out.println(nRows + %22 rows changed%22);%0D%0A
 %09%09%7D%0D

improve examples