我想将对象的值从DAO打印到servlet。

DAO:

public static List getFree(String svLectID,String ExLectID) throws SQLException, ClassNotFoundException
    {
        currentCon = JavaConnectionDB.getConnection() ;
        PreparedStatement ps1 = currentCon.prepareStatement("SELECT *\n" +
                                                            "FROM   (\n" +
                                                            "        SELECT      e1.FreeID,\n" +
                                                            "                    e1.lecturerID SVID,\n" +
                                                            "                    e1.availableID SVavail,\n" +
                                                            "                    e1.freedate AS SVFree,\n" +
                                                            "                    e2.lecturerID AS Examiner, \n" +
                                                            "                    e2.freedate EXFree,\n" +
                                                            "                    s.studentID,\n" +
                                                            "                    s.studentName,\n" +
                                                            "                    s.lecturerID AS lectID,\n" +
                                                            "                    sv.lecturerID AS SVlecturerID,\n" +
                                                            "                    sv.lecturerFullname AS SVlecturerName,\n" +
                                                            "                    ex.lecturerID AS EXlecturerID,\n" +
                                                            "                    ex.lecturerFullname AS EXlecturerName,\n" +
                                                            "                    v.availableID availID,\n" +
                                                            "                    v.availableDay,\n" +
                                                            "                    v.availableStart,\n" +
                                                            "                    v.availableEnd,\n" +
                                                            "                    ROW_NUMBER() OVER (PARTITION BY e1.lecturerID \n" +
                                                            "                                       ORDER BY dbms_random.random) AS rn\n" +
                                                            "        FROM        free e1 \n" +
                                                            "        INNER JOIN  free e2 \n" +
                                                            "                ON  e1.availableID = e2.availableID\n" +
                                                            "        INNER JOIN  student s\n" +
                                                            "                ON  s.lecturerID = e1.lecturerID\n" +
                                                            "        INNER JOIN  lecturer sv\n" +
                                                            "                ON  sv.lecturerID = e1.lecturerID\n" +
                                                            "        INNER JOIN  lecturer ex\n" +
                                                            "                ON  ex.lecturerID = e2.lecturerID\n" +
                                                            "        INNER JOIN  availability v\n" +
                                                            "                ON  v.availableID = e2.availableID\n" +
                                                            "                \n" +
                                                            "      \n" +
                                                            "        WHERE       e1.lecturerID = ? \n" +
                                                            "               AND  e2.lecturerID = ? \n" +
                                                            "               ORDER BY e2.availableID asc\n" +
                                                            "              \n" +
                                                            "        )\n" +
                                                            "WHERE rn <=5") ;
        ps1.setString(1, svLectID) ;
        ps1.setString(2, ExLectID);
        List list = new ArrayList() ;
        ResultSet rs1 = ps1.executeQuery() ;
        while(rs1.next())
        {
            Object[] obj = new Object[17] ;
            obj[0] = rs1.getString(1) ;
            obj[1] = rs1.getString(2);
            obj[2] = rs1.getInt(3);
            obj[3] = rs1.getDate(4);
            obj[4] = rs1.getString(5);
            obj[5] = rs1.getDate(6);
            obj[6] = rs1.getString(7);
            obj[7] = rs1.getString(8);
            obj[8] = rs1.getString(9);
            obj[9] = rs1.getString(10);
            obj[10] = rs1.getString(11);
            obj[11] = rs1.getString(12);
            obj[12] = rs1.getString(13);
            obj[13] = rs1.getInt(14);
            obj[14] = rs1.getString(15);
            obj[15] = rs1.getDate(16);
            obj[16] = rs1.getDate(17);
            list.add(obj) ;
            System.out.println("zabir "+rs1.getString(8));
        }
        return list ;
    }


如您所见,这些值存储在对象的列表中。我将这些值检索到servlet。

