This website uses cookies for visitor traffic analysis. By using the website, you agree with storing the cookies on your computer.More information

Simple DB application with JavaFX

In our last article, we told you that we're working on a JavaFX UI implementation for JVx. It's getting better every day and we think it's time to show you some source code :)

We use a very simple application for database access tests, it's this one:

javafx_DBaccess

It's a simple screen that shows a table of contacts with Search/Insert/Delete/Edit/Export options. There's a detail form that shows an image and all available information as editors. The editors were bound to the table and are always in sync.
You can change the value of an editor and the table will be updated automatically. The editors will be updated, if you change a cell in the table.

The Oracle database tables were created with following statements:

CREATE TABLE ACADEMICTITLES
(
  id             NUMBER(16) NOT NULL,
  academic_title VARCHAR2(200) NOT NULL
);
ALTER TABLE ACADEMICTITLES
  ADD PRIMARY KEY (ID);
ALTER TABLE ACADEMICTITLES
  ADD constraint ACTI_UK UNIQUE (ACADEMIC_TITLE);
CREATE TABLE COUNTRIES
(
  id      NUMBER(16) NOT NULL,
  country VARCHAR2(200) NOT NULL,
  eu      CHAR(1) DEFAULT 'N' NOT NULL
);
ALTER TABLE COUNTRIES
  ADD PRIMARY KEY (ID);
ALTER TABLE COUNTRIES
  ADD constraint CTRY_UK UNIQUE (COUNTRY);
CREATE TABLE HEALTHINSURANCES
(
  id               NUMBER(16) NOT NULL,
  health_insurance VARCHAR2(200) NOT NULL
);
ALTER TABLE HEALTHINSURANCES
  ADD PRIMARY KEY (ID);
ALTER TABLE HEALTHINSURANCES
  ADD constraint HEIN_UK UNIQUE (HEALTH_INSURANCE);
CREATE TABLE SALUTATIONS
(
  id         NUMBER(16) NOT NULL,
  salutation VARCHAR2(200)
)
ALTER TABLE SALUTATIONS
  ADD PRIMARY KEY (ID);
ALTER TABLE SALUTATIONS
  ADD constraint SALU_UK UNIQUE (SALUTATION);
CREATE TABLE CONTACTS
(
  id          NUMBER(16) NOT NULL,
  salu_id     NUMBER(16),
  acti_id     NUMBER(16),
  firstname   VARCHAR2(200) NOT NULL,
  lastname    VARCHAR2(200) NOT NULL,
  street      VARCHAR2(200),
  nr          VARCHAR2(200),
  zip         VARCHAR2(4),
  town        VARCHAR2(200),
  ctry_id     NUMBER(16),
  birthday    DATE,
  hein_id     NUMBER(16),
  filename    VARCHAR2(200),
  image       BLOB
);
ALTER TABLE CONTACTS
  ADD PRIMARY KEY (ID);
ALTER TABLE CONTACTS
  ADD constraint CONT_ACTI_ID_FK FOREIGN KEY (ACTI_ID)
  REFERENCES ACADEMICTITLES (ID);
ALTER TABLE CONTACTS
  ADD constraint CONT_CTRY_ID_FK FOREIGN KEY (CTRY_ID)
  REFERENCES COUNTRIES (ID);
ALTER TABLE CONTACTS
  ADD constraint CONT_HEIN_ID_FK FOREIGN KEY (HEIN_ID)
  REFERENCES HEALTHINSURANCES (ID);
ALTER TABLE CONTACTS
  ADD constraint CONT_SALU_ID_FK FOREIGN KEY (SALU_ID)
  REFERENCES SALUTATIONS (ID);

CREATE sequence SEQ_CONTACTS_ID
minvalue 1
maxvalue 9999999999999999
start WITH 1
increment BY 1
nocache
ORDER;
/

CREATE OR REPLACE TRIGGER TR_CONTACTS_BR_I
  before INSERT ON CONTACTS
  FOR each row
begin

  IF :new.id IS NULL then
    SELECT seq_contacts_id.NEXTVAL INTO :new.id FROM dual;
  end IF;

end;
/

You can see that the table definition contains foreign keys for every selection (like salutations or academic titles). It's a clean relational database model. All foreign keys are combobox editors editors in our GUI:

FK Editors

FK Editors

The birthday field is a date editor:

Date editor

Date editor

Here's the full source code of the application and I'll explain all relevant parts in detail:

package com.sibvisions.apps.javafx.db;

import javax.rad.application.IContent;
import javax.rad.application.genui.Application;
import javax.rad.application.genui.UILauncher;
import javax.rad.genui.celleditor.UIDateCellEditor;
import javax.rad.genui.celleditor.UIImageViewer;
import javax.rad.genui.celleditor.UINumberCellEditor;
import javax.rad.genui.component.UILabel;
import javax.rad.genui.container.UIGroupPanel;
import javax.rad.genui.container.UIPanel;
import javax.rad.genui.container.UISplitPanel;
import javax.rad.genui.control.UIEditor;
import javax.rad.genui.layout.UIBorderLayout;
import javax.rad.genui.layout.UIFormLayout;
import javax.rad.model.ModelException;
import javax.rad.ui.IContainer;
import javax.rad.util.event.IExceptionListener;

import com.sibvisions.apps.components.FilterEditor;
import com.sibvisions.apps.components.NavigationTable;
import com.sibvisions.rad.persist.StorageDataBook;
import com.sibvisions.rad.persist.jdbc.DBAccess;
import com.sibvisions.rad.persist.jdbc.DBStorage;

public class SimpleDBApplication extends Application
                                 implements IExceptionListener
{
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // Class members
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    private static final String            NO_IMAGE    =
                       "/com/sibvisions/apps/vxdemo/images/nobody.gif";
    private static final UIImageViewer    IMAGE_VIEWER =
                       new UIImageViewer(NO_IMAGE);

    private StorageDataBook sdbContacts;
    private UIBorderLayout  blThis             = new UIBorderLayout();
    private UISplitPanel    splitMain          = new UISplitPanel();
    private NavigationTable navContacts        = new NavigationTable();

    private UIFormLayout    flDetails          = new UIFormLayout();
    private UIFormLayout    layoutDetails      = new UIFormLayout();
    private UIPanel         panDetails         = new UIPanel();
    private UIGroupPanel    gpanDedails        = new UIGroupPanel("Contact");
    private UIBorderLayout  blContacts         = new UIBorderLayout();
    private UIPanel         panContacts        = new UIPanel();
    private UIFormLayout    layoutSearch       = new UIFormLayout();
    private UIPanel         panSearch          = new UIPanel();
    private UILabel         lblSalutation      = new UILabel("Salutation");
    private UILabel         lblAcademicTitle   = new UILabel("Academic title");
    private UILabel         lblFirstName       = new UILabel("First name");
    private UILabel         lblLastName        = new UILabel("Last name");
    private UILabel         lblStreet          = new UILabel("Street");
    private UILabel         lblNr              = new UILabel("Nr");
    private UILabel         lblZip             = new UILabel("ZIP");
    private UILabel         lblTown            = new UILabel("Town");
    private UILabel         lblBirthday        = new UILabel("DoB");
    private UILabel         lblHealthInsurance = new UILabel("Health insurance");
    private UILabel         lblSearch          = new UILabel("Search");
    private UIEditor        edtSalutation      = new UIEditor();
    private UIEditor        edtAcademicTitle   = new UIEditor();
    private UIEditor        edtFirstName       = new UIEditor();
    private UIEditor        edtLastName        = new UIEditor();
    private UIEditor        edtStreet          = new UIEditor();
    private UIEditor        editNr             = new UIEditor();
    private UIEditor        edtZip             = new UIEditor();
    private UIEditor        edtTown            = new UIEditor();
    private UIEditor        edtCountry         = new UIEditor();
    private UIEditor        edtBirthday        = new UIEditor();
    private UIEditor        edtHealthInsurance = new UIEditor();
    private FilterEditor    edtSearch          = new FilterEditor();
    private UIEditor        icoImage           = new UIEditor();
   
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // Initialization
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    /**
     * Creates a new instance of <code>Showcase</code>.
     *
     * @param pLauncher the launcher
     * @throws Throwable if the initialization failed
     */

    public SimpleDBApplication(UILauncher pLauncher) throws Throwable
    {
        super(pLauncher);
       
        setName("Simple DB application");
       
        initModel();
        initUI();
    }

    /**
     * Initializes model.
     *
     * @throws ModelException
     */

    private void initModel() throws ModelException
    {
        DBAccess dba = DBAccess.getDBAccess("jdbc:oracle:thin:@localhost:1521:xe");
        dba.setUsername("vxdemo");
        dba.setPassword("vxdemo");
        dba.open();
       
        DBStorage dbsContacts = new DBStorage();
        dbsContacts.setDBAccess(dba);
        dbsContacts.setWritebackTable("CONTACTS");
        dbsContacts.open();
       
        sdbContacts = new StorageDataBook(dbsContacts);
        sdbContacts.open();
       
        sdbContacts.getRowDefinition().getColumnDefinition("IMAGE").
             getDataType().setCellEditor(IMAGE_VIEWER);

        sdbContacts.getRowDefinition().getColumnDefinition("SOCIALSECNR").
             getDataType().setCellEditor(new UINumberCellEditor("0000"));
        sdbContacts.getRowDefinition().getColumnDefinition("BIRTHDAY").
             getDataType().setCellEditor(new UIDateCellEditor("dd.MM.yyyy"));
    }

    /**
     * Initializes UI.
     *
     * @throws Throwable if the initialization failed
     */

    private void initUI() throws Throwable
    {
        navContacts.setDataBook(sdbContacts);
        navContacts.setAutoResize(false);

        icoImage.setDataRow(sdbContacts);
        icoImage.setColumnName("IMAGE");
        icoImage.setPreferredSize(200, 140);

        edtSearch.setDataRow(sdbContacts);
        edtSalutation.setDataRow(sdbContacts);
        edtSalutation.setColumnName("SALU_SALUTATION");
        edtAcademicTitle.setDataRow(sdbContacts);
        edtAcademicTitle.setColumnName("ACTI_ACADEMIC_TITLE");
        edtFirstName.setDataRow(sdbContacts);
        edtFirstName.setColumnName("FIRSTNAME");
        edtLastName.setDataRow(sdbContacts);
        edtLastName.setColumnName("LASTNAME");
        edtStreet.setDataRow(sdbContacts);
        edtStreet.setColumnName("STREET");
        editNr.setDataRow(sdbContacts);
        editNr.setColumnName("NR");
        edtZip.setDataRow(sdbContacts);
        edtZip.setColumnName("ZIP");
        edtTown.setDataRow(sdbContacts);
        edtTown.setColumnName("TOWN");
        edtCountry.setDataRow(sdbContacts);
        edtCountry.setColumnName("CTRY_COUNTRY");
        edtBirthday.setDataRow(sdbContacts);
        edtBirthday.setColumnName("BIRTHDAY");
        edtHealthInsurance.setDataRow(sdbContacts);
        edtHealthInsurance.setColumnName("HEIN_HEALTH_INSURANCE");

        panSearch.setLayout(layoutSearch);
        panSearch.add(lblSearch, layoutSearch.getConstraints(0, 0));
        panSearch.add(edtSearch, layoutSearch.getConstraints(1, 0, -1, 0));

        panContacts.setLayout(blContacts);
        panContacts.add(panSearch, UIBorderLayout.NORTH);
        panContacts.add(navContacts, UIBorderLayout.CENTER);

        gpanDedails.setLayout(flDetails);
        gpanDedails.add(icoImage, flDetails.getConstraints(0, 0, 1, 7));
        flDetails.setHorizontalGap(15);
        gpanDedails.add(edtSalutation, flDetails.getConstraints(3, 0));
        flDetails.setHorizontalGap(5);
        gpanDedails.add(edtAcademicTitle, flDetails.getConstraints(3, 1));

        flDetails.setHorizontalGap(15);
        gpanDedails.add(lblSalutation, flDetails.getConstraints(2, 0));
        flDetails.setHorizontalGap(5);
        gpanDedails.add(lblAcademicTitle, flDetails.getConstraints(2, 1));
        gpanDedails.add(lblFirstName, flDetails.getConstraints(2, 2));
        gpanDedails.add(edtFirstName, flDetails.getConstraints(3, 2, -1, 2));
        gpanDedails.add(lblLastName, flDetails.getConstraints(2, 3));
        gpanDedails.add(edtLastName, flDetails.getConstraints(3, 3, -1, 3));
        gpanDedails.add(lblBirthday, flDetails.getConstraints(2, 4));
        gpanDedails.add(edtBirthday, flDetails.getConstraints(3, 4, 3, 4));
        gpanDedails.add(lblHealthInsurance,
                        flDetails.getConstraints(2, 5));
        gpanDedails.add(edtHealthInsurance,
                        flDetails.getConstraints(3, 5, -1, 5));
        gpanDedails.add(lblStreet, flDetails.getConstraints(2, 6));
        gpanDedails.add(edtStreet, flDetails.getConstraints(3, 6, -3, 6));
        gpanDedails.add(lblNr, flDetails.getConstraints(-2, 6));
        gpanDedails.add(editNr, flDetails.getConstraints(-1, 6));
        gpanDedails.add(lblZip, flDetails.getConstraints(2, 7));
        gpanDedails.add(edtZip, flDetails.getConstraints(3, 7));
        gpanDedails.add(lblTown, flDetails.getConstraints(4, 7));
        gpanDedails.add(edtTown, flDetails.getConstraints(5, 7, -1, 7));

        panDetails.setLayout(layoutDetails);
        panDetails.add(gpanDedails,
                       layoutDetails.getConstraints(0, 0, -1, 0));

        splitMain.setDividerPosition(250);
        splitMain.setDividerAlignment(UISplitPanel.DIVIDER_TOP_LEFT);
        splitMain.setFirstComponent(panContacts);
        splitMain.setSecondComponent(panDetails);

        setLayout(blThis);
        add(splitMain, UIBorderLayout.CENTER);
       
        setPreferredSize(1024, 768);
    }
   
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // Interface implementation
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    public IContainer getContentPane()
    {
        return this;
    }

    public <OP> IContent showMessage(OP pOpener, int pIconType,
                                     int pButtonType, String pMessage,
                                     String pOkAction, String pCancelAction)
                            throws Throwable
    {
        System.out.println(pMessage);
       
        return null;
    }

    public void handleException(Throwable pThrowable)
    {
        pThrowable.printStackTrace();
    }
   
}

