我尝试从db检索数据并使用JSP以表格形式显示它。
问题是当我query.list()
时它返回null。
db中有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 + "]";
}
}
的结果
答案 0 :(得分:1)
显然,由于HQL
不正确,您无法从数据库中获取任何数据。您需要获得所有Task
Query query = session.createQuery("from Task where USERNAME='"
+ userName + "'");
检查您的财产USERNAME
。它必须是持久化的类属性,而不是数据库列!
只需使用transaction = session.beginTransaction();
代替
transaction = session.getTransaction();
transaction.begin();
使用List<Task> task = null;
代替
List<Task> task = new LinkedList<Task>();