我现在正在使用rpc从我的数据库中提取数据,但每当我点击该按钮时,它确实会生成错误并且我的onFailure
命令始终被触发。
以下是该计划的代码。
public class PleaseWork implements EntryPoint {
private TextBox textbox2 = new TextBox();
private Label Hi = new Label("New label");
@SuppressWarnings("deprecation")
public void onModuleLoad() {
RootPanel rootPanel = RootPanel.get();
Button btnNewButton = new Button("New button");
btnNewButton.addClickListener(new ClickListener() {
public void onClick(Widget event) {
HelpConnectionAsync Abra =(HelpConnectionAsync) GWT.create(HelpConnection.class);
ServiceDefTarget target = (ServiceDefTarget) Abra;
String moduleRelativeURL = GWT.getModuleBaseURL() + "MySQLConnection";
target.setServiceEntryPoint(moduleRelativeURL);
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!");
}
};
Abra.sensors(callback);
}
});
rootPanel.add(btnNewButton, 23, 30);
rootPanel.add(textbox2,23, 70);
rootPanel.add(Hi, 23, 130);
}
}
这包括我的异步和服务程序
public interface HelpConnection extends RemoteService {
public String[] sensors() throws Exception;}
public interface HelpConnectionAsync {
public void sensors(AsyncCallback<String[]> callback);}
这是我的服务器端程序
public class MySQLConnection extends RemoteServiceServlet implements HelpConnection {
private Connection conn = null;
private ResultSet rs = null;
private PreparedStatement pstmt = null;
public static Connection getConnection() throws Exception {
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/temperature";
String username = "root";
String password = "123456";
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, username, password);
return conn;
}
@Override
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();
} finally {
try {
rs.close();
pstmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return user;
}}
最后这是控制台生成的代码
java.lang.NullPointerException
at com.google.gwt.user.client.rpc.core.java.lang.NullPointerException_FieldSerializer.instantiate(NullPointerException_FieldSerializer.java:16)
at com.google.gwt.user.client.rpc.core.java.lang.NullPointerException_FieldSerializer.create(NullPointerException_FieldSerializer.java:25)
at com.google.gwt.user.client.rpc.impl.SerializerBase.instantiate(SerializerBase.java:115)
at com.google.gwt.user.client.rpc.impl.ClientSerializationStreamReader.deserialize(ClientSerializationStreamReader.java:396)
at com.google.gwt.user.client.rpc.impl.AbstractSerializationStreamReader.readObject(AbstractSerializationStreamReader.java:119)
at com.google.gwt.user.client.rpc.impl.RequestCallbackAdapter.onResponseReceived(RequestCallbackAdapter.java:216)
at com.google.gwt.http.client.Request.fireOnResponseReceived(Request.java:258)
at com.google.gwt.http.client.RequestBuilder$1.onReadyStateChange(RequestBuilder.java:412)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172)
at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessagesWhileWaitingForReturn(BrowserChannelServer.java:338)
at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:219)
at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:136)
at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:571)
at com.google.gwt.dev.shell.ModuleSpace.invokeNativeObject(ModuleSpace.java:279)
at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeObject(JavaScriptHost.java:91)
at com.google.gwt.core.client.impl.Impl.apply(Impl.java)
at com.google.gwt.core.client.impl.Impl.entry0(Impl.java:242)
at sun.reflect.GeneratedMethodAccessor38.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172)
at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessages(BrowserChannelServer.java:293)
at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:547)
at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:364)
at java.lang.Thread.run(Unknown Source)
开发模式错误
[ERROR] Uncaught exception escaped
com.google.gwt.event.shared.UmbrellaException:捕获异常:未在com.google.gwt.com上的com.google.gwt.event.shared.HandlerManager.fireEvent(HandlerManager.java:129)指定服务实现URL .user.client.ui.Widget.fireEvent(Widget.java:129)位于com.google.gwt.vent.client的com.google.gwt.event.dom.client.DomEvent.fireNativeEvent(DomEvent.java:116) .ui.Widget.onBrowserEvent(Widget.java:177)com.google.gwt.user.client.DOM.dispatchEventImpl(DOM.java:1351)com.google.gwt.user.client.DOM.dispatchEvent(DOM) .java:1307)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)at java.lang.reflect.Method。在com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)的com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)上调用(未知来源)。 google.gwt.dev.shell.OophmSessionHandler.i nvoke(OophmSessionHandler.java:172)com.google.gwt.dev.shell.BrowserChannelServer.reactToMessagesWhileWaitingForReturn(BrowserChannelServer.java:338)com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:219) com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:136)com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:571)com.google.gwt.dev .shell.ModuleSpace.invokeNativeObject(ModuleSpace.java:279),位于com.google.gwt.core.client.impl.Impl.apply的com.google.gwt.dev.shell.JavaScriptHost.invokeNativeObject(JavaScriptHost.java:91) (Impl.java)com.google.gwt.core.client.impl.Impl.entry0(Impl.java:242)at sun.reflect.GeneratedMethodAccessor43.invoke(Unknown Source)at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown)来自com.google.gwt.devoke.Movod.invoke(MethodAdaptor.java:103)的com.google.gwt.dev上的java.lang.reflect.Method.invoke(未知来源) .shell.MethodDispatch.invoke(MethodDispatch.java:71)位于com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172)com.google.gwt.dev.shell.BrowserChannelServer.reactToMessages(BrowserChannelServer) .java:293)在com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:547)的java上com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:364)。 lang.Thread.run(Unknown Source)引起:com.google.gwt.user.client.rpc.ServiceDefTarget $ NoServiceEntryPointSpecifiedException:未在com.google.gwt.user.client.rpc.impl.RemoteServiceProxy中指定服务实现URL。 doPrepareRequestBuilderImpl(RemoteServiceProxy.java:430)at com.google.gwt.user.client.rpc.impl.RemoteServiceProxy.doInvoke(RemoteServiceProxy.java:368)at com.google.gwt.user.client.rpc.impl.RemoteServiceProxy $ com.mycompany.project.client.HelpConnection_Proxy.sensors上的ServiceHelper.finish(RemoteServiceProxy.java:74)(帮助Connection_Proxy.java :))com.mycompany.project.client.PleaseWork $ 1.onClick(PleaseWork.java:30)at com.google.gwt.user.client.ui.ListenerWrapper $ WrappedClickListener.onClick(ListenerWrapper.java:245 )com.google.gwt.event.dom.client.ClickEvent.dispatch(ClickEvent.java:56)com的com.google.gwt.event.dom.client.ClickEvent.dispatch(ClickEvent.java:1)。 go.com.gwt.event.shared.GwtEvent.dispatch(GwtEvent.java:1)位于com.google.web.bindery的com.google.web.bindery.event.shared.EventBus.dispatchEvent(EventBus.java:40)。 event.shample.SimpleEventBus.doFire(SimpleEventBus.java:193)位于com.google.web.bindery.event.shampleEventBus.fireEvent(SimpleEventBus.java:88)的com.google.gwt.event.shared.HandlerManager。 fireEvent(HandlerManager.java:127)在com.google.gwt.user.client.ui.Widget.fireEvent(Widget.java:129)com.google.gwt.event.dom.client.DomEvent.fireNativeEvent(DomEvent。 java:116)在com.google.gwt.user.client.ui.Widget.onBrowserEvent(Widget.java:177) com.google.gwt.user.client.DOM.dispatchEventImpl(DOM.java:1351)at com.google.gwt.user.client.DOM.dispatchEvent(DOM.java:1307)at sun.reflect.NativeMethodAccessorImpl.invoke0 (本机方法)at sun.reflect.NativeMethodAccessorImpl.invoke(未知来源)at the sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)at java.lang.reflect.Method.invoke(Unknown Source)at com.google.gwt.dev .shell.MethodAdaptor.invoke(MethodAdaptor.java:103)位于com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler) .java:172)在com.google.gwt.dev.shell.BrowserChannelServer.reactToMessagesWhileWaitingForReturn(BrowserChannelServer.java:338)com的com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:219)。 go.com.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:136)com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:571) )com.google.gwt.dev.shell.ModuleSpace.invokeNativeObject(ModuleSpace.java:279)com.google.gwt.dev.shell.JavaScriptHost.invokeNativeObject(JavaScriptHost.java:91)com.google.gwt。 core.client.impl.Impl.apply(Impl.java)位于sun.reflect.GeneratedMethodAccessor43.invoke(未知来源)的com.google.gwt.core.client.impl.Impl.entry0(Impl.java:242) sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)at java.lang.reflect.Method.invoke(Unknown Source)at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)com.google位于com.google.gwt.dev.shell.BrowserChannelServer的com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172)上的.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71) .reactToMessages(BrowserChannelServer.java:293)位于com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:547)com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer) .java:364)at java.lang.Thread.run(Unknown Source)
另一种基于更改返回User的尝试返回String [] {&#34; text1&#34;,&#34; text2&#34;} 再次在Developmentmode中产生了这个错误,
[ERROR] Uncaught exception escaped
java.lang.ClassCastException:[Ljava.lang.String;不能在com.mycompany.project.client.PleaseWork $ 1 $ 1.onSuccess(PleaseWork.java:40)的com.google.gwt.user.client.rpc.impl.RequestCallbackAdapter.onResponseReceived(RequestCallbackAdapter)中转换为java.lang.String。 .java:232)com.google.gwt.http.client.Request.fireOnResponseReceived(Request.java:258)at com.google.gwt.http.client.RequestBuilder $ 1.onReadyStateChange(RequestBuilder.java:412)at sun .reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)at java.lang.reflect.Method.invoke(Unknown Source)at com .google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)位于com.google.gwt.devoke(MododDispatch.invoke(MethodDispatch.java:71)的com.google.gwt.dev.shell com.google.gwt.dev.shell.BrowserChannelServer.reactToMessagesWhileWaitingForReturn(BrowserChannelServer)中的.OophmSessionHandler.invoke(OophmSessionHandler.java:172) .java:338)com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:219)com的com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:136)。 go.com.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:571)位于com.google.gwt.dev.shell.ModuleSpace.invokeNativeObject(ModuleSpace.java:279)的com.google.gwt.dev.shell。 com.google.gwt.core.client.impl.Impl.apply(Impl.java)上的JavaScriptHost.invokeNativeObject(JavaScriptHost.java:91),位于com.google.gwt.core.client.impl.Impl.entry0(Impl。 java. 242)at sun.reflect.GeneratedMethodAccessor43.invoke(Unknown Source)at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)at java.lang.reflect.Method.invoke(Unknown Source)at com.google.gwt.dev .shell.MethodAdaptor.invoke(MethodAdaptor.java:103)位于com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler) .java:172)at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessages(BrowserChannelServer.java:293)位于com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:547)com.google.gwt.dev。 java.lang.Thread.run(未知来源)中的shell.BrowserChannelServer.run(BrowserChannelServer.java:364)
答案 0 :(得分:0)
您已定义方法
public String[] sensors() throws Exception;
期望String []的返回值。
在您的调用中,您将返回值作为String处理:
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!");
}
};
实际上,它不是String,而是String []。将您的代码更改为:
AsyncCallback<String[]> callback = new AsyncCallback<String[]>(){
public void onSuccess (String[] result){
textbox2.setText(result[0]);
Hi.setText("You Pass!");
}
public void onFailure(Throwable caught) {
caught.printStackTrace();
Hi.setText("You fail!");
}
};
这将以String []的形式处理正确的返回值。
接下来需要改进的是您的服务器部分:
首先,如果您在服务器端捕获异常,则不会将其丢给客户端。在您的情况下,您捕获SQLException并继续,就好像服务器上的一切正常。添加throw语句将告诉客户端服务器上出现了问题。
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;
}}
如果您在数据库中找不到任何内容,那么您将返回null。在将值设置为窗口小部件之前,如果返回值不为null,则必须检查。
public void onSuccess (String[] result) {
if (result != null)
textbox2.setText(result[0]);
}
Hi.setText("You Pass!");
}
我个人更喜欢在这种情况下使用List。因为,您可以在不知道的情况下创建列表,返回多少个值。
答案 1 :(得分:0)
您是否有理由手动设置ServiceDefTarget
?如果没有,您可以针对onModuleLoad()
方法试用此代码吗?
@SuppressWarnings("deprecation")
public void onModuleLoad() {
RootPanel rootPanel = RootPanel.get();
Button btnNewButton = new Button("New button");
HelpConnectionAsync Abra =(HelpConnectionAsync) GWT.create(HelpConnection.class);
btnNewButton.addClickListener(new ClickListener() {
public void onClick(Widget event) {
Abra.sensors(new AsyncCallback<String[]>(){
public void onSuccess (String[] result){
if (result == null) {
Hi.setText("Result was null!");
return;
}
textbox2.setText(result[0]);
Hi.setText("You Pass!");
}
public void onFailure(Throwable caught) {
caught.printStackTrace();
Hi.setText("You fail!");
}
});
}
});
rootPanel.add(btnNewButton, 23, 30);
rootPanel.add(textbox2,23, 70);
rootPanel.add(Hi, 23, 130);
}
另外,在您的服务器方法中,在catch
区块中,您是否可以抓住Exception
而不是SQLException
?我怀疑有些东西逃脱了。
答案 2 :(得分:0)
我从您的PleaseWork课程中删除了ServiceDefTarget。它只是一个工作的基础,现在你只需添加数据库处理。我也改变了回调的类型。它现在适用于我(我删除了一些代码以减少代码量):
public void onModuleLoad() {
RootPanel rootPanel = RootPanel.get();
Button btnNewButton = new Button("New button");
btnNewButton.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
HelpConnectionAsync Abra = (HelpConnectionAsync) GWT
.create(HelpConnection.class);
AsyncCallback<String[]> callback = new AsyncCallback<String[]>() {
public void onSuccess(String[] result) {
textbox2.setText(result[0]);
Hi.setText("You Pass!");
}
public void onFailure(Throwable caught) {
caught.printStackTrace();
Hi.setText("You fail!");
}
};
Abra.sensors(callback);
}
});
在您的原始帖子中没有servlet映射,所以我添加了它:
@RemoteServiceRelativePath("Connection")
public interface HelpConnection extends RemoteService {
public String[] sensors() throws Exception;}
asyn对手:
public interface HelpConnectionAsync {
public void sensors(AsyncCallback<String[]> callback);}
MySQLConnection的重要部分:
@SuppressWarnings("serial")
public class MySQLConnection extends RemoteServiceServlet implements HelpConnection {
@Override
public String[] sensors() throws Exception {
String [] user = {"test"};
return user;
}}
除了Java类之外,我还使用了以下web.xml(/war/WEB-INF/web.xml)
....
<!-- Servlets -->
<servlet>
<servlet-name>servletName</servlet-name>
<servlet-class>src.server.MySQLConnection</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servletName</servlet-name>
<url-pattern>/helpconnection/Connection</url-pattern>
</servlet-mapping>
...
此外,我无法拒绝将已弃用的ClickListener替换为未弃用的ClickListener。