The application is really minimal and doesn't use a menu or toolbar, it's naked and only a frame with a panel in it. The class has 3 important parts. The first is the constructor where it gets the UILauncher:

public SimpleDBApplication(UILauncher pLauncher) throws Throwable
{
    super(pLauncher);
       
    setName("Simple DB application");
       
    initModel();
    initUI();
}

The launcher is responsible for starting an application, in our case it's the JavaFX implementation and the application will be started as real JavaFX application. The constructor sets the name (= frame title) and initializes our model and user interface.

The model is "mixed" in our simple application because we have a fat client that connects directly to the database without communication or application server. The initialization is short:

DBAccess dba = DBAccess.getDBAccess("jdbc:oracle:thin:@localhost:1521:xe");
dba.setUsername("demo");
dba.setPassword("demo");
dba.open();
               
DBStorage dbsContacts = new DBStorage();
dbsContacts.setDBAccess(dba);
dbsContacts.setWritebackTable("CONTACTS");
dbsContacts.open();
               
sdbContacts = new StorageDataBook(dbsContacts);
sdbContacts.open();

We create a new connection to our Oracle database via DBAccess and create a DBStorage for our CONTACTS table. The "real model" is our StorageDataBook because it's the class which is needed from our UI. The rest of the code:

sdbContacts.getRowDefinition().getColumnDefinition("IMAGE").
     getDataType().setCellEditor(IMAGE_VIEWER);

sdbContacts.getRowDefinition().getColumnDefinition("SOCIALSECNR").
     getDataType().setCellEditor(new UINumberCellEditor("0000"));
sdbContacts.getRowDefinition().getColumnDefinition("BIRTHDAY").
     getDataType().setCellEditor(new UIDateCellEditor("dd.MM.yyyy"));

