index.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Inventory</title>
</head>
<body>
<form action="checkInventory.jsp" method="post">
<table>
<tr>
<td colspan="2" align="center"><b>Add the item in Inventory</b></td>
</tr>
<tr><td>Item Name</td>
<td><input name="itemname" maxlength="25" size="50" /></td>
</tr>
<tr><td>Description</td>
<td><textarea rows="5" cols="36" name="description"></textarea></td>
</tr>
<tr><td>Quantity:</td>
<td><input name="quantity" maxlength="4" size="5"/></td>
</tr>
<tr><td colspan="2">
<input type="submit" name="AddToInventory" value="AddToInventory"/>
</td></tr>
</table>
</form>
</body>
</html>
inventory.java
package myAPP;
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="inventory")
public class inventory
{
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="itemid", unique=true, updatable=false)
private Integer itemid;
@Column(name="itemName")
private String itemName;
@Column(name="description")
private String description;
@Column(name="quantity")
private Integer quantity;
@Column(name="addedDate")
private String addedDate;
public inventory()
{
}
public Integer getItemID()
{
return itemid;
}
public void setItemID(Integer itemid)
{
this.itemid=itemid;
}
public String getItemName()
{
return itemName;
}
public void setItemName(String itemName)
{
this.itemName=itemName;
}
public String getDescription()
{
return description;
}
public void setDescription(String description)
{
this.description=description;
}
public Integer getQuantity()
{
return quantity;
}
public void setQuantity(Integer quantity)
{
this.quantity=quantity;
}
public String getAddedDate()
{
return addedDate;
}
public void setAddedDate(String addedDate)
{
this.addedDate=addedDate;
}
}
checkInventory.jsp
<%@page import="java.util.Iterator"%>
<%@page import="javax.persistence.Persistence"%>
<%@page import="java.util.List"%>
<%@page import="myAPP.inventory"%>
<%@page import="javax.persistence.EntityTransaction"%>
<%@page import="javax.persistence.EntityManager"%>
<%@page import="javax.persistence.EntityManagerFactory"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%!
private EntityManagerFactory eMF;
private EntityManager eM;
private EntityTransaction eT;
List<inventory>inv;
%>
<%eMF=Persistence.createEntityManagerFactory("INVJPAPU");
eM=eMF.createEntityManager();
String submit=request.getParameter("AddToInventory");
if(submit!=null&&("AddToInventory").equals(submit))
{
try
{
String itemname=request.getParameter("itemname");
String description=request.getParameter("description");
String quantity1=request.getParameter("quantity");
int quantity=Integer.parseInt(quantity1);
String addedDate=new java.util.Date().toString();
inventory inv=new inventory();
inv.setItemName(itemname);
inv.setDescription(description);
inv.setQuantity(quantity);
inv.setAddedDate(addedDate);
eT=eM.getTransaction();
eT.begin();
eM.persist(inv);
eT.commit();
}
catch(RuntimeException e)
{
if(eT!=null)eT.rollback();
throw e;
}
response.sendRedirect("checkInventory.jsp");
}
try
{
inv=eM.createQuery("SELECT i from Inventory i").getResultList();
}
catch(RuntimeException e)
{
throw e;
}
eM.close();
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Inventory</title>
</head>
<body>
<table border="1"><tr><td colspan="5" align="center">
<b><Click<a href="index.jsp"></a>to add items in the inventory</b>
</td></tr>
<tr><td>Item No</td>
<td>Item Name</td>
<td Description</td>
<td>Quantity</td>
<td>Added Date & Time</td>
</tr>
<%
Iterator iterator=inv.iterator();
while(iterator.hasNext())
{
inventory objInv=(inventory)iterator.next();
%>
<tr><td><b><%=objInv.getItemID()%></b></td>
<td><b><%=objInv.getItemName()%></b></td>
<td><b><%=objInv.getDescription()%></b></td>
<td><b><%=objInv.getQuantity()%></b></td>
<td><b><%=objInv.getAddedDate()%></b></td>
</tr>
<%
}
%>
</td></table>
</body>
</html>
库存表
create table Inventory(itemid int PRIMARY KEY AUTO_INCREMENT, itemName varchar(50), description varchar(200), quantity int(40),addedDate varchar(40));
persistence.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="INVJPAPU" transaction-type="RESOURCE_LOCAL">
<non-jta-data-source>jdbc/inventory</non-jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties/>
</persistence-unit>
</persistence>
我正面临“ jdbc /库存”的查找失败 根本原因:
javax.persistence.PersistenceException:异常[EclipseLink-7060] (Eclipse Persistence Services-2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.ValidationException异常 说明:无法获取数据源[jdbc /库存]。内部 异常:javax.naming.NamingException:查找失败 的“ jdbc /库存” SerialContext [myEnv = {java.naming.factory.initial = com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state = com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs = com.sun.enterprise.naming} [Root 异常是javax.naming.NameNotFoundException:找不到库存]
我已附上上面的错误屏幕截图。请帮助我解决问题。
谢谢!