在ArrayList DB中,数据从mysql servlet变为空

时间:2016-04-18 11:53:00

标签: java mysql json servlets arraylist

public static  ArrayList<Register> validateUser( String uname, String password)throws Exception 
{

    ArrayList<Register> login = new ArrayList<Register>();
    try 
    {
        con = establishConnection();
        pstmt = con.prepareStatement("SELECT uname,password FROM login WHERE uname = ? ");
        pstmt.setString(1,uname);
        //pstmt.setString(2, password);
        ResultSet rs = pstmt.executeQuery();
        if(rs.next())
        {

            Register reg = new Register();
            reg.setUname(rs.getString(uname));
            reg.setPassword(rs.getString(password));
            login.add(reg);
            System.out.println("password is "+rs.getString(2)+" username is "+rs.getString(1));
            if(rs.getString(2).equals(password))
            {

            }

        }
        else
            return login;
    }
    catch(SQLException ex)
    {
        ex.printStackTrace();
    }
    finally
    {
        try 
        {
            if(pstmt!=null)
                pstmt.close();
            if(con != null)
                con.close();
        } catch (SQLException e) 
        {
            e.printStackTrace();
        }
    }
    return login;

}

public void doGet(HttpServletRequest request,
        HttpServletResponse response)
throws ServletException, IOException {


     String userName = request.getParameter("userName");
     String password = request.getParameter("password");

     PrintWriter out=response.getWriter();
     if(userName == null)
     {
        out.println("The userName you entered is wrong");
     }
     if(password ==  null)
     {
        out.println("Wrong password");
     }



     //Get the DB connection Validation here 

     String feeds = null;

     try {
         ArrayList<Register> login = null;
        if(Connector.validateUser(userName,password) != null)
         {
            out.println("Login successfull !!!");
            out.print("The userName is "+userName+" pasword is "+password);


            Gson gson = new Gson();
            System.out.println(gson.toJson(Connector.validateUser(userName,password)));
            out.println(Connector.validateUser(userName,password));
            feeds = gson.toJson(Connector.validateUser(userName,password));


         }
         else
         {
            out.println("Authentication Failed Boss!!!");
         }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }



}

我需要将db数据转换为json。我得到空阵列。 sendmail调用arraylist并以json格式显示数据。 对此有任何帮助。

这是该程序的输出 登录成功!!! userName是示例pasword,例如[]

0 个答案:

没有答案