我尝试使用 java servlet 和 jpa 访问 MySQL 数据库。我使用 phpmyadmin 创建了 MySQL 数据库。我的数据库包含一些数据,但在 Tomcat 7 服务器上运行 java 项目后,它无法从数据库中获取数据。在这里,我发布了我的实体类 Servlet jsp 和 persistance.xml 以及结果的屏幕截图(这是我尝试实现的教程)
Eclipse Link 2.4.1 朱诺
数据库名称:棒球
表:球员
列:Id 主键自动递增,first_Name,last_Name
实体类
package org.developerworks.baseball;
import java.io.Serializable;
import javax.persistence.*;
/**
* The persistent class for the players database table.
*
*/
@Entity
@Table(name="players")
public class Player implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
private String first_Name;
private String last_Name;
public Player() {
}
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
public String getFirst_Name() {
return this.first_Name;
}
public void setFirst_Name(String first_Name) {
this.first_Name = first_Name;
}
public String getLast_Name() {
return this.last_Name;
}
public void setLast_Name(String last_Name) {
this.last_Name = last_Name;
}
}
PlayerServlet
package org.developerworks.baseball.web;
import java.io.IOException;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.developerworks.baseball.Player;
/**
* Servlet implementation class PlayerServlet
*/
@WebServlet("/PlayerServlet")
public class PlayerServlet extends HttpServlet implements javax.servlet.Servlet {
static final long serialVersionUID = 1L;
private EntityManagerFactory factory;
private EntityManager em;
/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#HttpServlet()
*/
public PlayerServlet() {
super();
factory = Persistence.createEntityManagerFactory("baseball");
em = factory.createEntityManager();
}
/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
@SuppressWarnings("unchecked")
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
List<Player> players = em.createQuery("select p from Player p").getResultList();
request.setAttribute("players", players);
request.getRequestDispatcher("/players.jsp").forward(request, response);
}
/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request,
HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}
播放器.jsp
<%@ 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>Insert title here</title>
</head>
<body>
<table>
<thead>
<tr>
<td>First Name</td>
<td>Last Name</td>
</tr>
</thead>
<c:forEach items="${players}" var="player">
<tr>
<td>${Player.first_Name}</td>
<td>${Player.last_Name}</td>
</tr>
</c:forEach>
</table>
</body>
</body>
</html>
持久性.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="baseball">
<class>org.developerworks.baseball.Player</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/baseball"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value=""/>
</properties>
</persistence-unit>
</persistence>
结果页面仅显示(我无法发布屏幕截图):名字姓氏
控制台输出
Oct 27, 2012 10:08:53 AM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jre7\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\Program Files (x86)\ActiveState Komodo Edit 7\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static;C:\Program Files\Java\jre7\bin;C:\Program Files\Java\jdk1.7.0_02\bin;C:\Python26;C:\Program Files\Microsoft\Web Platform Installer\;C:\Program Files\Java\jdk1.7.0_02\bin;C:\Program Files\Java\jre1.7.0\bin;C:\Python26;.
Oct 27, 2012 10:08:53 AM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:cricket' did not find a matching property.
Oct 27, 2012 10:08:53 AM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:Baseball' did not find a matching property.
Oct 27, 2012 10:08:53 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8085"]
Oct 27, 2012 10:08:53 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Oct 27, 2012 10:08:53 AM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 1350 ms
Oct 27, 2012 10:08:53 AM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Oct 27, 2012 10:08:53 AM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.23
Oct 27, 2012 10:08:58 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8085"]
Oct 27, 2012 10:08:58 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Oct 27, 2012 10:08:58 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 4686 ms
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
[EL Info]: 2012-10-27 10:09:09.954--ServerSession(1934061893)--EclipseLink, version: Eclipse Persistence Services - 2.4.1.v20121003-ad44345
[EL Info]: connection: 2012-10-27 10:09:10.495--ServerSession(1934061893)--file:/E:/Developments/ROSTER/wrk/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/Baseball/WEB-INF/classes/_baseball login successful
这里有什么错误??如何将数据获取到结果页面?
谢谢!