⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 petdto.java

📁 宠物医院
💻 JAVA
字号:
package bean;

import java.sql.ResultSet;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.Vector;

public class petDTO {
  private int petId;
  private String petName;
  private String petBirthDay;
  private int petTypeId;
  private int petOwerId;
  private String petOwerName;
  private String petTypeName;

  JDBCBean jdbc=new JDBCBean();
  Connection con=jdbc.getCon();
  PreparedStatement pstm=null;
  ResultSet rs=null;
  String sql;
  Vector vc=new Vector();
  boolean indiv=false;

  public petDTO() {
  }

  public void setPetId(int petId) {
    this.petId = petId;
  }

  public void setPetName(String petName) {
    this.petName = petName;
  }

  public void setPetBirthDay(String petBirthDay) {
    this.petBirthDay = petBirthDay;
  }

  public void setPetTypeId(int petTypeId) {
    this.petTypeId = petTypeId;
  }

  public void setPetOwerId(int petOwerId) {
    this.petOwerId = petOwerId;
  }

  public void setPetOwerName(String petOwerName) {
    this.petOwerName = petOwerName;
  }

  public void setPetTypeName(String petTypeName) {
    this.petTypeName = petTypeName;
  }

  public int getPetId() {
    return petId;
  }

  public String getPetName() {
    return petName;
  }

  public String getPetBirthDay() {
    return petBirthDay;
  }

  public int getPetTypeId() {
    return petTypeId;
  }

  public int getPetOwerId() {
    return petOwerId;
  }

  public String getPetOwerName() {
    return petOwerName;
  }

  public String getPetTypeName() {
    return petTypeName;
  }

  public Vector getPetType()   //得到宠物的类型,checkbox中动态的显示出来
  {
    try
    {
      vc.clear();
      sql="select name from types ";
      pstm=con.prepareStatement(sql);
      rs=pstm.executeQuery();
      while(rs.next())
      {
        vc.addElement(rs.getString(1));
      }
      pstm.close();
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
    return vc;
  }

  public void getTypeID()  //得到宠物类型的ID
  {
    try
    {
      sql="select id from types where name=?";
      pstm=con.prepareStatement(sql);
      pstm.setString(1,getPetTypeName());
      rs=pstm.executeQuery();
      while(rs.next())
      {
        setPetTypeId(rs.getInt(1));   //设置宠物类型的ID
      }
      pstm.close();
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
  }

  public boolean addType()   //添加宠物类型
  {
    try
    {
      sql="select * from types where name=?";
      pstm=con.prepareStatement(sql);
      pstm.setString(1,getPetTypeName());
      rs=pstm.executeQuery();
      if(rs.next())
      {
        indiv=true;  //已存在此宠物类型
      }
      if(indiv==false)
      {
        sql="insert into types(name) values (?)";
        pstm=con.prepareStatement(sql);
        pstm.setString(1,getPetTypeName());
        pstm.executeUpdate();
        return true;
      }
    }
    catch(Exception e)
    {
      e.printStackTrace();
      return false;
    }
    return false;
  }

  public boolean addPet()   //添加宠物信息
  {
    try
    {
      sql="insert into pets (name,birth_date,type_id,owner_id)"+
          "  values (?,?,?,?)";
      pstm=con.prepareStatement(sql);
      pstm.setString(1,getPetName());
      pstm.setString(2,getPetBirthDay());
      pstm.setInt(3,getPetTypeId());
      pstm.setInt(4,getPetOwerId());
      pstm.executeUpdate();
      pstm.close();

      sql="select * from pets where name=? and birth_date=? and type_id=? and owner_id=?";
      pstm=con.prepareStatement(sql);
      pstm.setString(1,getPetName());
      pstm.setString(2,getPetBirthDay());
      pstm.setInt(3,getPetTypeId());
      pstm.setInt(4,getPetOwerId());
      rs=pstm.executeQuery();
      if(rs.next())
      {
        return true;
      }
      pstm.close();
    }
    catch(Exception e)
    {
      e.printStackTrace();
      return false;
    }
    return false;
  }

  public boolean isInTable()   //判断是否有同名的宠物
  {
    try
    {
      sql="select * from pets where  owner_id=? and name=?";
      pstm=con.prepareStatement(sql);
      System.out.print(getPetOwerId());
      pstm.setInt(1,getPetOwerId());
      pstm.setString(2,getPetName());
      rs=pstm.executeQuery();
      if(rs.next())
      {
        return true;
      }
    }
    catch(Exception e)
    {
      e.printStackTrace();
      return false;
    }
    return false;
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -