0

I've the below DAO File.

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;
    }
}

And the below 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();
        }
    }

}

Here i'm trying to print the first value (caseNumber) in a jsp textbox using the below code.

<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>

But the output i get is as below.

org.bean.UserBean@16c8f41

but instead of this i need the exact value at there. please let me know where am i going wrong and how do i fix it.

user3872094
  • 3,269
  • 8
  • 33
  • 71
  • 2
    The exact value of what? UserBean has 7 properties that we know about. You're calling toString() on it. That will call... toString(). So if you haven't overridden toString(), it will default to Object's toString() implementation. If you want to display the case number of the UserBean, then use... `userBeans[0].caseNumber`, not `userBeans[0].toString()`. – JB Nizet Nov 24 '15 at 07:49
  • Seriously, take a break and work through a decent Java book/tutorial and then a JSP/JavaEE book/tutorial. – BalusC Nov 24 '15 at 07:56

2 Answers2

5

You must override the toString() method in ÙserBean. Otherwise the default toString method will be called. For example

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

Some IDE (eclipse, intellij,...) have shortcut to create the toString method. For example in eclipse it is

right click -> source -> generate toString()...
ThomasThiebaud
  • 11,331
  • 6
  • 54
  • 77
  • Hi, I've this in my Bean class, do you want me to update my question with that class also? – user3872094 Nov 24 '15 at 07:54
  • Yes please. But just provide the minimal code needed (eg not the getters and setters) – ThomasThiebaud Nov 24 '15 at 07:55
  • Huh? Why don't you tell the OP to simply use `${userBeans[0].caseNumber}`? – BalusC Nov 24 '15 at 07:55
  • I will probably not recommend this method as User Bean might have some specific purpose which might be containing user specific details. So overriding `toString` method will only give case number will might not be applicable for future enhancements as you are increasing the dependency on `toString` method – Vishrant Nov 24 '15 at 07:56
  • If he only wants the caseNumber attribute, using a getter is better. But the purpose of `toString()` is not to return a single property but a string representation of the object. My example was too simple, I will edit it. – ThomasThiebaud Nov 24 '15 at 08:26
2

Your object is of type List<UserBean> userBeans and you are trying to get value from userBeans[0].toString() which will obviously give you hash code of first element in List.

If you want to get value of case number from user bean in list you should probably use userBeans[0].getCaseNumber(); I am assuming you have getters for Case Number.

Vishrant
  • 15,456
  • 11
  • 71
  • 120
  • I've one small question, is there a way that i can get it in form of arrays, i.e. instead of `userBeans[0].getCaseNumber();` something like `userBeans[0].get[0];`? – user3872094 Nov 24 '15 at 08:01
  • Yes, you can set whole array as attribute and use `forEach` jsp scriptlet, this question will help you.. http://stackoverflow.com/questions/15839335/using-for-loop-inside-of-a-jsp – Vishrant Nov 24 '15 at 08:50