0

Javaサーブレットとjpaを使用してMySQLデータベースにアクセスしようとしました。phpmyadmin を使用して MySQL データベースを作成しました。私のデータベースにはいくつかのデータが含まれていますが、Tomcat 7 サーバーで Java プロジェクトを実行した後、データベースからデータを取得できません。ここに、エンティティ クラス サーブレット 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);
     }       

}

player.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>

persistence.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

ここでのエラーは何ですか?? 結果のページにデータを取得するにはどうすればよいですか??

ありがとう!

4

1 に答える 1

1

ではすべて小さいvar="player"ですが、式では大文字になっています${Player.first_Name}

<c:forEach items="${players}" var="player">
     <tr>
          <td>${player.first_Name}</td> <!-- player not Player -->
          <td>${player.last_Name}</td>  <!-- player not Player -->
     </tr>
</c:forEach>
于 2012-10-27T03:16:28.310 に答える