gpt4 book ai didi

java - 将对象的值从 DAO 传递到 2D 数组中的 servlet

转载 作者:行者123 更新时间:2023-12-02 03:37:27 25 4
gpt4 key购买 nike

我想将 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。

服务器:

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 应位于 getFree 方法返回的 list 中每个数组的第一个位置。

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

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

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[]>();

关于java - 将对象的值从 DAO 传递到 2D 数组中的 servlet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37269107/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com