Java 在GWT的异步方法中传递异常的正确方法是什么?
我不确定在GWT的异步方法中传递异常的正确方法是什么。我有一个数据服务,它连接到数据库,查询信息,然后将信息返回给客户端。但我根本不明白在发生检查异常时该怎么做,以及如何处理它 现在我有这样的东西Java 在GWT的异步方法中传递异常的正确方法是什么?,java,gwt,asynchronous,Java,Gwt,Asynchronous,我不确定在GWT的异步方法中传递异常的正确方法是什么。我有一个数据服务,它连接到数据库,查询信息,然后将信息返回给客户端。但我根本不明白在发生检查异常时该怎么做,以及如何处理它 现在我有这样的东西 public int addAssembler(int assemblerID, String name, boolean active) { Connection con = connect(); PreparedStatement ps = null;
public int addAssembler(int assemblerID, String name, boolean active) {
Connection con = connect();
PreparedStatement ps = null;
ResultSet rs = null;
try {
String query = "INSERT INTO assemblers (name, hidden) VALUES ('" + name + "'," + active + ")";
ps = con.prepareStatement(query);
System.out.println("addAssembler " + ps.toString());
ps.executeUpdate(query);
} catch (SQLException e) {
e.printStackTrace();
} finally {
close(rs, ps, con);
}
return 1;
}
它被称为这里
public void onClick(ClickEvent event) {
Ioma.dataservice.addAssembler(0, name.getText(), !isActive.getValue(), new AsyncCallback<Integer>() {
@Override
public void onFailure(Throwable caught) {
Window.alert("Failed to add assembler! Please check error log" + caught.toString());
}
@Override
public void onSuccess(Integer result) {
Window.alert("Assembler " + name.getText() + " added");
}
});
但它给了我一个SQLException的源代码not found错误。我甚至不确定这样做是否正确?有更好的办法吗 请阅读文档
您不能抛出SQLException
,因为客户端不知道这是什么。您可以做的是定义自己的异常类型(在客户端或共享包中):
然后在服务方法中声明它:
public interface MyService extends RemoteService {
int addAssembler(int assemblerID, String name, boolean active) throws DatabaseException;
}
然后您可以将其扔到服务器端:
public int addAssembler(int assemblerID, String name, boolean active) throws DatabaseException {
Connection con = connect();
PreparedStatement ps = null;
ResultSet rs = null;
try {
String query = "INSERT INTO assemblers (name, hidden) VALUES ('" + name + "'," + active + ")";
ps = con.prepareStatement(query);
System.out.println("addAssembler " + ps.toString());
ps.executeUpdate(query);
} catch (SQLException e) {
throw new DatabaseException(e); // <----- THIS IS IMPORTANT
} finally {
close(rs, ps, con);
}
return 1;
}
顺便说一句:对于所有的
异常,你不必这样做,只有那些不例外的。并且确保将其登录到服务器上。并且认真考虑向客户端发送SQL异常的完整堆栈跟踪和消息-如果发生SQL注入或连接字符串问题,您真的想发送“嘿,这是您打破我的方式!”向浏览器发送消息?
public interface MyService extends RemoteService {
int addAssembler(int assemblerID, String name, boolean active) throws DatabaseException;
}
public int addAssembler(int assemblerID, String name, boolean active) throws DatabaseException {
Connection con = connect();
PreparedStatement ps = null;
ResultSet rs = null;
try {
String query = "INSERT INTO assemblers (name, hidden) VALUES ('" + name + "'," + active + ")";
ps = con.prepareStatement(query);
System.out.println("addAssembler " + ps.toString());
ps.executeUpdate(query);
} catch (SQLException e) {
throw new DatabaseException(e); // <----- THIS IS IMPORTANT
} finally {
close(rs, ps, con);
}
return 1;
}
@Override
public void onFailure(Throwable caught)
{
if(caught instanceof DatabaseException)
Window.alert(caught.getLocalizedMessage());
}