求助,jsp里面java的类型转换问题

从dao里面返回的ArrayList转换成自定义的vo时报错,不知道怎么办了(我java没怎么学)
或者可不可以dao直接返回自定义vo

login.jsp

<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="UTF-8"%>
<%@ page import="vo.Admin,dao.AdminDao" %>
<html>
  <body>
    <form action = "login.jsp">
        账号:<input type = "text" name = "adminname"><br>
        密码:<input type = "password" name = "password"><br>
        <input type = "submit" value = "登录">
    </form>
    <%
        String adminname = request.getParameter("adminname");
        String password = request.getParameter("password");
        if(adminname!=null&&password!=null){
            AdminDao admindao=new AdminDao();
            System.out.print("test");
            Admin admin=(Admin)admindao.queryAllAdmins(adminname);
            if(password.equals(admin.getPassword())){
                response.sendRedirect("JxglSys/servlets/InitServlet");
            }
        }

     %>
  </body>
</html>

AdminDao.java

package dao;

import java.sql.*;
import java.util.ArrayList;
import vo.Admin;

public class AdminDao {
    public ArrayList queryAllAdmins(String adminname) throws Exception{
        ArrayList admins= new ArrayList();
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            String url = "jdbc:sqlserver://localhost:1433;DatabaseName=JXGL";
            Connection conn = DriverManager.getConnection(url,"sa","nmybj.19981227");
            System.out.print(adminname);
            String sql = "SELECT ADMINNAME,PASSWORD FROM ADMIN WHERE ADMINNAME = '"+adminname+"'";
            Statement stat = conn.createStatement();
            ResultSet rs = stat.executeQuery(sql);
            while(rs.next()) {
                Admin admin = new Admin();
                admin.setAdminname(rs.getString("ADMINNAME"));
                admin.setPassword(rs.getString("PASSWORD"));
                admins.add(admin);
            }
            rs.close();
            stat.close();
        }
        catch (SQLException e) {
                //e.printStackTrace();
                System.out.print("Database error");
            }

        return admins;
    }
}

Admin.java

package vo;

public class Admin {
    private String adminname;
    private String password;

    public String getAdminname() {
        return adminname;
    }
    public void setAdminname(String adminname) {
        this.adminname = adminname;
    }

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

2个回答

ArrayList admins= new ArrayList();
改成 List<Admin> admins=new ArrayList();
同样方法名改为
 public List<Admin> queryAllAdmins(String adminname) throws Exception{

queryAllAdmins方法的返回值是ArrayList是一个集合, 应该不能直接转vo吧,
还是用ArrayList admins=admindao.queryAllAdmins(adminname);

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问