gpt4 book ai didi

java - 从数据库读取数据并存储在数组列表中,但数组列表显示为空

转载 作者:行者123 更新时间:2023-11-29 11:39:44 25 4
gpt4 key购买 nike

我正在尝试从数据库检索数据并使用 JSP 以表格形式显示它。

问题是当我执行 query.list() 时,它返回 null。

数据库中有 10 条记录。

如何检索所有记录?

下面是我的 DTO

public class TaskDAO {
@SuppressWarnings("unchecked")
public List<Task> getUserTasks(String userName) {
// Task task = getTasksByUserName(userName);
Configuration configuration = new Configuration().configure();
SessionFactory sessionFactory = configuration.buildSessionFactory();

Session session = sessionFactory.openSession();
Transaction transaction = null;
List<Task> task = new LinkedList<Task>();
try {
transaction = session.getTransaction();
transaction.begin();
Query query = session.createQuery("select id, taskDescription from Task where USERNAME='"
+ userName + "'");
task = query.list();
transaction.commit();

} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
e.printStackTrace();
} finally {
session.close();
}

return task;

}

}

下面是我的 JSP

<%@page import="java.util.Iterator"%>
<%@page import="com.sk.app.dto.Task"%>
<%@page import="java.util.List"%>
<%@page import="com.sk.app.controller.Success"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Home Page</title>
</head>
<body>
<h2>Tasks</h2>
<table>
<%
List tasks = (List)request.getAttribute("taskList");
Iterator iterator = tasks.iterator();
while (iterator.hasNext()){
Task currentTask = (Task) iterator.next();%>

<tr>
<td> <%=currentTask.getId()%></td>
<td> <%=currentTask.getTaskDescription()%></td>
</tr>
<%} %>
</table>

下面是我的 Servlet

public class Success extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String userName = request.getParameter("userName");
try {
TaskDAO taskDAO = new TaskDAO();

List<Task> taskList = taskDAO.getUserTasks(userName);

request.setAttribute("taskList", taskList);
Iterator<Task> iterator = taskList.iterator();
int count = 0;
while (iterator.hasNext()){
count ++;
}
System.out.println(count);
RequestDispatcher requestDispatcher = request
.getRequestDispatcher("task.jsp");

requestDispatcher.forward(request, response);
} catch (Exception e) {

e.printStackTrace();
}

}
}

下面是我的任务 dto

public class Task {
private int id;
private String taskDescription;
private String USERNAME;
public String getUserName() {
return USERNAME;
}
public void setUserName(String userName) {
this.USERNAME = userName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTaskDescription() {
return taskDescription;
}
public void setTaskDescription(String taskDescription) {
this.taskDescription = taskDescription;
}
@Override
public String toString() {
return "Task [id=" + id + ", taskDescription=" + taskDescription
+ ", USERNAME=" + USERNAME + "]";
}


}

当我调试程序时,这是我检查 task = query.list(); debug 得到的结果

下图是表结构,用户名列是用户表的外键。 table

最佳答案

显然,您没有从数据库中获取任何数据,因为您的 HQL是不正确的。您需要获取所有 Task

Query query = session.createQuery("from Task where USERNAME='"
+ userName + "'");

检查您的属性(property)USERNAME 。它必须是持久类属性,而不是数据库列!

只需使用 transaction = session.beginTransaction();代替

transaction = session.getTransaction();
transaction.begin();

使用List<Task> task = null;代替

List<Task> task = new LinkedList<Task>();

关于java - 从数据库读取数据并存储在数组列表中,但数组列表显示为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36029500/

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