Reputation: 407
I have an Application which displays a Chart, which is filled with data from an SQL table. I was looking for an possibility to let the user "Edit" the table so he can change the Chart. Today I found the Vaadin "SQLContainer" Add-on which is exactly what I need. I was able to connect to the Database and get the table I need and connect it to a Vaadin Table so I get to see a Database table inside Vaadin. I've read the Vaadin tutorial for SQLContainer (Updated AdressBook Tutorial) quite a few times but I still don’t get how to commit something to the DB via the SQLContainer. This is what I’ve got so far:
public void displayTable(){
try {
connectionPool = new SimpleJDBCConnectionPool(
"org.postgresql.Driver",
"jdbc:postgresql://localhost:5432/database", "username", "password", 2, 5);
FreeformQuery query = new FreeformQuery("select * FROM table", connectionPool);
container = new SQLContainer(query);
container.addListener(new QueryDelegate.RowIdChangeListener() {
public void rowIdChange(RowIdChangeEvent event) {
System.err.println("Old ID: " + event.getOldRowId());
System.err.println("New ID: " + event.getNewRowId());
}
});
} catch (SQLException e) {
e.printStackTrace();
}
table= new Table("Table",container);
table.setSelectable(true);
table.addListener(this);
window.addComponent(table);
}
}
I'm working with Vaadin Version 6.6.6 and I use PostgrSQL.
Upvotes: 1
Views: 6697
Reputation: 297
I see that your query is just "select * FROM table". If this is the case, you should use TableQuery instead of FreeformQuery. TableQuery already has all read/write, sorting, filtering, etc implemented for the case that you are populating the container directly from a single SQL table.
Using TableQuery, you should be able to set the vaadin table in editable mode (setEditable) and directly be able to manipulate values in the database, storing them by calling container.commit() unless it's put in writeThrough and autoCommit modes. Or you can take one row of the table (one Item) and edit it in a Vaadin Form.
If you need to use FreeformQuery (for more complex queries, e.g. JOINing two or more tables) you need to implement write support yourself, as already stated in another answer. For a simple example on this, check out the implementation of the DemoFreeformQueryDelegate for the SQLContainer demo applications.
HTH
Upvotes: 3
Reputation: 6543
When it comes to the SqlContainer the author provides you with he following information
FreeformQuery mode allows you to specify any complex query and have it's results populate the container, however you need to impelment support for writing, sorting, filtering and lazy loading by implementing the FreeformQueryDelegate interface.
I haven't used the SqlContainer myself but here is an sample and the source code for it. If you press a row in the table you will get an option to save/edit data.
Upvotes: 1