Easy database queries for Java

Getting Started (JPA)

  1. The full Jinq distribution contains a sample project that demonstrates how to use Jinq. Download it and unpack it somewhere.
  2. Jinq requires Java 8. Be sure you have the latest Java 8 JDK from Oracle installed.
  3. The Jinq download contains a directory called sample with the sample project. The sample code for this program is in the sample/src directory.

Compiling the Sample Program

You must compile the sample code before running it. You can compile it using Maven, Ant, or manually.

Compile Using Maven

There is a Maven pom.xml in the sample directory. You can compile and execute the sample program using

mvn package exec:java

Compile Using Ant

There is an Ant build.xml build file in the sample directory. You can compile and execute the sample program using

ant

Compile Manually

You can compile the sample program from the command-line or using an IDE. Create a project, and include all the source code in the sample/src directory. Ensure that the .jar files in the sample/lib directory are in your classpath. Compile the project and then run the program defined by the class SampleMain.

Understanding the Sample Program

When run, the sample program does these things:

The code is laid out so that

Below is the code for one of the queries in the sample program. It first prints out that it is running a query to find all the customers from the UK. It then executes the query. The query code gets all the customers from the database. It iterates over those customers and keeps only those where the Country field of the customer is equal to "UK". Finally, the results of the query are displayed on the screen.

out.println("CUSTOMERS FROM THE UK");

customers()
   .where(c -> c.getCountry().equals("UK"))
   
   .forEach( c -> 
      out.println(c.getName() + " " + c.getCountry()));

When the code is run, the sample code displays the text below on the screen. It says that it is looking for customers from the UK. It displays the database query that Jinq derived from the sample code and that it is using to find the results. Then it displays the results it found. The sample database only contains one customer from the UK, who is named Dave.

CUSTOMERS FROM THE UK
  SELECT A FROM Customer A WHERE A.country = 'UK'
Dave UK

Jinq Queries