Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You have defined a method </p> <pre><code>public String[] sensors() throws Exception; </code></pre> <p>expecting a return value of String[].</p> <p>In your call, you handle the return value as String:</p> <pre><code> AsyncCallback callback = new AsyncCallback(){ public void onSuccess (Object result){ textbox2.setText((String)result); Hi.setText("You Pass!"); } public void onFailure(Throwable caught) { caught.printStackTrace(); Hi.setText("You fail!"); } }; </code></pre> <p>In fact, it is not a String, it is a String[]. Change your code to:</p> <pre><code> AsyncCallback&lt;String[]&gt; callback = new AsyncCallback&lt;String[]&gt;(){ public void onSuccess (String[] result){ textbox2.setText(result[0]); Hi.setText("You Pass!"); } public void onFailure(Throwable caught) { caught.printStackTrace(); Hi.setText("You fail!"); } }; </code></pre> <p>This will handle the return value correct as String[]. </p> <p>Next thing to improve is your server part:</p> <p>First, if you catch an exception on the server side, you will not throw it to the client. In your case, you catch the SQLException and proceed as if everything is ok on the server. Adding a throw statement will tell the client, that something went wrong on the server.</p> <pre><code>public String[] sensors() throws Exception { String [] user = null; try { conn = getConnection(); rs = pstmt.executeQuery("SELECT ID, times FROM sensor ORDER BY id DESC LIMIT 1;"); // extract data from the ResultSet while (rs.next()) { user = new String[] {rs.getString(1),rs.getString(2)}; } } catch(SQLException e) { e.printStackTrace(); throw new MyException(); } finally { try { rs.close(); pstmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); throw new MyException(); } } return user; }} </code></pre> <p>If you don't find something on your DB, yow will return null. You have to check, if your return value is not null before setting the value to the widget. </p> <pre><code> public void onSuccess (String[] result) { if (result != null) textbox2.setText(result[0]); } Hi.setText("You Pass!"); } </code></pre> <p>I personally prefer using List in this case. Because, you can create the list without knowing, how many values you will return. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload