简体   繁体   English

GWT、RPC 和 SQL,您的结果集是保留在服务器端还是可以在客户端使用它?

[英]GWT, RPC, and SQL, does your resultset remain server-side or can you utilise it client side?

I have briefly changed from Android to GWT to make my app work online now.我已经从 Android 简单地更改为 GWT,以使我的应用程序现在可以在线运行。

I realise that there are many differences from Android apps to GWT projects, but one of them, I cannot wrap my head around, so maybe some one of you can clear it up for me.我意识到从 Android 应用程序到 GWT 项目有很多不同之处,但其中之一,我无法理解,所以也许你们中的某个人可以为我澄清一下。

I have access to my database through RPC and I can send this data to my GUI through a DefaultCallback with an onFailure and OnSuccess.我可以通过 RPC 访问我的数据库,我可以通过带有 onFailure 和 OnSuccess 的 DefaultCallback 将此数据发送到我的 GUI。 Like this:像这样:

@Override
public void sayHello(String name) {
    this.serviceAsync.sayHello(name,  new DefaultCallback());
}

    private class DefaultCallback implements AsyncCallback {

    @Override
    public void onFailure(Throwable caught) {
        System.out.println("An error has occured");
    }

    @Override
    public void onSuccess(Object result) {
        System.out.println("Response received");
        if (result instanceof String) {
            mainGUI.updateLabel(String.valueOf(result));
        }
    }
}

SQL comes into the play as I access my database here, right?, so let us say I want to fill a table of accounts.当我在这里访问我的数据库时, SQL开始发挥作用,对吧?,让我们说我想填充一个帐户表。 I would access the table, make a loop, and then populate the table.我会访问表格,进行循环,然后填充表格。

I utilise the SQL like so:我像这样使用 SQL:

        Connection c = null;

    try {
        Class.forName("org.sqlite.JDBC");
        c = DriverManager.getConnection("server");
    }
    catch (Exception e) {
        System.err.println(e.getClass().getName() + ": " + e.getMessage());
    }
    return c;

And a resultset like so:和这样的结果集:

    ResultSet myGet(Connection db, String sql){
    try {
        return db.createStatement().executeQuery(sql);
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return null;
}

However, going from a databaseHelper in Android where I could easily run a cursor and then utilise my data from that cursor in loops and so on, I find this "defaultCallback" method above to be very very rigid.但是,从 Android 中的 databaseHelper 开始,我可以轻松地运行游标,然后在循环中利用来自该游标的数据等等,我发现上面的“defaultCallback”方法非常严格。

As such, my question is: Is there a tip/trick to add some flexibility to this?因此,我的问题是:是否有提示/技巧可以为此增加一些灵活性? So as to make it more similar to the way you would utilise a resultSet in Java or a Cursor in Android?为了使它更类似于您在 Java 中使用 resultSet 或在 Android 中使用 Cursor 的方式?

First I would not go with GWT RPC nowadays.首先,我现在不会使用 GWT RPC。 I would go with a REST-ful approach.我会采用 REST-ful 方法。 For example domino-rest ( https://github.com/DominoKit/domino-rest ) is a great implementation to use on the client side.例如 domino-rest ( https://github.com/DominoKit/domino-rest ) 是在客户端使用的一个很好的实现。 On the server side, you can use Spring Boot or jersey or something like that to provide a restful service.在服务器端,您可以使用 Spring Boot 或 jersey 或类似的东西来提供宁静的服务。 Besides that you need a separate data model to transport your result to the client (which needs to be serialized and deserialized for transport).除此之外,您需要一个单独的数据模型来将您的结果传输到客户端(需要对其进行序列化和反序列化以进行传输)。 On the server side you'll have a resource, which does the database request and map the result into the data object.在服务器端,您将拥有一个资源,它执行数据库请求并将结果映射到数据对象中。

Here you'll find an example:在这里你会找到一个例子:

https://github.com/NaluKit/nalu-examples/tree/master/devkexample https://github.com/NaluKit/nalu-examples/tree/master/devkexample

Inside the PersonService-class ( https://github.com/NaluKit/nalu-examples/blob/master/devkexample/devkexample-server/src/main/java/de/gishmo/example/devk/server/service/PersonService.java ) you'll do your db request and map your data.在 PersonService 类( https://github.com/NaluKit/nalu-examples/blob/master/devkexample/devkexample-server/src/main/java/de/gishmo/example/devk/server/service/PersonService. java ) 你会做你的数据库请求并映射你的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM