J2EE中EJB遇到的问题,有图,有源码,求帮助
程序代码:
做一个网上购书系统,使用的是JDK1.5+JBOSS5.0+MySQL5.1
以下为main.jsp代码:
<%@page contentType="text/html;charset=GBK"%>
<%@page import="book.book.checkinout.BookAgentRemote,book.book.domain.Book,
javax.naming.*,java.util.List"%>
<script language="javascript">
function check(f)
{
if(f.name.value=="")
{
alert("登录名称必须填写!");
return false
}
if(f.pwd.value=="")
{
alert("登录密码必须填写!");
return false;
}
}
function ch(f)
{
if(f.bookname.value=="")
{
alert("图书名称必须填写!");
return false
}
}
</script>
<center><img src="baner.jpg" width="95%">
<table>
<tr>
<form action="so.jsp" name="form1" method="post" onSubmit="return ch(this)"><td>图书搜索:</td><td><input type=text name=bookname></td><td><input type=submit value="搜索"></td></form>
<form name="f" action="login.jsp" method="post" onSubmit="return check(this)"><td>会员名称:</td><td><input type=text name=name></td><td>会员密码:</td><td><input type=password name=pwd></td><td><input type=submit value="登录"></td></form> <td><a href=reg>会员注册</a></td></tr></table>
<table><tr>
<%
try{
InitialContext ctx=new InitialContext();
BookAgentRemote bookdao=(BookAgentRemote)ctx.lookup("BookAgentBean/remote");
String index=request.getParameter("index");
if(index==null || "".equals(index.trim()))
index="1";
int max=2;
int whichpage=Integer.parseInt(index);
List<Book> books=bookdao.getBookList(max,whichpage);
for(Book b:books){
int id=b.getBid();
String name=b.getBName();
String price=b.getBPrice();
out.println("<td><table>");
out.println("<tr><td rowspan=3><a href=><img src=20.jpg></a></td><td>"+name+"</td></tr><tr><td>"+price+"</td></tr><tr><td><a href=addBook.jsp?id="+id+">购买</a></td></tr>");
out.println("</table></td>");
}
int after=whichpage+1;
int before=whichpage-1;
if(before==0)
before=1;
out.println("</tr><tr align=center><td><a href=main.jsp?index="+before+">上一页</a>");
out.println("<a href=main.jsp?index="+after+">下一页</a></td></tr>");
}catch(Exception e){
out.println(e.getMessage());
}
%>
</table>
</center>
<table width="95%" border="0" cellspacing="0" cellpadding="0" align="center" height="1">
<tr>
<td bgcolor="#000000"> </td>
</tr>
</table>
<table width="96%" border="0" cellspacing="0" cellpadding="4" align="center">
<tr>
<td>
<div align="center">
IT在中国电脑学习网
!
@2007<br>
公司地址:
郑州市二七路200号金博大D座2708室<br>
电话:
66202195电子邮件:
<A HREF='mailto:<!--Email-->'></A>itzcn@126.com</div></td></tr>
</table>
</td>
</tr>
</table>
</td>
<td width="30"> </td>
</tr>
</table>
</BODY>
</HTML>
</table>
以下为BookAgentBean.java代码:
package book.book.checkinout;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.*;
import book.book.domain.*;
@Stateless
public class BookAgentBean implements BookAgentRemote{
@PersistenceContext
(unitName="JEE_book_PU")
private EntityManager manager;
public void createBook(Book book) {
manager.persist(book);
}
public Book findBook(int pKey) {
return manager.find(Book.class, pKey);
}
public Book findBook(String isbn)
{
Query query = manager.createQuery("from Book bk where bk.isbn = ?1");
query.setParameter(1, isbn);
return (Book)query.getSingleResult();
}
public List<Book> getBookList3(String bName){
Query query=manager.createQuery("from Book o where o.BName=?1");
query.setParameter(1, bName);
return (List<Book>)query.getResultList();
}
public List<Book> getBookList(int max,int whichpage){
int index=(whichpage-1)*max;
Query query=manager.createQuery("select p from Book p order by p.bid asc");
List<Book> result=(List<Book>)query.setMaxResults(max).setFirstResult(index).getResultList();
return result;
}
}
以下为main.jsp代码:
<%@page contentType="text/html;charset=GBK"%>
<%@page import="book.book.checkinout.BookAgentRemote,book.book.domain.Book,
javax.naming.*,java.util.List"%>
<script language="javascript">
function check(f)
{
if(f.name.value=="")
{
alert("登录名称必须填写!");
return false
}
if(f.pwd.value=="")
{
alert("登录密码必须填写!");
return false;
}
}
function ch(f)
{
if(f.bookname.value=="")
{
alert("图书名称必须填写!");
return false
}
}
</script>
<center><img src="baner.jpg" width="95%">
<table>
<tr>
<form action="so.jsp" name="form1" method="post" onSubmit="return ch(this)"><td>图书搜索:</td><td><input type=text name=bookname></td><td><input type=submit value="搜索"></td></form>
<form name="f" action="login.jsp" method="post" onSubmit="return check(this)"><td>会员名称:</td><td><input type=text name=name></td><td>会员密码:</td><td><input type=password name=pwd></td><td><input type=submit value="登录"></td></form> <td><a href=reg>会员注册</a></td></tr></table>
<table><tr>
<%
try{
InitialContext ctx=new InitialContext();
BookAgentRemote bookdao=(BookAgentRemote)ctx.lookup("BookAgentBean/remote");
String index=request.getParameter("index");
if(index==null || "".equals(index.trim()))
index="1";
int max=2;
int whichpage=Integer.parseInt(index);
List<Book> books=bookdao.getBookList(max,whichpage);
for(Book b:books){
int id=b.getBid();
String name=b.getBName();
String price=b.getBPrice();
out.println("<td><table>");
out.println("<tr><td rowspan=3><a href=><img src=20.jpg></a></td><td>"+name+"</td></tr><tr><td>"+price+"</td></tr><tr><td><a href=addBook.jsp?id="+id+">购买</a></td></tr>");
out.println("</table></td>");
}
int after=whichpage+1;
int before=whichpage-1;
if(before==0)
before=1;
out.println("</tr><tr align=center><td><a href=main.jsp?index="+before+">上一页</a>");
out.println("<a href=main.jsp?index="+after+">下一页</a></td></tr>");
}catch(Exception e){
out.println(e.getMessage());
}
%>
</table>
</center>
<table width="95%" border="0" cellspacing="0" cellpadding="0" align="center" height="1">
<tr>
<td bgcolor="#000000"> </td>
</tr>
</table>
<table width="96%" border="0" cellspacing="0" cellpadding="4" align="center">
<tr>
<td>
<div align="center">
IT在中国电脑学习网
!
@2007<br>
公司地址:
郑州市二七路200号金博大D座2708室<br>
电话:
66202195电子邮件:
<A HREF='mailto:<!--Email-->'></A>itzcn@126.com</div></td></tr>
</table>
</td>
</tr>
</table>
</td>
<td width="30"> </td>
</tr>
</table>
</BODY>
</HTML>
</table>
以下为BookAgentBean.java代码:
package book.book.checkinout;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.*;
import book.book.domain.*;
@Stateless
public class BookAgentBean implements BookAgentRemote{
@PersistenceContext
(unitName="JEE_book_PU")
private EntityManager manager;
public void createBook(Book book) {
manager.persist(book);
}
public Book findBook(int pKey) {
return manager.find(Book.class, pKey);
}
public Book findBook(String isbn)
{
Query query = manager.createQuery("from Book bk where bk.isbn = ?1");
query.setParameter(1, isbn);
return (Book)query.getSingleResult();
}
public List<Book> getBookList3(String bName){
Query query=manager.createQuery("from Book o where o.BName=?1");
query.setParameter(1, bName);
return (List<Book>)query.getResultList();
}
public List<Book> getBookList(int max,int whichpage){
int index=(whichpage-1)*max;
Query query=manager.createQuery("select p from Book p order by p.bid asc");
List<Book> result=(List<Book>)query.setMaxResults(max).setFirstResult(index).getResultList();
return result;
}
}
搜索更多相关主题的帖子:
javascript 网上购书 function color
----------------解决方案--------------------------------------------------------