我有下面的DAO文件。

package org.DAO;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;

import org.bean.UserBean;
import org.code.general.DBConnection;

public class GetDataDAO {
    DBConnection dbConnection = new DBConnection();

    public List<UserBean> list() throws Exception {
        List<UserBean> userBeans = new ArrayList<UserBean>();
        try {
            Connection conn = dbConnection.conn;
            Statement stmt = dbConnection.stmt;
            ResultSet rs = dbConnection.rs;
            PreparedStatement ps = dbConnection.ps;
            String excelPath = dbConnection.excelPath;
            String queryString = null;
            dbConnection.createClassForName();
            conn = DriverManager.getConnection(
                    "jdbc:odbc:Driver={Microsoft Excel Driver (*.xls)};DBQ=" + excelPath + "; READONLY=FALSE;");
            System.out.println("Connecting to database…");
            System.out.println("Oracle JDBC Driver Registered!");
            if (conn != null) {
                System.out.println("You made it, take control your database now!");
            } else {
                System.out.println("Failed to make connection!");
            }
            stmt = conn.createStatement();
            queryString = "Select * from [report1448039568905$] where ([Case Owner] = 'SSHD' or [Case Owner] = 'Hyderabad Operations' or [Case Owner] = 'Customer Service- Core')";
            rs = stmt.executeQuery(queryString);
            ResultSetMetaData rsmd = rs.getMetaData();
            int rowCount = rsmd.getColumnCount();
            System.out.println("bno of cols are " + rsmd.getColumnCount());
            for (int i = 0; i < rowCount; i++) {
                System.out.print(rsmd.getColumnName(i + 1) + "  \t");
                System.out.println(rsmd.getColumnTypeName(i + 1));
            }

            while (rs.next()) {
                UserBean userBean = new UserBean();
                userBean.setCaseNumber(rs.getString(1));
                userBean.setCaseOwner(rs.getString(2));
                userBean.setStatus(rs.getString(4));
                userBean.setIssue(rs.getString(5));
                userBean.setReason(rs.getString(6));
                userBean.setDateOpened(rs.getString(7));
                userBean.setAge(rs.getInt(8));
                userBeans.add(userBean);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return userBeans;
    }
}


而下面的Servlet

package org.servlet;

import org.DAO.*;
import org.bean.UserBean;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/GetData")
public class GetData extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public GetData() {
        super();
    }

    GetDataDAO getdatadao = null;

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // set content type

        try {
            getdatadao = new GetDataDAO();
            List<UserBean> userBeans = getdatadao.list();
            request.setAttribute("userBeans", userBeans);
            request.getRequestDispatcher("DisplayTable.jsp").forward(request, response);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}


在这里,我尝试使用以下代码在jsp文本框中打印第一个值(caseNumber)。

<body>
    <table border=2 width="45%">
        <tr>
            <td colspan="2" align="center"><b>Comedy Movies</b></td>
        </tr>
        <tr>

            <td><b>Title</b></td>
            <td><b>Description</b></td>
        </tr>

        <tr>
            <td>Case Number</td>
            <td><c:out value="${userBeans[0].toString()}" /></td>
        </tr>
    </table>
</body>


但是我得到的输出如下。

org.bean.UserBean@16c8f41


但是除了这个,我需要那里的确切值。请让我知道我要去哪里错了以及如何解决。

最佳答案

您的对象的类型为List<UserBean> userBeans,并且您正尝试从userBeans[0].toString()获取值,这显然将为您提供List中第一个元素的哈希码。

如果要从列表中的用户bean获取案例编号的值,则可能应使用userBeans[0].getCaseNumber();,我假设您具有案例编号的吸气剂。

08-03 12:04