title: MySQL java servlet and JPA
body: <p>I tried to access MySQL database using java servlet and jpa. I created MySQL database using phpmyadmin. My database contains some data but after running the java project on Tomcat 7 server it can't fetch data from the database. Here I post my Entity class Servlet jsp and persistance.xml and screen capture of the result (<a href="http://www.ibm.com/developerworks/opensource/tutorials/os-eclipse-europa1/index.html" rel="nofollow">Here</a> is the tutorial that I tried to implemet)</p>
  
  <p>Eclipse Link 2.4.1 juno</p>
  
  <p>database name: baseball</p>
  
  <p>table: players</p>
  
  <p>columns: Id primary key auto increment
  , first_Name , last_Name</p>
  
  <p>Entity class</p>
  
  <pre><code>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;
  }
  
  }
  </code></pre>
  
  <p>PlayerServlet</p>
  
  <pre><code>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&lt;Player&gt; 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);
       }       
  
  }
  </code></pre>
  
  <p>player.jsp</p>
  
  <pre><code>&lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  pageEncoding="ISO-8859-1"%&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt;
  &lt;title&gt;Insert title here&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
   &lt;table&gt;
        &lt;thead&gt;
             &lt;tr&gt;
                  &lt;td&gt;First Name&lt;/td&gt;
                  &lt;td&gt;Last Name&lt;/td&gt;
             &lt;/tr&gt;
        &lt;/thead&gt;
        &lt;c:forEach items="${players}" var="player"&gt;
             &lt;tr&gt;
                  &lt;td&gt;${Player.first_Name}&lt;/td&gt;
                  &lt;td&gt;${Player.last_Name}&lt;/td&gt;
             &lt;/tr&gt;
        &lt;/c:forEach&gt;
   &lt;/table&gt;
  &lt;/body&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>persistence.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;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"&gt;
   &lt;persistence-unit name="baseball"&gt;
  
        &lt;class&gt;org.developerworks.baseball.Player&lt;/class&gt;
  &lt;properties&gt;
    &lt;property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/baseball"/&gt;
    &lt;property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/&gt;
    &lt;property name="javax.persistence.jdbc.user" value="root"/&gt;
    &lt;property name="javax.persistence.jdbc.password" value=""/&gt;
  
  &lt;/properties&gt;          
   &lt;/persistence-unit&gt;     
  &lt;/persistence&gt;
  </code></pre>
  
  <p>Resulting page only shows(I can't post screen captures):
       First Name Last Name</p>
  
  <p>Console output</p>
  
  <pre><code>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
  </code></pre>
  
  <p>What is the error here ??
  How can I get the data to the resulting page ??</p>
  
  <p>Thanks!</p>