I am trying to create my first MySQLConnection in Eclipse and am getting an error on authenticateUser of “Multiple markers at this line
– The return type is incompatible with
DBConnection.authenticateUser(String, String)”
I understand this means “that there is more than one error … but I can’t solve the issue…”.
I am using http://altair.cs.oswego.edu/~tenberge/tenbergen.org/misc/DB-Access-in-GWT-The-Missing-Tutorial.pdf as my guide.
This is my code:
AwardTracker.gwt.xml
<module>
<inherits name="com.google.gwt.user.User"/>
<inherits name="com.google.gwt.user.theme.standard.Standard"/>
<entry-point class="org.AwardTracker.client.AwardTracker"/>
<!-- servelet context - path is arbitrary, but must match up with the rpc init inside java class -->
<!-- Tomcat will listen for this from the server and waits for rpc request in this context -->
<servelet class="org.AwardTracker.server.MySQLConnection"
path="/MySQLConnection" />
<inherits name="com.google.gwt.user.theme.standard.Standard"/>
<inherits name="com.google.gwt.user.theme.chrome.Chrome"/>
<inherits name="com.google.gwt.user.theme.dark.Dark"/>
User.java
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;
}
}
MySQLConnection.java
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
}
}
public int authenticateUser(String user, String pass) {
User user;
try {
PreparedStatement ps = conn.prepareStatement(
"select readonly * from users where username = \"" + user + "\" AND " + "password = \"" + pass + "\"");
ResultSet result = ps.executeQuery();
while (result.next()) {
user = new User(result.getString(1), result.getString(2));
}
result.close();
ps.close();
} catch (SQLException sqle) {
//do stuff on fail
}
return user;
}
}
Any help would be greatly appreciated.
Regards,
Glyn
Hi BenvynQ,
Thanks for your help. Just goes to show that these tutorials are not perfect. I made this change and now I get an error within authenticateUser:
while (result.next()) {
user = new User(result.getString(1), result.getString(2));
}
Of “The constructor User(String, String) is undefined”.
Thanks,
Glyn
Hi Bevnq,
Thanks awfully for your help. I think there were a few “}” missing so I have modified as following.
Also, you say “User user = null; // necessary unless you do something in the exception handler
“. My impression of this is that we are trying to find out if the user is in the table and return an exception if they are not (i.e., verify the username and password). Therefore, the return needs to be a confirmation and therefore we are doing something with the exveption handler. I am obviously missing something; so how do we know the user has logged in successfully?
Also, Eclipse does not seem to have an issue with mixed case for the package as I am doing this in stages and all has worked up to adding this.
Regards,
Glyn
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
}
}
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 users where username = \"" + userName + "\" AND " + "password = \"" + pass + "\"");
result = ps.executeQuery();
while (result.next()) {
user = new User(result.getString(1), result.getString(2));
}
}
catch (SQLException sqle) {
//do stuff on fail
}
finally {
if (result != null) {
try {
result.close();
}
catch (SQLException e) {
e.printStackTrace();
}
}
if (ps != null) {
try {
ps.close();
}
catch (SQLException e) {
e.printStackTrace();
}
}
}
return user;
}
}
You have defined the method
yet the object you are returning is
reading the tutorial looks like you should have declared the method