is just column configuration and formatting, e.g. use an image viewer instead of a text editor.

The UI initialization is trivial, because it creates UI components, sets a layout and adds the components to the main panel.The interesting part in the initUI method is the model binding:

navContacts.setDataBook(sdbContacts);
...
edtSalutation.setDataRow(sdbContacts);
edtSalutation.setColumnName("SALU_SALUTATION");
edtAcademicTitle.setDataRow(sdbContacts);
edtAcademicTitle.setColumnName("ACTI_ACADEMIC_TITLE");
edtFirstName.setDataRow(sdbContacts);
edtFirstName.setColumnName("FIRSTNAME");

The first command sets the contacts databook as "datasource" for the Table view. A navigation table is a simple Table view with additional buttons for Inserting/Deleting/Editing/Exporting records.

The next command is interesting because it binds the contacts databook to an editor:

edtSalutation.setDataRow(sdbContacts);
edtSalutation.setColumnName("SALU_SALUTATION");

but with a column name which doesn't exist in our database table: SALU_SALUTATION.

This is a special feature of DBStorage because it checks foreign key columns and creates dynamic "not existing" columns for referenced tables. It's a little bit magic but it reduces source code to a bare minimum. The contacts storage also knows how to fetch such "not existing" columns because we don't have storages or databooks for our SALUTATIONS or ACADEMICTITLES table. We don't need references because our contacts storage handles everything. Oh.. this magic can be deactivated and it's always possible to do the same programatically, but we love the so called "automatic link detection".

The rest of the class is not relevant, e.g.

public IContainer getContentPane()
{
    return this;
}

public <OP> IContent showMessage(OP pOpener, int pIconType,
                                 int pButtonType, String pMessage,
                                 String pOkAction, String pCancelAction)
                        throws Throwable
{
    System.out.println(pMessage);
       
    return null;
}

public void handleException(Throwable pThrowable)
{
    pThrowable.printStackTrace();
}

It's only the implementation of some abstract and interface methods, but it has no functionality.

And that was all you need to create a simple CRUD application. It's fully functional and allows Insert/Update/Edit operations without additional code. It makes no difference if you edit directly in the table or in the form via editors. The editors work automatically with the right datatype and show a combobox in case of FK columns and a date chooser in case of a date datatype, e.g.:

Combo Box

Combo Box

There's no need to create a java.sql.Connection manually or use java.sql.PreparedStatement for insert/update/delete. This funcationality was encapsulated in JVx' DBStorage and we simply use the functionality for our JavaFX UI and in all other UI technologies as well.

Better Lambda support for JVx

I wrote about JVx and Java 8, Events and Lambdas some weeks ago.

It was a first test with JVx events and Lambdas and it was great to see that everything worked as expected, BUT not all JVx events were supported because not all listeners were defined as SAM type.

It was great to see that Lambdas were supported with our event handling implementation but it was a shame that only action events and some model events were really supported. So we started thinking :)

Our key listener was defined like this:

public interface IKeyListener
{
    public void keyTyped(UIKeyEvent pKeyEvent);

    public void keyPressed(UIKeyEvent pKeyEvent);

    public void keyReleased(UIKeyEvent pKeyEvent);
}

The problem was that there were 2 methods too much. So we split the interface in 3 new interfaces:

  • IKeyPressedListener
  • IKeyReleasedListener
  • IKeyTypedListener

Every listener has exactly one method, e.g.

public interface IKeyPressedListener
{
    public void keyPressed(UIKeyEvent pKeyEvent);
}

The new listener, for backwards compatibility, is this:

public interface IKeyListener extends IKeyTypedListener,
                                      IKeyPressedListener,
                                      IKeyReleasedListener
{
}

Our listener are handled from event handlers, like KeyHandler. The old implementation was:

public class KeyHandler extends RuntimeEventHandler<IKeyListener>
{
    public KeyHandler(String pListenerMethodName)
    {
        super(IKeyListener.class, pListenerMethodName);
    }
}

(The handler creation with a method name wasn't really typesafe but it worked)

We refactored the handler a little bit and the result is:

public class KeyHandler<L> extends RuntimeEventHandler<L>
{
    public KeyHandler(Class<L> pClass)
    {
        super(pClass);
    }  
}

After some refactoring, our listeners were Lambda and backwards compatible, e.g.:

userName.eventKeyPressed().addListener(e -> System.out.println(e.getKeyChar()));

The next nightly build will contain all changes!
There are still some smaller todos, but Lambdas are well supported.

VisionX 2.2 Preview Release

The first preview release of VisionX 2.2 is available. The exact version number is 2.2.41.

It available as trial version or for our customers via download area.

The preview release contains updates of all dependent frameworks like JVx and vaadin UI.

Here's a list with more details:

  • Responsive web applications

    Window shutter web control

  • Browser navigation
  • Application styles
  • Support for embedded devices
  • Style definition with VisionX
    Style definition

    Style definition

  • New EPlug support
  • Custom Database connection strings (e.g. Oracle TNS entry)
    Custom DB URL

    Custom connection string

  • Live Template support for work-screens
  • XLSX reports instead of XLS
  • Ready for application monitoring
  • UI improvements
  • Ready for native Android client

Sure, we fixed some bugs and we also improved overall performance because of improvements in JVx. The most interesting thing is the support for embedded devices like RaspberryPi.

Have fun with VisionX :)

Plain JDBC vs. DBStorage

Did you ever use plain JDBC? For sure.

