如何在 vaadin 中使 SQLContainer 自动复习 [英] How to make SQLContainer auto refresher in vaadin

查看:19
本文介绍了如何在 vaadin 中使 SQLContainer 自动复习的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 vaadin 应用程序,我使用 SQLContainer 来显示数据库记录.我想让 SQLcontainer 在发生任何数据库更改时自行更新的问题.请问有人帮我吗?

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?

推荐答案

AFAIK 您不能在任何数据库系统(如 MySQL、Oracle 或 MSSQL)中注册更改的侦听器".这不是数据库的预期和架构如何工作.

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.

解决方法是在 Vaadin 中注册 JavaScript 回调并每 x 秒更新一次表.通过这种方式,您甚至不需要启用 PushMode - 它仍然是向服务器发出请求的客户端,而不是其他(肮脏的 imo)方式.

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.

以下示例在 MySQL 和 Vaadin 7.3 上进行了测试:

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();
        }
    });

这篇关于如何在 vaadin 中使 SQLContainer 自动复习的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