SERVLET:

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

         currentCon = JavaConnectionDB.getConnection();


         // Set response content type
         response.setContentType("text/html");
         PrintWriter out = response.getWriter();
         ServletContext context=getServletContext();

         String[] studentID = request.getParameterValues("studentID");
         String[] supervisorID =  request.getParameterValues("supervisorID");
         String[] examinerID = request.getParameterValues("examinerID");


        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");


         try{
             out.println("<br><center><table><tr>"
                                    + "<th>Student Name</th>"
                                    + "<th>Project Title</th>"
                                    + "<th>Supervisor Name</th>"
                                    + "<th>Examiner Name</th>"
                                    + "<th>Start</th>"
                                    + "<th>End</th>"
                                    + "<th>Date</th>"
                                    + "</tr>");


                          for (int i=0 ; i<studentID.length ; i++){

                                FreeBean free = new FreeBean();
                                PresentationBean present = new PresentationBean();
                                StudentBean student = new StudentBean();
                                List list = new ArrayList() ;

                                int SVavailableID = free.getAvailableID();
                                int EXavailableID = free.getAvailableID();


                                list =  GenerateScheduleDAO.getFree(supervisorID[i],examinerID[i]);

                                System.out.println(list.get(0)); //DEBUGGED HERE
                                System.out.println(list); //DEBUGGED HERE


                               out.println("<tr>");
                               out.println("<tr>");
                               out.println("<td>"+ studentID[i]+"</td>");
                               out.println("<td> Hello </td>");
                               out.println("<td>"+ supervisorID[i] +"</td>");
                               out.println("<td>"+ examinerID[i] +"</td>");
                               out.println("<td>"+ SVavailableID+"</td>");
                               out.println("<td>"+ EXavailableID+"</td>");
                               out.println("<td>"+ EXFreeDate+"</td>");
                               out.println("</tr>");


                            }//student loop
                            out.println("</center></table><br><br>");

         out.println("</body>");
         out.println("</html>");
         }// first try
         catch (Exception e)
                {
                    e.printStackTrace() ;
                }//first catch

     }//throws method


我试图像这样首先使用SOP获得值:

 System.out.println(list.get(0)); //DEBUGGED HERE
 System.out.println(list); //DEBUGGED HERE


第一个SOP产生:[Ljava.lang.Object;@13432ad
第二批SOP产生:[[Ljava.lang.Object;@1dd079f,

我的第一个假设是get(0)会给我FreeID值。如DAO中所声明。
那么,如果使用get(0)是错误的,我如何获得该值?

最佳答案

您的freeId应该位于方法list返回的getFree中每个数组的第一个位置。

要获取freeId的第一个元素的list,您应该执行以下操作:

System.out.println((Object[]) list.get(0))[0]);


对于列表的第二个元素的freeId:

System.out.println((Object[]) list.get(1))[0]);


等等。

这样做会更易读:

Object[] firstObjectAsArray = (Object[]) list.get(0);
System.out.println(firstObjectAsArray[0]);

Object[] secondObjectAsArray = (Object[]) list.get(1);
System.out.println(secondObjectAsArray[0]);




打印freeId所有元素的list

for (Object objectAsArray : list) {
    System.out.println(((Object[]) objectAsArray)[0]);
}




注意:查看您的代码,没有必要创建一个空列表。编码:

List list = new ArrayList() ;   // Not necessary
int SVavailableID = free.getAvailableID();
int EXavailableID = free.getAvailableID();
list =  GenerateScheduleDAO.getFree(supervisorID[i],examinerID[i]);


可以优化如下:

int SVavailableID = free.getAvailableID();
int EXavailableID = free.getAvailableID();
List list =  GenerateScheduleDAO.getFree(supervisorID[i],examinerID[i]);




注意:使用泛型,您不需要强制转换。为此,请替换列表的定义

List list = new ArrayList();




List<Object[]> list = new ArrayList<Object[]>();

07-24 20:28