Main features:

  1. Single configuration file

  2. Auto synchronization for database in case of JPA entities (thanks to Hibernate)

  3. Elegant and simple API (proven to work with minimum Java experience)

  4. Single API to communicate with databases with both: JDBC (SQL inside Java) and JPA (No SQL, just Java classes with mapping to database).

  5. Convention for keeping SQL queries inside external files, perfectly makes send to.

  6. Built in caching mechanism.

Configurations

The framework run on local file based H2 database by default, so basically no configuration needed. This could be good idea for test automation, proof-of-concepts, or trying things out. However, for real-life applications, you will need to control the configurations.

To manage the configuration of your database(s), please refer to the Config Guide.

Basic Example:

[source, java]
package com.jk.example;

import java.util.List;
import java.util.Map;

import com.jk.db.dataaccess.core.JKDataAccessService;
import com.jk.db.datasource.JKDataSourceFactory;
import com.jk.util.JK;

public class JdbcBasicExample {
	public static void main(String[] args) {
		//Create Dao Instance
		JKDataAccessService dao = JKDataSourceFactory.getDataAccessService();

		if (!dao.isTableExists("ACCOUNTS")) {
			dao.runScript("/script.sql");
			dao.execute("INSERT INTO ACCOUNTS VALUES(?,?,?)", 5, "Jalal", 100);
			dao.execute("INSERT INTO ACCOUNTS VALUES(?,?,?)", 6, "Ata", 200);
			dao.execute("INSERT INTO ACCOUNTS VALUES(?,?,?)", 7, "Essa", 300);
			dao.execute("INSERT INTO ACCOUNTS VALUES(?,?,?)", 8, "Jamal", 400);
		}
		// return single results
		JK.line();
		long result = dao.executeQueryAsLong("SELECT BALANCE FROM ACCOUNTS WHERE ID=?", 5);
		JK.print(result);

		// return single row
		JK.line();
		Object[] row = dao.executeQueryAsRow("SELECT * FROM ACCOUNTS WHERE ID=?", 5);
		JK.print(row);

		// return multiple rows
		JK.line();
		List<List<Object>> rows = dao.executeQueryAsList("SELECT * FROM ACCOUNTS WHERE BALANCE>?", 100);
		for (List<Object> eachRow : rows) {
			JK.print(eachRow);
		}

		JK.line();
		// execute query from file, it will look into "/src/main/resources/jk/sql"
		// folder for the scripts file
		List<List<Object>> rows2 = dao.executeQueryAsList("all_accounts.sql", 100);
		for (List<Object> eachRow : rows2) {
			JK.print(eachRow);
		}

		// execute query and fill directly into a bean, the default is to have the same
		// name in both, bean and the tables
		JK.line();
		List<Account> rows3 = dao.executeQueryAsListOfObjects(Account.class, "all_accounts.sql", 100);

		for (Account account : rows3) {
			JK.print(account);
		}

		// execute query and fill directly into bean, and map the fields using Map
		// object
		JK.line();

		Map<String, Object> fieldsMapping = JK.toMap("accountId", "id", "accountName", "name", "accountBalance","balance");
		List<Account2> rows4 = dao.executeQueryAsListOfObjects(Account2.class, fieldsMapping, "all_accounts.sql", 100);
		for (Account2 account : rows4) {
			JK.print(account);
		}
	}
}