Package Com. Persia. JPA. onetable; Import Java. Io. serializable; Import Javax. Persistence. column; Import Javax. Persistence. entity; Import Javax. Persistence. generatedvalue; Import Javax. Persistence. ID; Import Javax. Persistence. Table; @ entity @ table (name =" Person ") Public Class Person Implements Serializable { Private Integer ID; Private String name; Private Short age; @ ID @ generatedvalue Public Integer GETID (){ Return ID ;} Public Void Setid (integer ID ){ This . ID = ID;} @ column (name =" Personname ", Nullable = False , Length = 32) Public String getname (){Return Name ;} Public Void Setname (string name ){ This . Name = Name ;}@ column (nullable = False ) Public Short getage (){ Return Age ;} Public Void Setage (short age ){ This . Age = age ;}}
Package Com. Persia. entity. Dao; Import Java. util. List;Import Javax. EJB. Remote; Import Javax. EJB. stateless; Import Javax. Persistence. entitymanager; Import Javax. Persistence. persistencecontext; Import Javax. Persistence. query; Import Com. Persia. JPA. onetable. person; @ stateless @ remote ({persondao. Class }) Public Class Persondaobean Implements Persondao {@ persistencecontext Protected Entitymanager em; @ override Public Person getpersonbyid ( Int Personid ){ Return Em. Find (person. Class , Integer. valueof (personid);} @ override Public List getpersonlist ( Int Max, Int Whichpage ){ // Todo auto-generated method stub Try { Int Index = (whichpage-1) * max; query = em. createquery ("From person P order by ID ASC "); List list = query. setmaxresults (max). setfirstresult (INDEX). getresultlist (); em. Clear (); Return List ;} Catch (Exception e) {e. printstacktrace (); Return Null ; }}@ Override Public String getpersonnamebyid ( Int Personid) {person P = em. Find (person. Class , Integer. valueof (personid )); Return P. getname () ;}@ override Public Boolean Insertperson (string name, Short Age ){ Try {Person P = New Person (); p. setname (name); p. setage (short. valueof (AGE); em. persist (p );} Catch (Exception e) {e. printstacktrace (); Return False ;} Return True ;} @ Override Public Boolean Updateperson (person ){// Todo auto-generated method stub Try {Em. Merge (person );} Catch (Exception e) {e. printstacktrace (); Return False ;} Return True ;}}
JSP test page
<% Properties props = New Properties (); props. setproperty (" Java. Naming. Factory. Initial "," Org. jnp. Interfaces. namingcontextfactory "); Props. setproperty ("Java. Naming. provider. url "," Localhost: 1099 "); Props. setproperty (" Java. Naming. Factory. url. pkgs "," Org. JBoss. Naming "); Initialcontext CTX = New Initialcontext (props ); Try {Persondao = (persondao) CTX. Lookup (" Persondaobean/remote "); Persondao. insertperson (" Persia ",( Short ) 26); Out. println (persondao. getpersonnamebyid (1); person = persondao. getpersonbyid (1); person. setname (" Linda "); Persondao. updateperson (person); Out. println (" <Br> =============== pagination display ===========================< br/> "); String Index = request. getparameter (" Index "); If (Index = Null |" ". Equals (index. Trim () Index =" 1 "; Int Max = 2; Int Whichpage = integer. parseint (INDEX); List list = persondao. getpersonlist (max, whichpage ); If (List! = Null ) {Iterator it = List. iterator (); While (It. hasnext () {person P = (person) it. Next (); Out. println (" No: "+ P. GETID () +" Name: "+ P. getname () +" <Br> ");}}} Catch (Exception e) {out. println (E. getmessage () ;}%>