Vaadin SQL Container - Table Query



Sometimes I think I am the only Java programmer in Australia who uses the Java Framework called Vaadin. For the most part my colleagues are deeply entrenched in Spring and have no time to lift their heads out of the Spring Documentation to experiment with a different type of Framework.

One of the interesting features of Vaadin is its simple, but effective SQL Container Add On. It provides a simple layer on top of a database. The add on does not provide a JPA interface into the data ( this feature is supported by a different commercial add on ) it does however provide a nice, simple object model for queries and database connections.

This short blog will focus on a Table Query using the SQL Container Add On. I will create other blogs in the future describing more complex features of the Add On.

The first thing you need to establish is a Connection Pool.
            
SimpleJDBCConnectionPool connectionPool =  new SimpleJDBCConnectionPool "com.mysql.jdbc.Driver","jdbc:mysql://localhost:3306/<database name>", "<user>", "<password>", 2, 5);

In this method a Simple Connection Pool object is created against a MySQL Database. The initial connections has been set to two ( 2 ), with a maximum of five ( 5 ) connections allowed.

There is also a J2EE Connection Pool which can be used within J2EE containers which have J2EE data resources. This class simplifies the connection into these resources.

Following the creation of the connection pool you can create a Table Query. This is a component which provides operations on a single database table.

For example if you have a table within your database called suburb, then a Table Query for that table may look like this:

TableQuery q = new TableQuery("suburb", connectionPool);

The TableQuery supports multiple users attempting to write to this table at the same time. This is achieved via an optimistic locking process.

q.setVersionColumn("OPTLOCK");

Once the Table Query is in place, you need to create a SQL Container. A container is an important concept in Vaadin as they are used as components that link the UI to the Data.

SQLContainer container = new SQLContainer(q);

That's it for basic database connections and tables.

More interesting operations to follow in later blogs.



Comments

Popular posts from this blog

Vaadin - GRID Component

Connecting a Vaadin SQL Container to a Combo Box

Vaadin and Google Maps