Wasn't it horrible because of so much boilerplate code? There are different solutions like Hibernate, EclipseLink, MyBatis, .... But such libraries create mappings, annotations, xml files, POJOs, ....

The idea behind JDBC is great but the API generates a lot of code and work.

I want to show you how easy DB access could be, without overhead.
First I want to define the table for our test:

CREATE TABLE t_calculate
(
  id integer GENERATED BY DEFAULT AS IDENTITY(start WITH 1) PRIMARY KEY,
  costs decimal(10) DEFAULT 0,
  factor decimal(2) DEFAULT 10,
  STATUS char(1) DEFAULT '?',
  description varchar(100)
)

The following snippet opens a HSQLDB connection inserts 3 records, checks the auto-generated id of the first inserted record, counts the number of records and does some filtering by id and value.

Here's the code:

//OPEN DB connection        
Class.forName("org.hsqldb.jdbcDriver");

Connection connection = DriverManager.getConnection(
                           "jdbc:hsqldb:hsql://localhost/testdb", "sa", "");

try
{
    //pre-create statements
    PreparedStatement psInsert = connection.prepareStatement(
                                       "insert into t_calculate (costs) values (?)");
    PreparedStatement psFetchById  = connection.prepareStatement(
                                       "select * from t_calculate where id = ?");

    try
    {
        //INSERT first record
        psInsert.setObject(1, BigDecimal.valueOf(35));
        psInsert.execute();
       
        //check generated ID
        CallableStatement csId = connection.prepareCall("CALL IDENTITY()");
        ResultSet resKey = csId.executeQuery();
       
        if (resKey.next())
        {
            Long id = (Long)resKey.getObject(1);
           
            psFetchById.setObject(1, id);
           
            ResultSet res = psFetchById.executeQuery();
           
            try
            {
                if (res.next())
                {
                    Assert.assertEquals(Long.valueOf(1), id);
                    Assert.assertEquals(BigDecimal.valueOf(10), res.getBigDecimal("FACTOR"));
                    Assert.assertEquals("?", res.getString("STATUS"));
                }
                else
                {
                    Assert.fail("Couldn't fetch record!");
                }
            }
            finally
            {
                res.close();
            }
        }
        else
        {
            Assert.fail("Couldn't fetch identity!");
        }
       
        //INSERT second record
        psInsert.setObject(1, BigDecimal.valueOf(40));
        psInsert.execute();
       
        //INSERT third record
        psInsert.setObject(1, BigDecimal.valueOf(45));
        psInsert.execute();
 
        //Check record count
        Statement stmt = connection.createStatement();
       
        ResultSet res = stmt.executeQuery("select count(*) from t_calculate");
       
        try
        {
            if (res.next())
            {
                Assert.assertEquals(3, res.getInt(1));
            }
            else
            {
                Assert.fail("Couldn't fetch record count!");
            }
        }
        finally
        {
            res.close();
        }
       
        //FILTER by ID
        psFetchById.setObject(1, Long.valueOf(2));
       
        try
        {
            res = psFetchById.executeQuery();

            if (res.next())
            {
                Assert.assertEquals(40, res.getInt("COSTS"));
            }
            else
            {
                Assert.fail("Couldn't fetch records with id = 1!");
            }
        }
        finally
        {
            res.close();
        }
       
        //FILTER by value (COSTS)
        PreparedStatement psFetchByCosts  = connection.prepareStatement(
                                     "select count(*) from t_calculate where costs >= ?");
        psFetchByCosts.setObject(1, BigDecimal.valueOf(40));
       
        try
        {
            res = psFetchByCosts.executeQuery();
           
            try
            {
                if (res.next())
                {
                    Assert.assertEquals(2, res.getInt(1));
                }
                else
                {
                    Assert.fail("Couldn't fetch records with costs >= 40!");
                }
            }
            finally
            {
                res.close();
            }
        }
        finally
        {
            psFetchByCosts.close();
        }
    }
    finally
    {
        psInsert.close();
        psFetchById.close();
    }
}
finally
{
    connection.close();
}

Nice, isn't it. Hopefully I didn't forget a close() call. The use-case was simple and not really complex compared to real world problems, but the LoC are extreme. All together 144 lines (comments included).

Sure you could write a small utility to auto-close statements and to read result sets, but why re-inventing the wheel? The most problems seem to be trivial but gets really complex.

I want to show you different solutions with DBStorage from JVx framework. It's a small class with extreme power. It handles whole JDBC for you, solves CRUD and more. The class is a server-side class and was designed for multi-tier environments but can also be used directly on client-side.

Here's the code, for the same use-case as before:

DBAccess dba = DBAccess.getDBAccess("jdbc:hsqldb:hsql://localhost/testdb");
dba.setUsername("sa");
dba.setPassword("");
dba.open();

