0

我是Java技术的新手,所以如果我提出一个愚蠢的问题,请原谅...

我想制作一个 JSF 2.0 servlet 并将 MySql 用于数据库引擎。

问题是我似乎无法连接到服务器。

我使用 netbeans IDE,作为指导,我使用了来自 youtube 的教程:http ://www.youtube.com/watch?v=UBNaiVWwAZw&feature=related

不同的是我使用的是Tomcat。

我的 persistence.xml 文件位于 Web/META-INF/persistence.xml 中,内容为:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.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_2_0.xsd">
  <persistence-unit name="AnunturiJsfPU" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>my.servlets.Users</class>
    <class>my.servlets.UsersSessions</class>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
      <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/tz_cadastru_galati"/>
      <property name="javax.persistence.jdbc.password" value=""/>
      <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
      <property name="javax.persistence.jdbc.user" value="root"/>
    </properties>
  </persistence-unit>
</persistence>

我的 servlet 文件是:

package my.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@WebServlet(name = "testOkServlet2", urlPatterns = {"/testOkServlet2"})
public class testOkServlet2 extends HttpServlet {


    @PersistenceUnit
    EntityManagerFactory emf;


    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
                EntityManager em = emf.createEntityManager();
                Users u = (Users) em.createNamedQuery("Users.findAll").getResultList().get(0);
                String myUser = u.getUserNume();
                out.println("User:" + myUser);
        } catch (Exception e)
        {
            out.println(e.getMessage());
        }
        finally {           
            out.close();
        }
    }


    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }


    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}

这是我的用户对象:

package my.servlets;

import java.io.Serializable;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.xml.bind.annotation.XmlRootElement;


@Entity
@Table(name = "users")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Users.findAll", query = "SELECT u FROM Users u"),
    @NamedQuery(name = "Users.findByUserID", query = "SELECT u FROM Users u WHERE u.userID = :userID"),
    @NamedQuery(name = "Users.findByUserNume", query = "SELECT u FROM Users u WHERE u.userNume = :userNume"),
    @NamedQuery(name = "Users.findByNumePrenume", query = "SELECT u FROM Users u WHERE u.numePrenume = :numePrenume"),
    @NamedQuery(name = "Users.findByEmail", query = "SELECT u FROM Users u WHERE u.email = :email"),
    @NamedQuery(name = "Users.findByParola", query = "SELECT u FROM Users u WHERE u.parola = :parola"),
    @NamedQuery(name = "Users.findByContActiv", query = "SELECT u FROM Users u WHERE u.contActiv = :contActiv"),
    @NamedQuery(name = "Users.findByDataAdaugarii", query = "SELECT u FROM Users u WHERE u.dataAdaugarii = :dataAdaugarii"),
    @NamedQuery(name = "Users.findByNivelAccesID", query = "SELECT u FROM Users u WHERE u.nivelAccesID = :nivelAccesID")})
public class Users implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "userID")
    private Integer userID;
    @Basic(optional = false)
    @Column(name = "userNume")
    private String userNume;
    @Basic(optional = false)
    @Column(name = "numePrenume")
    private String numePrenume;
    @Basic(optional = false)
    @Column(name = "email")
    private String email;
    @Basic(optional = false)
    @Column(name = "parola")
    private String parola;
    @Basic(optional = false)
    @Column(name = "contActiv")
    private boolean contActiv;
    @Basic(optional = false)
    @Column(name = "dataAdaugarii")
    @Temporal(TemporalType.TIMESTAMP)
    private Date dataAdaugarii;
    @Basic(optional = false)
    @Column(name = "nivelAccesID")
    private short nivelAccesID;

    public Users() {
    }

    public Users(Integer userID) {
        this.userID = userID;
    }

    public Users(Integer userID, String userNume, String numePrenume, String email, String parola, boolean contActiv, Date dataAdaugarii, short nivelAccesID) {
        this.userID = userID;
        this.userNume = userNume;
        this.numePrenume = numePrenume;
        this.email = email;
        this.parola = parola;
        this.contActiv = contActiv;
        this.dataAdaugarii = dataAdaugarii;
        this.nivelAccesID = nivelAccesID;
    }

    public Integer getUserID() {
        return userID;
    }

    public void setUserID(Integer userID) {
        this.userID = userID;
    }

    public String getUserNume() {
        return userNume;
    }

    public void setUserNume(String userNume) {
        this.userNume = userNume;
    }

    public String getNumePrenume() {
        return numePrenume;
    }

    public void setNumePrenume(String numePrenume) {
        this.numePrenume = numePrenume;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getParola() {
        return parola;
    }

    public void setParola(String parola) {
        this.parola = parola;
    }

    public boolean getContActiv() {
        return contActiv;
    }

    public void setContActiv(boolean contActiv) {
        this.contActiv = contActiv;
    }

    public Date getDataAdaugarii() {
        return dataAdaugarii;
    }

    public void setDataAdaugarii(Date dataAdaugarii) {
        this.dataAdaugarii = dataAdaugarii;
    }

    public short getNivelAccesID() {
        return nivelAccesID;
    }

    public void setNivelAccesID(short nivelAccesID) {
        this.nivelAccesID = nivelAccesID;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (userID != null ? userID.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Users)) {
            return false;
        }
        Users other = (Users) object;
        if ((this.userID == null && other.userID != null) || (this.userID != null && !this.userID.equals(other.userID))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "my.servlets.Users[ userID=" + userID + " ]";
    }

}

如果我错过了发布任何其他信息,请告诉我,我会放到网上。

请帮我。提前感谢曼尼。

4

1 回答 1

0

在这种情况下,首先要做的是手动检查与服务器的连接,以确保您可以访问服务器。

于 2012-11-12T03:16:53.200 回答