packageDao.Impl;importjava.sql.Connection;importjava.sql.PreparedStatement;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.util.ArrayList;importjava.util.List;importentity.pet;importDB.JNDI;importDao.petDao;public class petDaoImpl extends JNDI implementspetDao {privateConnection cn;privatePreparedStatement ps;privateResultSet rs;public ListreturnList() {
List li=new ArrayList();
cn=super.getConnection();
String sql="select * from pet order by petId";try{
ps=cn.prepareStatement(sql);
rs=ps.executeQuery();while(rs.next()){
pet pe=newpet();
pe.setPetId(rs.getInt("petId"));
pe.setPetName(rs.getString("petName"));
pe.setPetBread(rs.getString("petBread"));
pe.setPetSex(rs.getString("petSex"));
pe.setBirthday(rs.getString("birthday"));
pe.setDescription(rs.getString("description"));
li.add(pe);
}
}catch(SQLException e) {//TODO Auto-generated catch block
e.printStackTrace();
}returnli;
}public intinsertPet(pet pet) {int i=0;
cn=super.getConnection();
String sql="insert into pet values (pet_squ.nextval,?,?,?,to_date(?,'YYYY-MM-dd'),?)";try{
ps=cn.prepareStatement(sql);
ps.setString(1,pet.getPetName() );
ps.setString(2, pet.getPetBread());
ps.setString(3,pet.getPetSex() );
ps.setString(4,pet.getBirthday() );
ps.setString(5,pet.getDescription() );
i=ps.executeUpdate();
}catch(SQLException e) {//TODO Auto-generated catch block
e.printStackTrace();
}returni;
}public ListselectPet(String petType) {
List li=new ArrayList();if(petType.equals("请选择")){
cn=super.getConnection();
String sql="select * from pet order by petId";try{
ps=cn.prepareStatement(sql);
rs=ps.executeQuery();while(rs.next()){
pet pe=newpet();
pe.setPetId(rs.getInt("petId"));
pe.setPetName(rs.getString("petName"));
pe.setPetBread(rs.getString("petBread"));
pe.setPetSex(rs.getString("petSex"));
pe.setBirthday(rs.getString("birthday"));
pe.setDescription(rs.getString("description"));
li.add(pe);
}
}catch(SQLException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
}else{
cn=super.getConnection();
String sql="select * from pet where petBread=? order by petId";try{
ps=cn.prepareStatement(sql);
ps.setString(1,petType);
rs=ps.executeQuery();while(rs.next()){
pet pe=newpet();
pe.setPetId(rs.getInt("petId"));
pe.setPetName(rs.getString("petName"));
pe.setPetBread(rs.getString("petBread"));
pe.setPetSex(rs.getString("petSex"));
pe.setBirthday(rs.getString("birthday"));
pe.setDescription(rs.getString("description"));
li.add(pe);
}
}catch(SQLException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
}returnli;
}
}