try
{
    DBStorage dbs = new DBStorage();
    dbs.setDBAccess(dba);
    dbs.setWritebackTable("t_calculate");
    dbs.open();
   
    //inserting records
    Bean bean35 = new Bean();
    bean35.put("COSTS", BigDecimal.valueOf(35));
   
    bean35 = dbs.insert(bean35);

    Bean bean40 = new Bean();
    bean40.put("COSTS", BigDecimal.valueOf(40));
   
    bean40 = dbs.insert(bean40);

    Bean bean45 = new Bean();
    bean45.put("COSTS", BigDecimal.valueOf(45));

    bean45 = dbs.insert(bean45);
   
    Assert.assertEquals(BigDecimal.valueOf(1), bean35.get("ID"));
    Assert.assertEquals(BigDecimal.valueOf(10), bean35.get("FACTOR"));
    Assert.assertEquals("?", bean35.get("STATUS"));
   
    //filtering
    List<IBean> liBeans = dbs.fetchBean(null, null, 0, -1);
   
    Assert.assertEquals(3, liBeans.size());
   
    IBean bean = dbs.fetchBean(new Equals("ID", BigDecimal.valueOf(2)));
   
    Assert.assertEquals(BigDecimal.valueOf(40), bean.get("COSTS"));
   
    liBeans = dbs.fetchBean(new GreaterEquals("COSTS", BigDecimal.valueOf(40)), null, 0, -1);
   
    Assert.assertEquals(2, liBeans.size());
}
finally
{
    CommonUtil.close(dba);
}

Better? Only 49 LoC.

As I told you before, the DBStorage is a server-side class and the API wasn't designed as client API. But there are some very useful classes in JVx. We call them DataBooks. A databook can be compared to a database table because it is row and column oriented. We have different DataBooks like MemDataBook, RemoteDataBook or StorageDataBook. MemDataBook is the base of all our databooks and RemoteDataBook extends it. The StorageDataBook extends the RemoteDataBook. A remote databook gets its records from a remote storage. The storage databook directly gets its records from a DBStorage.
Sounds tricky? A little bit, but these classes allow using multi-tier architectures without making a difference how many tiers you use.

Here's the same example again, but using a StorageDataBook for using a DBStorage with client API:

DBAccess dba = DBAccess.getDBAccess("jdbc:hsqldb:hsql://localhost/testdb");
dba.setUsername("sa");
dba.setPassword("");
dba.open();

try
{
    DBStorage dbs = new DBStorage();
    dbs.setDBAccess(dba);
    dbs.setWritebackTable("t_calculate");
    dbs.open();

    StorageDataBook sdb = new StorageDataBook(dbs);
    sdb.open();
   
    //inserting records
    sdb.insert(false);
    sdb.setValues(new String[] {"COSTS", "DESCRIPTION"},
                  new Object[] {BigDecimal.valueOf(35), "Record with costs = 35"});
    sdb.insert(false);
    sdb.setValues(new String[] {"COSTS", "DESCRIPTION"},
                  new Object[] {BigDecimal.valueOf(40), "Record with costs = 40"});
    sdb.insert(false);
    sdb.setValues(new String[] {"COSTS", "DESCRIPTION"},
                  new Object[] {BigDecimal.valueOf(45), "Record with costs = 45"});
   
    sdb.saveAllRows();
   
    sdb.setSelectedRow(0);
   
    Assert.assertEquals(BigDecimal.valueOf(1), sdb.getValue("ID"));
    Assert.assertEquals(BigDecimal.valueOf(10), sdb.getValue("FACTOR"));
    Assert.assertEquals("?", sdb.getValue("STATUS"));

    //filtering
    Assert.assertEquals(3, sdb.getRowCount());
   
    sdb.setFilter(new Equals("ID", BigDecimal.valueOf(2)));
   
    Assert.assertEquals(1, sdb.getRowCount());
   
    Assert.assertEquals(BigDecimal.valueOf(40), sdb.getValue("COSTS"));
   
    sdb.setFilter(new GreaterEquals("COSTS", BigDecimal.valueOf(40)));

    Assert.assertEquals(2, sdb.getRowCount());
}
finally
{
    CommonUtil.close(dba);
}

The LoC: 51 (but we could save 3 lines).
The code doesn't use a remote storage, because we don't use a multi-tier architecture, but how does it look with a multi-tier architecture?

The server in our example is a Tomcat application server and we connect via HttpConnection. You could also use different server implementations like vert.x or a plain socket server.

Here is the code:

HttpConnection con = new HttpConnection("http://localhost/myapp/services/Server");

MasterConnection macon = new MasterConnection(con);
macon.open();

try
{
    RemoteDataSource rds = new RemoteDataSource(macon);
    rds.open();
   
    RemoteDataBook rdb = new RemoteDataBook();
    rdb.setDataSource(rds);
    rdb.setName("calculate");
   
    //inserting records
    rdb.insert(false);
    rdb.setValues(new String[] {"COSTS", "DESCRIPTION"},
                  new Object[] {BigDecimal.valueOf(35), "Record with costs = 35"});
    rdb.insert(false);
    rdb.setValues(new String[] {"COSTS", "DESCRIPTION"},
                  new Object[] {BigDecimal.valueOf(40), "Record with costs = 40"});
    rdb.insert(false);
    rdb.setValues(new String[] {"COSTS", "DESCRIPTION"},
                  new Object[] {BigDecimal.valueOf(45), "Record with costs = 45"});
   
    rdb.saveAllRows();
   
    rdb.setSelectedRow(0);
   
    Assert.assertEquals(BigDecimal.valueOf(1), rdb.getValue("ID"));
    Assert.assertEquals(BigDecimal.valueOf(10), rdb.getValue("FACTOR"));
    Assert.assertEquals("?", rdb.getValue("STATUS"));

    //filtering
    Assert.assertEquals(3, rdb.getRowCount());
   
    rdb.setFilter(new Equals("ID", BigDecimal.valueOf(2)));
   
    Assert.assertEquals(1, rdb.getRowCount());
   
    Assert.assertEquals(BigDecimal.valueOf(40), rdb.getValue("COSTS"));
   
    rdb.setFilter(new GreaterEquals("COSTS", BigDecimal.valueOf(40)));

    Assert.assertEquals(2, rdb.getRowCount());
}
finally
{
    CommonUtil.close(macon);
}

