问题
i have a vaadin application, and i used SQLContainer to display database records. the issue that i want to make the SQLcontainer update itself when any database change occurs. cam anyone help me please?
回答1:
AFAIK you can't register a "listener" on changes in any database systems like MySQL, Oracle or MSSQL. Its not how databases are intended and architectured to work.
Workaround is to register JavaScript callback in Vaadin and update table every x seconds. This way you won't even need to enable PushMode - it will be still the client who will make requests to the server and not the other (dirty imo) way around.
Following example was tested on MySQL and Vaadin 7.3:
final VerticalLayout layout = new VerticalLayout();
setContent(layout);
final Table table = new Table("Table");
try
{
JDBCConnectionPool connectionPool = new SimpleJDBCConnectionPool("com.mysql.jdbc.Driver", "jdbc:mysql://localhost:3306/test", "root", "");
QueryDelegate qd = new FreeformQuery("select * from testTable", connectionPool, "id");
final SQLContainer c = new SQLContainer(qd);
table.setContainerDataSource(c);
}
catch (SQLException e)
{
e.printStackTrace();
}
layout.addComponent(table);
JavaScript.getCurrent().execute("setInterval(function(){refreshTable();},5000);");
JavaScript.getCurrent().addFunction("refreshTable", new JavaScriptFunction()
{
@Override
public void call(JSONArray arguments) throws JSONException
{
table.refreshRowCache();
}
});
来源:https://stackoverflow.com/questions/27483684/how-to-make-sqlcontainer-auto-refresher-in-vaadin