mysql 2

来源:互联网 发布:新开淘宝店卖什么好 编辑:程序博客网 时间:2024/06/10 14:19
public class PetDaoImpl extends BaseDao implements PetDao{
 @Override
 public List<Pet> getAllPet() {
  ResultSet rs = this.executeQuery("select * from pet", null);//查询pey表操作
  List<Pet> list = new ArrayList<Pet>();
  try {
   while(rs.next()){
    Pet pet = new Pet();有set和get 方法
    pet.setName(rs.getString("name"));//查询名字pet 类中有。re自带 getstring
    pet.setTypeName(rs.getString("typename"));//查询类型
    list.add(pet);
   }
  } catch (SQLException e) {
   e.printStackTrace();
  }finally{
   this.closeAll(null, null,rs);
  }
  return list;
 }
 @Override
 public List<Pet> selectPet(String sql, String[] param) {
  // TODO Auto-generated method stub
  return null;
 }
 @Overrides
 public int updatePet(String sql, Object[] param) {//定义update方法 。和param数组
//增删查改的操作方法
public int executeSQL(String preparedSql, Object[] param) {
  Connection conn = null;
  PreparedStatement pstmt = null;
  int num = 0;
  /* 处理SQL,执行SQL */
  try {
   conn = getConn(); // 得到数据库连接
   pstmt = conn.prepareStatement(preparedSql); // 得到PreparedStatement对象
   if (param != null) {
    for (int i = 0; i < param.length; i++) {
     pstmt.setObject(i + 1, param[i]); // 为预编译sql设置参数
    }
   }
   // System.out.println(preparedSql);
   num = pstmt.executeUpdate(); // 执行SQL语句
  } catch (ClassNotFoundException e) {
   e.printStackTrace(); // 处理ClassNotFoundException异常
  } catch (SQLException e) {
   e.printStackTrace(); // 处理SQLException异常
  } finally {
   this.closeAll(conn, pstmt, null);
  }
  return num;
 }

  // TODO Auto-generated method stub
  return 0;
 }
0 0
原创粉丝点击