简体   繁体   中英

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.

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.

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. 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. I would access the table, make a loop, and then populate the table.

I utilise the SQL like so:

        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.

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?

First I would not go with GWT RPC nowadays. I would go with a REST-ful approach. For example domino-rest ( https://github.com/DominoKit/domino-rest ) is a great implementation to use on the client side. On the server side, you can use Spring Boot or jersey or something like that to provide a restful service. 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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