运行JPA购物车时收到以下错误。我在下面包含了DBUtil和Product DB代码:
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)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
root cause
java.lang.NoClassDefFoundError: Could not initialize class music.data.DBUtil
music.data.ProductDB.selectProducts(ProductDB.java:57)
music.admin.ProductAdminController.displayProducts(ProductAdminController.java:74)
music.admin.ProductAdminController.doGet(ProductAdminController.java:36)
javax.servlet.http.HttpServlet.service(HttpServlet.java:618)
javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
这是data.DBUtil:
package music.data;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
public class DBUtil {
private static final EntityManagerFactory emf =
Persistence.createEntityManagerFactory("productMaint2PU");
public static EntityManagerFactory getEmFactory() {
return emf;
}
}
以下是产品数据库:
package music.data;
import java.sql.*;
import java.util.*;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.NoResultException;
import javax.persistence.TypedQuery;
import music.business.Product;
public class ProductDB
{
//This method returns null if a product isn't found.
public static Product selectProduct(String productCode)
{
EntityManager em = DBUtil.getEmFactory().createEntityManager();
String qString = "SELECT p FROM Product p " +
"WHERE p.productCode = :productCode";
TypedQuery<Product> q = em.createQuery(qString, Product.class);
q.setParameter("productCode", productCode);
try {
Product product = q.getSingleResult();
return product;
} catch (NoResultException e) {
return null;
} finally {
em.close();
}
}
//This method will return 0 if productID isn't found.
// public static int selectProductID(Product product)
//This method returns null if a product isn't found.
public static Product selectProduct(int productID)
{
EntityManager em = DBUtil.getEmFactory().createEntityManager();
String qString = "SELECT p FROM Product p " +
"WHERE p.productID = :productID";
TypedQuery<Product> q = em.createQuery(qString, Product.class);
q.setParameter("productID", productID);
try {
Product product = q.getSingleResult();
return product;
} catch (NoResultException e) {
return null;
} finally {
em.close();
}
}
//This method returns null if a product isn't found.
public static List<Product> selectProducts()
{
EntityManager em = DBUtil.getEmFactory().createEntityManager();
String qString = "SELECT p from Product p";
TypedQuery<Product> q = em.createQuery(qString, Product.class);
List<Product> products;
try {
products = q.getResultList();
if (products == null || products.isEmpty())
products = null;
} finally {
em.close();
}
return products;
}
//adds a new product
public static void insertProducts(Product product)
{
EntityManager em = DBUtil.getEmFactory().createEntityManager();
EntityTransaction trans = em.getTransaction();
trans.begin();
try {
em.persist(product);
trans.commit();
} catch (Exception e) {
System.out.println(e);
trans.rollback();
} finally {
em.close();
}
}
//updates the product
public static void updateProduct(Product product)
{
EntityManager em = DBUtil.getEmFactory().createEntityManager();
EntityTransaction trans = em.getTransaction();
trans.begin();
try {
em.merge(product);
trans.commit();
} catch (Exception e) {
System.out.println(e);
trans.rollback();
} finally {
em.close();
}
}
//checks to see if the product exists
public static boolean exists(String productCode) {
Product p = selectProduct(productCode);
return p != null;
}
//removes the product
public static void removeProduct(Product product)
{
EntityManager em = DBUtil.getEmFactory().createEntityManager();
EntityTransaction trans = em.getTransaction();
trans.begin();
try {
em.remove(em.merge(product));
trans.commit();
} catch (Exception e) {
System.out.println(e);
trans.rollback();
} finally {
em.close();
}
}
}
如果需要更多代码,请告知我们。
这是完整的堆栈跟踪。我知道我正在使用正确的密码和用户b / c我已经在另一个正在运行的程序上测试了这些:
21-Nov-2015 11:01:06.784 WARNING [http-nio-8084-exec-126] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesJdbc The web application [productMaint2] registered the JDBC driver [com.mysql.jdbc.Driver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
21-Nov-2015 11:01:07.295 INFO [http-nio-8084-exec-126] org.apache.catalina.startup.HostConfig.undeploy Undeploying context [/productMaint2]
21-Nov-2015 11:01:40.935 INFO [http-nio-8084-exec-125] org.apache.catalina.startup.HostConfig.deployDescriptor Deploying configuration descriptor C:\Users\Carla\AppData\Roaming\NetBeans\8.0.2\apache-tomcat-8.0.15.0_base\conf\Catalina\localhost\productMaint2.xml
21-Nov-2015 11:01:43.235 INFO [http-nio-8084-exec-125] org.apache.catalina.startup.HostConfig.deployDescriptor Deployment of configuration descriptor C:\Users\Carla\AppData\Roaming\NetBeans\8.0.2\apache-tomcat-8.0.15.0_base\conf\Catalina\localhost\productMaint2.xml has finished in 2,300 ms
21-Nov-2015 11:01:43.250 INFO [http-nio-8084-exec-128] org.apache.catalina.util.LifecycleBase.start The start() method was called on component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/productMaint2]] after start() had already been called. The second call will be ignored.
[EL Info]: 2015-11-21 11:01:48.41--ServerSession(1856302541)--EclipseLink, version: Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd
[EL Severe]: ejb: 2015-11-21 11:01:48.72--ServerSession(1856302541)--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLException: Access denied for user 'test'@'localhost' (using password: YES)
Error Code: 1045