列表值是打印地址而不是值

时间:2015-11-24 07:47:20

标签: java jstl

我在下面的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

但不是这个,我需要那里的确切值。请让我知道我哪里出错了,我该如何解决它。

2 个答案:

答案 0 :(得分:5)

您必须覆盖toString()中的ÙserBean方法。否则,将调用默认的toString方法。例如

@Override
public String toString() {
    return this.caseNumber + " " + this.caseOwner;
}

某些IDE(eclipse,intellij,...)具有创建toString方法的快捷方式。例如,在eclipse中它是

right click -> source -> generate toString()...

答案 1 :(得分:2)

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

如果你想从列表中的用户bean获取案例编号的值,你应该使用userBeans[0].getCaseNumber();我假设你有案例编号的getter。