LoC: 50 (but we could save the same 3 lines as before)

The API calls are the same, but we used a HttpConnection, a MasterConnection, a RemoteDataSource and a RemoteDataBook. The HttpConnection could be replaced with e.g. NetConnection or any other transport protocol. The MasterConnection is protocol independent and handles the communication between client and server-tier. The RemoteDataSource is the datasource for RemoteDataBook. The RemoteDataBook communicates indirectly with a DBStorage on the server-tier.

The DBStorage definition for the server-side is the same as in all other examples:

DBAccess dba = DBAccess.getDBAccess("jdbc:hsqldb:hsql://localhost/testdb");
dba.setUsername("sa");
dba.setPassword("");
dba.open();

but the server-tier contains the definition. The client doesn't know connection settings and credentials in that case.

Sure, there's a difference between fat-client, client/server and multi-tier architecture, but have you ever seen an easier API to use a multi-tier architecture?

The DBStorage and DataBooks have a lot of useful APIs for you and your database applications. It solves all CRUD operations, take care of column metadata and much more.

JVx is Apache 2.0 licensed.

JVx application on embedded devices

In the last weeks, we did many experiments with JVx on Raspberry Pi, especially using a Pi as server for JVx applications. Our vision was that the business logic could run on a Pi without Java application server like Tomcat or Jetty. Our example application was a simple sensor recorder. We read temperature data and stored the read data into a JavaDB (on the same device). We knew that Jetty and Tomcat worked on a Pi but it was not necessary to run an application server without web content.

We tried different implementations:

  • Fun

    A recorder class that saved data directly in the JavaDB. A good old Java application with one DBStorage because plain JDBC was painful.

  • Because JVx can do that

    A simple recorder client with embedded JVx server. The client was a simple class with a DirectServerConnection. The server saved retrieved temperature data in the JavaDB.

  • Production

    The same simple recorder client but the server run standalone. The DirectServerConnection was replaced with a Netconnection.

The first implementation was for fun only and not relevant for our tests. Our Pi should be a server that measures data and allows accessing data from JavaDB (for charts). Some real client applications could use the temperature server for integrating temperature data. The same application could use other Sensor data from different RasPis (measuring lux, webcam, ...) - a RasPi (sensor) network.

The big problem was that JVx server didn't work without an application server. It worked embedded and via http, but we didn't have a plain socket implementation. It wasn't a JVx problem because the server component was ready for socket connections but we didn't have a Socket server. We made some experiments with vert.x and the result was a nice socket server. We also used JVx with vert.x Http server. Both implementations are available on GitHub. It worked like a charm but we didn't like the library dependencies. We already had a socket server API for our product VisionX, but it wasn't decoupled. Now it is!

We used the VisionX socket API and merged it in our applications library. After some modifications and bugfixes, in JVx, it's now possible to use it without additional dependencies. It's a simple socket server for JVx server component. We've used this server to run VisionX and had no problems. VisionX is a huge JVx application and if VisionX works, all other JVx applications will work.

It's very simple to use the the socket server:

NetServer server = new NetServer("servername", 556);
server.start();

The server has a main method and you can use it without additional coding. Simply set the system properties server.hostname, server.port.

To open a connection, simply call:

IConnection con = new NetConnection("servername", 556);

MasterConnection appcon = new MasterConnection(con);
       
appcon.setApplicationName("tempsensor");
appcon.setUserName("user");
appcon.setPassword("password");
appcon.open();

Not really rocket-science and just works!

The only problem was the server name and port configuration, for every client. Not really cool in a "sensor network". So we started a simple Broadcaster. It's a simple class that checks the network for existing servers. It's easy to use on client-side:

List<ConnectInfo> liConInfo = Broadcaster.search("MyAPP");

and the server side isn't too complex:

Broadcaster broadcaster = new Broadcaster();
broadcaster.setServerPort(556);
broadcaster.setIdentifier("MyAPP");
broadcaster.start();

The result contains a list of available hostnames and ports for the first found server. It's still space for a better solution, but I think that's the reason why vert.x uses hazelcast.

To cut a long story short: It's now possible to use complete JVx applications without Java application server, especially on embedded devices like RaspberryPi.

Sure, it's still recommended to use an application server if your device serves web content or a whole web application e.g. with JVx' vaadin UI (see IoT: Window shutter control).

R&D GitHub repositories

We pushed some of our research projects to GitHub. Our company account currently contains 3 projects:

  • jvx.vert.x

    A complete JVx connection implementation for vert.x. It contains a NetSocket and Http Server.

  • DropboxStorage

    A storage for accessing Dropbox files and directories. Use it to access Dropbox files from JVx applications.

  • flylink

    Flyspray integration for TestLink.

We'll use GitHub for smaller projects and finished research projects. Our SourceForge account will be used for all other projects. We do some tests with GitHub and maybe we'll move more projects from time to time.

JVx and JavaFX (fasten your seat belts)

Today is a great day, because I'm able to write about our latest Research Project.
It has to do with JavaFX and JVx - what else.

If you remember one of my blog postings in 2013, you know that we tried to implement our JVx UI with JavaFX. Long story, short: Not possible with one master thesis.

But... we always try to reach our goals and it's an honor to tell you that we're back on the track.

Our JavaFX UI is one of the coolest things on this IT planet.

