Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Gain SQL SELECT functionality in Java

Select, filter, and sort collections or arrays of objects with a simple mechanism

  • Print
  • Feedback

Suppose you want to display data contained in an array or collection in a table-like format, i.e., in rows and columns. You want to be able to choose which attributes to display. In addition, you want to only display a subset of the data that meet certain conditions, and you want to sort that data based on numerous sorting criteria and define a preference order for those criteria.

This is the type of functionality that SQL SELECT provides: In a SQL SELECT statement, you can define your datasource (the FROM clause); you define which attributes (columns) you'd like to see (the SELECT clause); you can add conditions (the WHERE clause); and you can define the order in which the data is presented (the ORDER BY clause).

This article describes a number of classes and interfaces that allow you to apply the same functionality to an array or a collection of objects of any type. To achieve this functionality, the mechanism described uses some design patterns.

The approach presented here has the following benefits:

  1. This article's code (available for download from Resources) allows you to present your data in many different ways without affecting the original data
  2. Sorting, filtering, and selecting the appropriate attributes are simplified, and results are quickly achieved
  3. The classes and interfaces permit you to think of each part of the SELECT clause separately, allowing you to produce neat, reusable, and extendable code


However, as you might guess, this mechanism employs generic interfaces that can be abused when misunderstood (more on that later).

The mechanism's classes and interfaces

The following list gives a short description of the classes and interfaces we need. Later you will see how they all work together to achieve the desired functionality. Each class and interface is quite simple, consisting of one or two methods.

  1. Interface Invoker defines only one method: public Object invoke(Object o). It allows you to wrap a method call in an interface and return the method's result. Later, you will see how it can be used.
  2. Interface Condition also defines one method: public boolean passes(Object o). It allows you to check any kind of object for any kind of condition and return either true or false. Again, you will soon learn how to use this interface.
  3. Class SelectStatement represents a complete SELECT statement containing a SELECT, FROM, WHERE, and ORDER BY clause.
  4. Class SelectInstruction represents a single part of a SELECT clause, namely a column and a column name. An entire SELECT clause is represented by an array of SelectInstruction objects.
  5. Class OrderInstruction represents a single part of an ORDER BY clause. An entire ORDER BY clause is represented by an array of OrderInstruction objects.

  6. Class Executor executes the SelectStatement, which contains the entire execution logic.
  7. Class ResultTable represents the data returned by executing the SelectStatement. It contains the result data in the form of an Object[][], allowing you to easily traverse and display the data.


Now, let's see how all these classes and interfaces work together. This class diagram illustrates how they relate to each other.

  • Print
  • Feedback

Resources