How to make SQLContainer auto refresher in vaadin [closed]

泄露秘密 提交于 2019-12-24 22:26:33

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!