У меня есть эта ошибка, но я не знаю, где проблема. Я предполагаю, что eclipseLink- mysql вместе не совпадают. Я использую Tomcat 8.0.27.0 и mysql -connector-java8-5.1.23.jar, но я изменяю файл jar. Я принимаю ошибку. Я не понимаю, где произошла ошибка? Я использовал netbeans 8.2R C. И я думаю, что мои коды очень легко читаются.
Я хочу сделать сущность продукта. затем запишите в базу данных, я использовал jsf и jsp, но просто mysql не подключается.
STACK TRACE:
com.sun.faces.mgbean.ManagedBeanCreationException: Cant instantiate class: com.kerem.inventory.faces.ProductSummaryBean.
at com.sun.faces.mgbean.BeanBuilder.newBeanInstance(BeanBuilder.java:191)
at com.sun.faces.mgbean.BeanBuilder.build(BeanBuilder.java:100)
at com.sun.faces.mgbean.BeanManager.createAndPush(BeanManager.java:409)
at com.sun.faces.mgbean.BeanManager.create(BeanManager.java:269)
at com.sun.faces.el.ManagedBeanELResolver.resolveBean(ManagedBeanELResolver.java:257)
at com.sun.faces.el.ManagedBeanELResolver.getValue(ManagedBeanELResolver.java:117)
at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
at org.apache.el.parser.AstIdentifier.getValue(AstIdentifier.java:80)
at org.apache.el.parser.AstValue.getValue(AstValue.java:137)
at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:184)
at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109)
at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)
at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182)
at javax.faces.component.UIData.getValue(UIData.java:732)
at javax.faces.component.UIData.getDataModel(UIData.java:1822)
at javax.faces.component.UIData.setRowIndexWithoutRowStatePreserved(UIData.java:484)
at javax.faces.component.UIData.setRowIndex(UIData.java:473)
at com.sun.faces.renderkit.html_basic.TableRenderer.encodeBegin(TableRenderer.java:81)
at javax.faces.component.UIComponentBase.encodeBegin(UIComponentBase.java:864)
at javax.faces.component.UIData.encodeBegin(UIData.java:1133)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1854)
at javax.faces.render.Renderer.encodeChildren(Renderer.java:176)
at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:889)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1856)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:456)
at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:133)
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:647)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:217)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
Caused by: javax.persistence.PersistenceException: Exception [EclipseLink-4003] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DatabaseException
Exception Description: Configuration error. Class [com.mysql.jdbc.Driver] not found.
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:766)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.getAbstractSession(EntityManagerFactoryDelegate.java:204)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.getDatabaseSession(EntityManagerFactoryDelegate.java:182)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.getDatabaseSession(EntityManagerFactoryImpl.java:527)
at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactoryImpl(PersistenceProvider.java:140)
at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactory(PersistenceProvider.java:177)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:79)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
at com.kerem.inventory.repository.ProductRepository.getList(ProductRepository.java:17)
at com.kerem.inventory.faces.ProductSummaryBean.<init>(ProductSummaryBean.java:20)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at java.lang.Class.newInstance(Class.java:442)
at com.sun.faces.mgbean.BeanBuilder.newBeanInstance(BeanBuilder.java:186)
... 56 more
Caused by: Exception [EclipseLink-4003] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DatabaseException
Exception Description: Configuration error. Class [com.mysql.jdbc.Driver] not found.
at org.eclipse.persistence.exceptions.DatabaseException.configurationErrorClassNotFound(DatabaseException.java:89)
at org.eclipse.persistence.sessions.DefaultConnector.loadDriverClass(DefaultConnector.java:267)
at org.eclipse.persistence.sessions.DefaultConnector.connect(DefaultConnector.java:85)
at org.eclipse.persistence.sessions.DatasourceLogin.connectToDatasource(DatasourceLogin.java:162)
at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.setOrDetectDatasource(DatabaseSessionImpl.java:204)
at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.loginAndDetectDatasource(DatabaseSessionImpl.java:741)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.login(EntityManagerFactoryProvider.java:239)
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:685)
... 71 more
постоянство. xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" 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">
<persistence-unit name="WebApplication5PU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>com.kerem.inventory.entity.Product</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/jsfkerem?zeroDateTimeBehavior=convertToNull"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.password" value="kerem2112"/>
<property name="javax.persistence.schema-generation.database.action" value="create"/>
</properties>
</persistence-unit>
</persistence>
Мой класс сущности Product. java
package com.kerem.inventory.entity;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity @Table(name="Product")
public class Product implements Serializable{
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="productId")
private long productId;
@Column(name="productName")
private String productName;
@Column(name="salesPrice")
private double salesPrice;
public Product(long productId, String productName, double salesPrice) {
this.productId = productId;
this.productName = productName;
this.salesPrice = salesPrice;
}
public Product() {
}
public long getProductId() {
return productId;
}
public void setProductId(long productId) {
this.productId = productId;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public double getSalesPrice() {
return salesPrice;
}
public void setSalesPrice(double salesPrice) {
this.salesPrice = salesPrice;
}
}
Мой класс репозитория ProductRepository. java
package com.kerem.inventory.repository;
import com.kerem.inventory.entity.Product;
import java.util.*;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
public class ProductRepository {
public ProductRepository() {
}
public List<Product> getList() {
EntityManagerFactory factory = Persistence.createEntityManagerFactory("WebApplication5PU");
EntityManager manager = factory.createEntityManager();
String string = "select product from Product as product";
Query query = manager.createQuery(string);
List<Product> productList = query.getResultList();
manager.close();
return productList;
}
public void insert(Product product) {
EntityManagerFactory factory = Persistence.createEntityManagerFactory("WebApplication5PU");
EntityManager manager = factory.createEntityManager();
manager.getTransaction().begin();
manager.persist(product);
manager.getTransaction().commit();
manager.close();
}
public Product find(long productId) {
EntityManagerFactory factory = Persistence.createEntityManagerFactory("WebApplication5PU");
EntityManager manager = factory.createEntityManager();
Product product = manager.find(Product.class,productId);
manager.close();
return product;
}
public void update(Product product) {
EntityManagerFactory factory = Persistence.createEntityManagerFactory("WebApplication5PU");
EntityManager manager = factory.createEntityManager();
manager.getTransaction().begin();
manager.merge(product);
manager.getTransaction().commit();
manager.close();
}
public void remove(long productId) {
EntityManagerFactory factory = Persistence.createEntityManagerFactory("WebApplication5PU");
EntityManager manager = factory.createEntityManager();
Product product = manager.find(Product.class,productId);
manager.remove(product);
manager.close();
}
}
ProductSummaryBean. java
package com.kerem.inventory.faces;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import java.util.*;
import com.kerem.inventory.entity.*;
import com.kerem.inventory.repository.ProductRepository;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
@ManagedBean
@RequestScoped
public class ProductSummaryBean {
private List<Product> productList;
public ProductSummaryBean() {
ProductRepository repository = new ProductRepository();
productList = repository.getList();
}
public List<Product> getProductList() {
return productList;
}
public void delete(){
long productId = getProductId();
System.out.println("Product Id : "+productId);
ProductRepository repository = new ProductRepository();
repository.remove(productId);
}
private long getProductId() {
HttpServletRequest request = (HttpServletRequest) FacesContext
.getCurrentInstance()
.getExternalContext()
.getRequest();
long productId = 0;
if (request.getParameter("productId") != null) {
productId = Long.parseLong(request.getParameter("productId"));
}
return productId;
}
}
ProductLoginBean. java
package com.kerem.inventory.faces;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import com.kerem.inventory.entity.*;
import com.kerem.inventory.repository.ProductRepository;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
@ManagedBean
@RequestScoped
public class ProductLoginBean {
private Product product;
public ProductLoginBean() {
long productId = getProductId();
if (productId == 0) {
product = new Product();
} else {
ProductRepository repository = new ProductRepository();
product = repository.find(productId);
}
}
private long getProductId() {
HttpServletRequest request = (HttpServletRequest) FacesContext
.getCurrentInstance()
.getExternalContext()
.getRequest();
long productId = 0;
if (request.getParameter("productId") != null) {
productId = Long.parseLong(request.getParameter("productId"));
}
return productId;
}
public void save() {
ProductRepository repository = new ProductRepository();
long productId = getProductId();
System.out.println("Saving"
+ product.getProductName() + " " + product.getProductId() + " " + product.getSalesPrice());
if (productId != 0) {
repository.update(product);
} else {
repository.insert(product);
}
}
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
}
ProductLoginPage.x html
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<h:form>
Id -> <h:inputText value="#{productLoginBean.product.productId}"/><br/>
Name -> <h:inputText value="#{productLoginBean.product.productName}"/><br/>
Sales Price -> <h:inputText value="#{productLoginBean.product.salesPrice}"/> <br/>
<h:commandButton value="Add" action="#{productLoginBean.save()}">
<f:param name="productId" value="#{productLoginBean.product.productId}"/>
</h:commandButton>
</h:form>
</h:body>
</html>
ProductSummaryPage.x html
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<h1>Product Summary Page</h1>
<h:form>
<h:dataTable value="#{productSummaryBean.productList}" var="product" border="1">
<h:column>
<h:outputText value="#{product.productId}"/>
</h:column>
<h:column>
<h:outputText value="#{product.productName}"/>
</h:column>
<h:column>
<h:outputText value="#{product.salesPrice}"/>
</h:column>
<h:column>
<h:outputLink value="ProductLoginPage.xhtml">
<f:param name="productId" value="#{product.productId}"/>
Update
</h:outputLink>
</h:column>
<h:column>
<h:commandLink value="Sil" action="#{productSummaryBean.delete()}">
<f:param name="productId" value="#{product.productId}"/>
</h:commandLink>
</h:column>
</h:dataTable>
<h:commandLink action="ProductLoginPage.xhtml" value="Add"/>
</h:form>
</h:body>
</html>