日记
import java.util.Date;
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 {
private Integer personid;
private String name;
private boolean sex;
private Short age;
private Date birthday;
@Id
@GeneratedValue
public Integer getPersonid(){
return personid;
}
public void setPersonid(Integer personid){
this.personid=personid;
}
@Column(name="personname",nullable=false,length=32)
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
@Column(nullable=false)
public boolean getSex(){
return sex;
}
public void setSex(boolean sex){
this.sex=sex;
}
@Column(nullable=false)
public Short getAge(){
return age;
}
public void setAge(Short age){
this.age=age;
}
public Date getBirthday(){
return birthday;
}
public void setBirthday(Date birthday){
this.birthday=birthday;
}
}
import java.util.Date;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.adam.PersonDAO;
import org.adam.bean.Person;
@Stateless
@Remote({PersonDAO.class})
public class PersonDAOBean implements PersonDAO{
@PersistenceContext
protected EntityManager em;
public String getPersonNameByID(int personid) {
Person person=em.find(Person.class, Integer.valueOf(personid));
return person.getName();
}
public boolean insertPerson(String name, boolean sex,short age, Date birthday){
try{
Person person=new Person();
person.setAge(Short.valueOf(age));
person.setName(name);
person.setBirthday(birthday);
person.setSex(sex);
em.persist(person);
}catch(Exception e){
return false;
}
return true;
}
}
<%@ page contentType="text/html; charset=GBK"%>
<%@ page import="org.adam.PersonDAO,
javax.naming.*,
java.util.Properties,
java.util.Date,
java.text.SimpleDateFormat"%>
<%
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 = (PersonDAO) ctx.lookup("PersonDAOBean/remote");
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
persondao.insertPerson("Adam", true, (short)26,formatter.parse("1980-9-30"));
//添加一个人
out.println(persondao.getPersonNameByID(1)); //取personid为1的人
} catch (Exception e) {
out.println(e.getMessage());
}
%>