0

我正在尝试在 Eclipse 中创建我的第一个 MySQLConnection,但在 authenticateUser 上出现“此行有多个标记 - 返回类型与 DBConnection.authenticateUser(String, String) 不兼容”的错误

我理解这意味着“存在不止一个错误……但我无法解决问题……”。

我使用http://altair.cs.oswego.edu/~tenberge/tenbergen.org/misc/DB-Access-in-GWT-The-Missing-Tutorial.pdf作为我的指南。

这是我的代码:

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;
}

}

任何帮助将不胜感激。

问候,

格林

嗨 BenvynQ,

谢谢你的帮助。只是表明这些教程并不完美。我进行了此更改,现在在 authenticateUser 中出现错误:

while (result.next()) {
user = new User(result.getString(1), result.getString(2));
}

“构造函数 User(String, String) 未定义”。

谢谢,

格林

嗨,贝文克,

非常感谢您的帮助。我认为缺少一些“}”,因此我进行了如下修改。

此外,您说“用户用户 = null;// 必要的,除非您在异常处理程序中执行某些操作”。我对此的印象是,我们试图找出用户是否在表中,如果不在则返回异常(即验证用户名和密码)。因此,返回需要是确认,因此我们正在使用 exveption 处理程序做一些事情。我显然错过了一些东西;那么我们怎么知道用户已经登录成功呢?

此外,Eclipse 似乎对包的混合大小写没有问题,因为我正在分阶段执行此操作,并且所有人都已准备好添加它。

问候,

格林

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;
}
}
4

1 回答 1

0

你已经定义了方法

public int authenticateUser(String user, String pass) {

但是您要返回的对象是

User user;

阅读教程看起来你应该已经声明了方法

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;
于 2013-01-18T03:09:59.677 回答