プロジェクトを実行すると、「RPC 呼び出しに失敗しました」というエラー メッセージが表示されます。これは、LoginView クラスの AuthenticationHandler クラスからのものです。また、ClickHandler のウィンドウ アラートには、「aa/bb」のページに入力された値が表示されます。MySQL と Tomcat が起動し、多数のオンライン ガイドとフォーラムに従って、すべてのドライバーがインストールされていることを確認しようとしました。しかし、私はこの点で何かが正しいとは思っていません。しかし、私は間違っている可能性があります。何を確認し、どのように修正するか教えてください。私は初心者なので、詳細をいただければ幸いです。
Webアプリケーションを「実行」し、GAEが有効になっていません(別の質問でこれが原因であることがわかったので、これについて言及します)。DBuser のユーザーとパスワード DBpass が設定された MySQL データベース賞トラッカーがあり、データが含まれる 1 つのテーブルがあります。
エラー ログには、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;
}
}
LoginView クラス
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;
}
}