当我尝试连接到db时,我收到500错误并关闭我的eclipse / tomcat。有谁知道我怎么能解决它?
确认Servlet
package be.pxl.tafelboeker.servlets;
import be.pxl.tafelboeker.dao.BoekingDAO;
import be.pxl.tafelboeker.domain.Boeking;
import be.pxl.tafelboeker.services.BoekingService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet(value = "/Confirm", initParams = {
@WebInitParam(name = "ConfirmPage",
value = "/confirm.jsp") })
public class ConfirmServlet extends HttpServlet {
private String ConfirmPage;
private String geluktPage;
private String misluktPage;
private BoekingService service = new BoekingService();
@Override
public void init() throws ServletException {
// TODO Auto-generated method stub
super.init();
ConfirmPage = getInitParameter("ConfirmPage");
geluktPage ="gelukt.jsp";
misluktPage = "mislukt.jsp";
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Boeking bean = new Boeking();
HttpSession sess = request.getSession();
bean = (Boeking)sess.getAttribute("bean");
request.setAttribute("bean", bean);
request.getRequestDispatcher(ConfirmPage).forward(request,response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO STUDENT : boek tafel, toon gelukt.jsp indien de boeking gelukt is en toon mislukt.jsp indien de tafel niet geboekt kan worden
Boeking bean = new Boeking();
HttpSession sess = request.getSession();
bean = (Boeking)sess.getAttribute("bean");
BoekingDAO dao = new BoekingDAO("jdbc:mysql://localhost/survey", "root", "");
BoekingService serv = new BoekingService();
serv.setDao(dao);
if (serv.boekTafel(bean)) {
request.getRequestDispatcher(geluktPage).include(request,response);
} else {
request.getRequestDispatcher(misluktPage).include(request,response);
}
//Krijg een error bij het aanroepen van mijn Persistence krijg deze pas vandaag als ik mijn oefeningen van gister wil openen die werkte krijg ik het nu ook
//Dit is zeer vervelend. Kan nu niet testen of mijn query en de rest wel klopt.
//Heb mijn best gedaan om het zo goed mogelijk in te zullen
//Hetzelfde geld voor overzichtservlet & overzicht.jsp die ook dezelfde fout krijgen
}
}
Boeking Dao
package be.pxl.tafelboeker.dao;
import be.pxl.tafelboeker.domain.Boeking;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityManager;
import javax.persistence.Persistence;
import javax.persistence.Query;
import java.time.LocalDate;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
public class BoekingDAO {
private String url;
private String user;
private String password;
EntityManagerFactory emf;
public BoekingDAO(String url, String user, String password) {
this.url = url;
this.user = user;
this.password = password;
}
public void setDriver(String driver)
throws ClassNotFoundException {
Class.forName("com.mysql.jdbc.Driver");
}
private EntityManager getEntityManager() {
emf = Persistence.createEntityManagerFactory("booking");
EntityManager em = emf.createEntityManager();
return em;
}
// TIP : Gebruik TemporalType.DATE voor je query parameter
public boolean isTafelBeschikbaar(LocalDate dag, int uur) {
EntityManager em = getEntityManager();
Query q1 = em.createQuery("select b from booking as b where (b.dag=?1 AND b.uur =?2) ");
q1.setParameter(1, dag);
q1.setParameter(2, uur);
if (q1.getMaxResults() == 0) {
return true;
} else {
return false;
}
}
public void boekTafel(Boeking bean) {
EntityManager em = getEntityManager();
em.persist(bean);
em.close();
emf.close();
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
DriverLoader
package be.pxl.tafelboeker.dao;
public class DriverLoader {
static{
System.out.print("loading database");
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println("nope");
e.printStackTrace();
}
}
}
偷看服务
package be.pxl.tafelboeker.services;
import be.pxl.tafelboeker.dao.BoekingDAO;
import be.pxl.tafelboeker.domain.Boeking;
import java.time.LocalDate;
import java.util.Date;
public class BoekingService {
private BoekingDAO dao;
public BoekingService(){
}
public BoekingDAO getDao() {
return dao;
}
public void setDao(BoekingDAO dao) {
this.dao = dao;
}
private boolean isTafelBeschikbaar(LocalDate dag, int uur){
return dao.isTafelBeschikbaar(dag, uur) ;
}
public boolean boekTafel(Boeking boeking){
LocalDate dag = boeking.getDag();
int uur = boeking.getUur();
if(isTafelBeschikbaar(dag, uur)){
dao.boekTafel(boeking);
return true;
}
return false;
}
}
POM
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>be.pxl</groupId>
<artifactId>HerexamensOef</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>HerexamensOef Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<finalName>HerexamensOef</finalName>
</build>
</project>
持久性
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">
<persistence-unit name="booking" transaction-type="RESOURCE_LOCAL">
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/booking" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="" />
<property name="javax.persistence.schema-generation.database.action"
value="none" />
<!-- Hibernate specific -->
<property name="hibernate.show_sql" value="true" />
</properties>
</persistence-unit>
</persistence>
错误
HTTP Status 500 - Servlet execution threw an exception
type Exception report
message Servlet execution threw an exception
description The server encountered an internal error that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: Servlet execution threw an exception
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
java.lang.NoClassDefFoundError: javax/persistence/Persistence
be.pxl.tafelboeker.dao.BoekingDAO.getEntityManager(BoekingDAO.java:35)
be.pxl.tafelboeker.dao.BoekingDAO.isTafelBeschikbaar(BoekingDAO.java:43)
be.pxl.tafelboeker.services.BoekingService.isTafelBeschikbaar(BoekingService.java:28)
be.pxl.tafelboeker.services.BoekingService.boekTafel(BoekingService.java:34)
be.pxl.tafelboeker.servlets.ConfirmServlet.doPost(ConfirmServlet.java:59)
javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
note The full stack trace of the root cause is available in the Apache Tomcat/8.0.33 logs.
预订SQL
-- phpMyAdmin SQL Dump
-- version 4.4.10
-- http://www.phpmyadmin.net
--
-
- Host: localhost
-- Generation Time: Jun 03, 2016 at 06:50 PM
-- Server version: 5.5.42
-- PHP Version: 7.0.0
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
--
-- Database: `boekingdb`
--
-- --------------------------------------------------------
--
-- Table structure for table `BOOKING`
--
CREATE TABLE `BOOKING` (
`id` bigint(20) NOT NULL,
`DAY` date DEFAULT NULL,
`NAME` varchar(255) DEFAULT NULL,
`CITY` varchar(255) DEFAULT NULL,
`STREET` varchar(255) DEFAULT NULL,
`HOUR` int(11) DEFAULT NULL
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;
--
-- Indexes for table `BOOKING`
--
ALTER TABLE `BOOKING`
ADD PRIMARY KEY (`id`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `BOOKING`
--
ALTER TABLE `BOOKING`
MODIFY `id` bigint(20) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=4;
答案 0 :(得分:3)
您的持久性文件使用2.1版本的JPA架构:
{"seatCode":"40B"}
所以你应该使用匹配的hibernate版本:min Hibernate 4.3.X. 我想你在persistence.xml中使用Hibernate
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">
因此,如果您想使用JPA作为接口,则应在pom.xml中添加此依赖项:
<property name="hibernate.show_sql" value="true" />
此依赖项包括<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.2.2.Final</version>
</dependency>
,其中包含类hibernate-jpa-2.1-api
否则,如果你想直接使用Hibernate而不使用JPA作为接口,你可以使用这个依赖(或+ 4.3.X):
javax.persistence.Persistence
答案 1 :(得分:0)
您必须将javax.persistence-api.jar添加为依赖项:
<!-- https://mvnrepository.com/artifact/javax.persistence/persistence-api -->
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0.2</version>
</dependency>