It's cool because of JavaFX. It's APIs are still not perfect and the overall performance could be better but it has so many awesome things. Two of them are styling via CSS and animations. JavaFX is the official replacement for Swing and it has potential to bring back "write once run anywhere". We don't know if JavaFX will be the holy grail for desktop application development but there's nothing comparable in the Java world.
To be honest, it's not a problem for us because our JVx is (UI) technology independent and if JavaFX won't do it, we'll implement the next UI toolkit for JVx.

... but back to the topic

You should already know that JVx is a Full-stack application framework for building database (ERP) software. It's GUI technology independent and we've different UI implementations. The most important ones are based on Swing and vaadin. We also have native Android and iOS support and our applications run on embedded devices as well. Our applications don't need an application server to work properly because we have a plain socket server as well (IoT ready).

Our preferred Java UI technology was and is still Swing, because it's feature rich and you can build every kind of application without bigger problems. The problem with swing is that it's old and not maintained from Oracle. Sure it will work with future Java releases without bigger problems (hopefully) but you can't use modern development techniques or create fancy and fresh UIs with less effort. A standard Swing application looks boring and cold. Sure, it's possible to pimp a swing application with lots of pictures or nice looking LaFs, but it's not fun.

Swing development is like software development in the stone-age, compared to state-of-the-art web development. If you do web development, you have CSS3 and HTML5 and some nice designed UI frameworks. I won't say that you're faster or that web development is better but it's easier to build nice looking UIs. I'm not a big fan of classical web development because most applications are horrible to maintain, there are soo many different frameworks and you can't be sure that your investment is future-proof because many frameworks don't survive the first year. But everything depends on knowledge and the right framework - for sure.

IMO it's better to use one framework that solves most of application relevant problems - if it exists. If we talk about web applications, I recommend vaadin because it's feature rich, just works, is very polished and is licensed under Apache 2.0. The community is active and great support is available!
Sure, there are some other frameworks like GXT or extJS from Sencha, but the license could be a problem if you build commercial applications. Don't forget RAP, but it's not trivial to start with the whole Eclipse ecosystem.

I'm talking about (enterprise) application development and not about webpage or webservice development. I would recommend pure HTML5 and CSS3 with one or two smaller javascript libs for developing fancy "webpages", or php instead of Java. If Java and "webpages", PrimeFaces is a good decision.

Soo, if Swing is "dead" and web development is hard, what should we use for (enterprise) application development?

If you develop for the desktop, try to start with JavaFX becasue it's maintained and has potential for mobile devices.
If you develop web applications, use vaadin.

(This is my personal opinion, mixed with my technology and project experience)

And why should you use JVx?

JVx bundles all together in one independent framework (it's still small and easy to learn). You'll be independent of the underlying UI technology. Write your application once and start it with different UI technologies or use it on different devices without problems. Your investment is future-proof. JVx is open source and licensed under Apache 2.0. JVx doesn't hide the underlying technology. It's always possible to use features from the technology if you want. If you use a UI without abstraction, you can't switch to another UI easily without rewriting most parts of the UI. If you start with e.g. JavaFX and if Oracle will abandon JavaFX, will you rewrite the application?

You can compare the concept of JVx with SLF4J, but with a different use-case: (enterprise) application development

And what about JavaFX?

I like JavaFX because of, IMO, 5 great things: scene graph, CSS styling, Animations, Web engine and the community. All other things are important and useful but not essential for real-world applications (vs. Swing). It's not possible to compare Swing with JavaFX because JavaFX was written from scratch and Swing was based on AWT. Sure, both toolkits are UI toolkits :) but with different concepts.

JavaFX bridges the gap between desktop and web development. It's still a toolkit for desktop applications but with most advantages of web development. It's possible to create fancy applications without headache. There are cool tools like Scene Builder or ScenicView. IDE support is getting better and developing controls is not too hard.

Sounds too good? JavaFX is on the right track and ready for real-world applications.

That was enough for us to start a JavaFX UI for JVx. It's the only framework which allows switching between Swing, Web, JavaFX or native mobile. Sounds unbelievable? Here's is our first video with our JavaFX implementation. It demonstrates the full power of JVx and JavaFX.

The video starts with our good old Swing UI. The same application will be shown as web application in a Chrome browser. And finally it will run as JavaFX application. You should carefully check the JavaFX application because it has an embedded browser that shows the same application, with a different UI technology - at the same time. Don't miss the animations :)

It's a world premiere!

JVx and JavaFX



We know that the styling is not really cool but we're currently in the development phase and aren't finished... be tolerant :)

JVx with vert.x

We had our first contact with vert.x in 2012 as we tried to use it for our first RaspberryPi project. We did a simple implementation of our JVx connection interface and used the Vertx net implementation to run our applications without application server on a RasPi. Our implementation was based on vert.x 1.2. We didn't use our implementation since that time. We re-used the project in the last weeks because vert.x is still alive and we changed some things in our connection to support streaming protocols. It was a great test case for our changes.

First of all, we did an update to vert.x 2.1.5. Our implementation is now up-to-date. We also changed the implementation because it wasn't an optimized solution. We removed our internal buffering and used vert.x core classes. We also created verticles to support the core concept of vert.x.

If you're interested, simply check the source code. There are two test cases: TestNetSocketConnection and TestHttpConnection in the test folder.

You could use the solution to run a JVx application on an embedded device :) without a Java application server.

But you should know that this project isn't an official project of SIB Visions, and we don't offer support for it. But we try to answer your questions via Forum.

The project is currently located in the JVx repository, but we'll move it to its own repository in next few weeks.