当我运行我的项目时,我收到错误消息“RPC 调用失败”。这将来自我的 LoginView 类 AuthenticationHandler 类。此外,ClickHandler 中的窗口警报正在显示在“aa/bb”页面中输入的值。MySQL 和 Tomcat 已启动,我已尝试通过遵循众多在线指南和论坛来确保安装所有驱动程序。但是,我怀疑我在这方面做得不对。但是,我可能是错的。有人可以让我知道要检查什么以及如何解决它。细节将不胜感激,因为我是新手。
我“运行为”Web 应用程序并且未启用 GAE(我提到这一点,因为我发现这是另一个问题的原因)。我有一个 MySQL 数据库奖励跟踪器,其中包含一个 DBuser 用户和密码 DBpass 设置,其中包含一个包含数据的表。
错误日志显示了我在 MySQLConnection 类中插入的消息“连接到数据库时出错 - 不好”。以下是日志和类:
安慰:
java.sql.SQLException: No suitable driver found for jbdc:mysql://localhost:3306/awardtracker
Error connecting to database - not good eh
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at org.AwardTracker.server.MySQLConnection.<init>(MySQLConnection.java:23)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at java.lang.Class.newInstance0(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
at org.mortbay.jetty.servlet.Holder.newInstance(Holder.java:153)
at org.mortbay.jetty.servlet.ServletHolder.getServlet(ServletHolder.java:339)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:463)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:362)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:729)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:405)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.handler.RequestLogHandler.handle(RequestLogHandler.java:49)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:324)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:505)
at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:843)
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:647)
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:211)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:380)
at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:395)
at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:488)
Starting Jetty on port 8888
[WARN] Exception while dispatching incoming RPC call
com.google.gwt.user.server.rpc.UnexpectedException: Service method 'public abstract org.AwardTracker.client.User org.AwardTracker.client.DBConnection.authenticateUser(java.lang.String,java.lang.String)' threw an unexpected exception: java.lang.NullPointerException
at com.google.gwt.user.server.rpc.RPC.encodeResponseForFailure(RPC.java:389)
at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:579)
at com.google.gwt.user.server.rpc.RemoteServiceServlet.processCall(RemoteServiceServlet.java:208)
at com.google.gwt.user.server.rpc.RemoteServiceServlet.processPost(RemoteServiceServlet.java:248)
at com.google.gwt.user.server.rpc.AbstractRemoteServiceServlet.doPost(AbstractRemoteServiceServlet.java:62)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:487)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:362)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:729)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:405)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.handler.RequestLogHandler.handle(RequestLogHandler.java:49)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:324)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:505)
at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:843)
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:647)
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:211)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:380)
at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:395)
at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:488)
Caused by: java.lang.NullPointerException
at org.AwardTracker.server.MySQLConnection.authenticateUser(MySQLConnection.java:37)
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.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:561)
... 22 more
[ERROR] 500 - POST /org.AwardTracker.AwardTracker/MySQLConnection (127.0.0.1) 57 bytes
Request headers
Accept: */*
X-GWT-Permutation: HostedMode
X-GWT-Module-Base: http://127.0.0.1:8888/org.AwardTracker.AwardTracker/
Content-Type: text/x-gwt-rpc; charset=utf-8
Referer: http://127.0.0.1:8888/AwardTracker.html?gwt.codesvr=127.0.0.1:9997
Accept-Language: en-au
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; MAAU)
Host: 127.0.0.1:8888
Content-Length: 199
Connection: Keep-Alive
Cache-Control: no-cache
Response headers
Content-Type: text/plain
DBConnection 类
package org.AwardTracker.client;
import com.google.gwt.user.client.rpc.RemoteService;
/**
* The client side stub for the RPC service.
*/
public interface DBConnection extends RemoteService {
public User authenticateUser(String user, String pass);
}
用户类
package org.AwardTracker.client;
import com.google.gwt.user.client.rpc.IsSerializable;
public class User implements IsSerializable {
@SuppressWarnings("unused")
private String username;
@SuppressWarnings("unused")
private String password;
@SuppressWarnings("unused")
private User() {
//just here because GWT wants it.
}
public User(String username, String password) {
this.username = username;
this.password = password;
}
}
登录视图类
package org.AwardTracker.client;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
public class LoginView extends Composite {
private TextBox textBoxUsername;
private PasswordTextBox passwordTextBox;
private DBConnectionAsync rpc;
public LoginView(final NavHandler navHandler) {
rpc = (DBConnectionAsync) GWT.create(DBConnection.class);
ServiceDefTarget target = (ServiceDefTarget) rpc;
String moduleRelativeURL = GWT.getModuleBaseURL() + "MySQLConnection";
target.setServiceEntryPoint(moduleRelativeURL);
VerticalPanel verticalPanel = new VerticalPanel();
initWidget(verticalPanel);
Label lblWelcomeToThe = new Label("Welcome to the Award Tracker login page");
lblWelcomeToThe.setStyleName("gwt-Label-Login");
verticalPanel.add(lblWelcomeToThe);
Label lblSignInTo = new Label("Please sign in to your account");
lblSignInTo.setStyleName("gwt-Label-Login");
verticalPanel.add(lblSignInTo);
FlexTable flexTable = new FlexTable();
verticalPanel.add(flexTable);
Label lblUserName = new Label("Username:");
lblUserName.setStyleName("gwt-Label-Login");
flexTable.setWidget(0, 0, lblUserName);
lblUserName.setHeight("12px");
textBoxUsername = new TextBox();
flexTable.setWidget(0, 1, textBoxUsername);
Label lblNewLabel = new Label("Password:");
lblNewLabel.setStyleName("gwt-Label-Login");
flexTable.setWidget(1, 0, lblNewLabel);
passwordTextBox = new PasswordTextBox();
flexTable.setWidget(1, 1, passwordTextBox);
CheckBox chckbxRememberMeOn = new CheckBox("Remember me on this computer");
chckbxRememberMeOn.setStyleName("gwt-Checkbox-Login");
flexTable.setWidget(2, 1, chckbxRememberMeOn);
Button btnSignIn = new Button("Sign In");
btnSignIn.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
if (textBoxUsername.getText().length() == 0) {
Window.alert("Username is empty.");
}else if (passwordTextBox.getText().length() == 0) {
Window.alert("Password is empty.");
}else {
AsyncCallback<User> callback = new AuthenticationHandler<User>();
Window.alert(textBoxUsername.getText() + "/" + passwordTextBox.getText());
rpc.authenticateUser(textBoxUsername.getText(), passwordTextBox.getText(), callback);
}
}
class AuthenticationHandler<T> implements AsyncCallback<User> {
public void onFailure(Throwable ex) {
Window.alert("RPC call failed.");
}
public void onSuccess(User result) {
navHandler.go("SelectPerson");
}
}
});
flexTable.setWidget(3, 1, btnSignIn);
Label lblCreateAcct = new Label("Create an account");
lblCreateAcct.setStyleName("gwt-Link-Login");
lblCreateAcct.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
navHandler.go("CreateAccount");
}
});
flexTable.setWidget(4, 1, lblCreateAcct);
}
}
MySQLConnection 类
package org.AwardTracker.server;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import org.AwardTracker.client.DBConnection;
import org.AwardTracker.client.User;
public class MySQLConnection extends RemoteServiceServlet implements DBConnection {
private Connection conn = null;
private String status;
private String url = "jbdc:mysql://localhost/awardtracker/";
private String user = "DBuser";
private String pass = "DBpass";
public MySQLConnection() {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection(url, user, pass);
} catch (Exception e) {
//NEVER catch exceptions like this
System.out.println("Error connecting to database - not good eh");
}
}
public User authenticateUser(String userName, String pass) {
User user = null; // necessary unless you do something in the exception handler
ResultSet result = null;
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(
"select readonly * from at_accounts where acc_email_address = \"" + userName + "\" AND " + "acc_password = \"" + pass + "\"");
result = ps.executeQuery();
while (result.next()) {
user = new User(result.getString(1), result.getString(2));
}
}
catch (SQLException sqle) {
//do stuff on fail
System.out.println("SQLException 1.");
}
finally {
if (result != null) {
try {
result.close();
}
catch (SQLException e) {
System.out.println("SQLException 2.");
e.printStackTrace();
}
}
if (ps != null) {
try {
ps.close();
}
catch (SQLException e) {
System.out.println("SQLException 3.");
e.printStackTrace();
}
}
}
return user;
}
}