struts2表单进行用户登录的功能,在action类里访问sql server 2005,结果报错说索引超出范围,大家帮忙看下我action类写的方法对不对,谢谢了import java.sql.*;import com.opensymphony.xwork2.*;public class Login extends ActionSupport{
private String userName;
private String password;

private String rsUserName;
private String rsPassword;
private int rsP;

public String execute() throws Exception,SQLException{
        String connectionUrl = "jdbc:sqlserver://localhost:1433;database=LifeInfoSearch;user=LifeInfoSearch;password=lifeinfosearch2009";//
        Connection con = DriverManager.getConnection(connectionUrl);
        System.out.println("Connection Successful! ");
        try{
        String sql="SELECT * FROM UserInfo WHERE username=?";
        PreparedStatement pstmt=con.prepareStatement(sql);
        pstmt.setString(2, userName);
        ResultSet rs=pstmt.executeQuery();
        while(rs.next()){
            rsUserName=rs.getString(2);
            rsPassword=rs.getString(3);
            rsP=rs.getInt(4);
        }
        rs.close();
        pstmt.close();
        
        if(rsUserName.equals(userName) && rsPassword.equals(password)){
        return SUCCESS;
        }
        else
        {
         return ERROR;
        }
  
        }
        finally
        {
            con.close();
        }
    
}

public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}}

解决方案 »

  1.   

       pstmt.setString(2, userName);
        应该是 pstmt.setString(1, userName)吧
      

  2.   

     pstmt.setString(1, userName)
    不是第几列是用户名,是第几个问号,要填充的内容
      

  3.   

    pstmt.setString(2, userName);
    setString()该方法的第一个参数指明?的位置
    不是列