我正在用 J2EE 编写我的第一个项目。我想通过 servlet 填充数据库的一些表。例如:
这是我的实体“Administrator.java”
package cinema.entity;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Amministratore implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String user_name;
private String psw;
private String name;
private String surname;
private String email;
public Administrator () {
}
public Administrator (String user_name, String name, String surname,String psw, String email) {
this.user_name=user_name;
this.name=name;
this.surname=surname;
this.psw=psw;
this.email=email;
}
public String getPsw () {
return psw;
}
public void setPsw(String psw) {
this.psw = psw;
}
public String getUserName () {
return user_name;
}
public void setUserName (String user_name) {
this.user_name=user_name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name=name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname=surname;
}
public String getEmail() {
return email;
}
public void getEmail(String email) {
this.email=email;
}
@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.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 Amministratore)) {
return false;
}
Amministratore other = (Amministratore) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
@Override
public String toString() {
return "cinema.entity.Amministratore[ id=" + id + " ]";
}
}
这是我的小服务程序:
package cinema.servlet;import cinema.entity.Administrator;
import cinema.interfacce.AdminBeanRemote;
import java.io.IOException;
import java.io.PrintWriter;
import javax.ejb.EJB;
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 = "InitializeServlet", urlPatterns = {"/InitializeServlet"})
public class InitializeServlet extends HttpServlet {
@EJB
AdminBeanRemote adminBean;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
adminBean.cleanAdmin();
adminBean.addNewAdmin("admin1", "Silvia", "Fichera", "admin1", "admin1@cinema.it");
adminBean.addNewAdmin("admin2", "Giulia", "Fichera", "admin2", "admin2@cinema.it");
adminBean.addNewGestore("gestore1", "Mike", "Bongiorno", "gestore1", "gestore1@cinema.it");
adminBean.addNewAdmin("gestore2", "Carlo", "Conti", "gestore2", "gestore2@cinema.it");
try {
getServletConfig().getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);
return;
} finally {
}
}
@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";
}
}
在 AdminBean 中,我有函数 cleanAdmin() 和 addNewAdmin:
package cinema.bean;
import cinema.entity.*;
import cinema.interfacce.AdminBeanRemote;
import java.util.Iterator;
import java.util.List;
import javax.ejb.Stateless;
import javax.ejb.Remote;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceException;
import javax.persistence.Query;`
@Stateless
@Remote
public class AdminBean implements AdminBeanRemote {
@PersistenceContext
private EntityManager em;
@Override
public void cleanAdmin() {
List admin = em.createQuery("SELECT c FROM Administrator c").getResultList();
for(Iterator iter = admin.iterator(); iter.hasNext();) {
em.remove(iter.next());
}
em.flush();
}
@Override
public boolean addNewAdmin(String user_name, String name, String surname, String psw, String email)throws PersistenceException {
if(em.find(Administrator.class, user_name)!=null) {
System.out.println("Admin already existing");
return false;
}else{
Administrator administrator;
administrator = new Administrator(user_name, name, surname, psw, email);
em.persist(administrator);
}
return true;
}
}
有什么建议么?我找不到文件persistance.xml,NetBeans 在其示例数据库中创建了我的表。
感谢您的帮助,如果语法不正确,我们深表歉意!