📄 pet.java~36~
字号:
package gxaccp.t07.guoneng_wei.petclinicmanagesystem.actionform;
import java.sql.*;
import java.util.*;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import gxaccp.t07.guoneng_wei.jdbc.*;
public class Pet
extends ActionForm { //id petName ownerID ownerName petTypeID petTypeName
private String id; // --宠物编号
private String petName; // --宠物名
private String ownerID; // --所有者ID(主人)
private String ownerName; // --所有者姓名(主人)
private String petTypeID; // --宠物类型
private String petTypeName; // --宠物类型名
private Vector pets=new Vector(); // --宠物类型名
public ActionErrors validate(ActionMapping actionMapping,
HttpServletRequest httpServletRequest) {
/** @todo: finish this method, this is just the skeleton.*/
return null;
}
public void reset(ActionMapping actionMapping,
HttpServletRequest servletRequest) {
id = ""; // --宠物编号
petName = ""; // --宠物名
petTypeID = ""; // --宠物类型
petTypeName = "";
ownerID = ""; // --所有者ID(主人)
ownerName = "";
}
public String getOwnerID() {
return ownerID;
}
public String getPetName() {
return petName;
}
public String getPetTypeID() {
return petTypeID;
}
public String getOwnerName() {
return ownerName;
}
public String getId() {
return id;
}
public String getPetTypeName() {
return petTypeName;
}
public Vector getPets() {
return pets;
}
public void setOwnerID(String ownerID) {
this.ownerID = ownerID;
}
public void setPetName(String petName) {
this.petName = petName;
}
public void setPetTypeID(String petTypeID) {
this.petTypeID = petTypeID;
}
public void setOwnerName(String ownerName) {
this.ownerName = ownerName;
}
public void setId(String id) {
this.id = id;
}
public void setPetTypeName(String petTypeName) {
this.petTypeName = petTypeName;
}
public void setPets(Vector pets) {
this.pets = pets;
}
public String addPetInfo() {
Connection con = DataBaseAccess.getConnection();
CallableStatement cst = null;
String petId =null;
try {
// proc_insertPetInfo @OutID OUTPUT, '咪咪',1,'56315253'
cst = con.prepareCall("{call proc_insertPetInfo(?,?,?,?)}");
cst.registerOutParameter(1, java.sql.Types.CHAR);
cst.setString(2, this.getPetName());
cst.setInt(3, Integer.parseInt(this.getPetTypeID()));
cst.setString(4, this.getOwnerID());
cst.execute();
petId = cst.getString(1);
if (petId != null&&!petId.equals("########")) {
this.setId(petId);
// this.valueToOwnerName();
}
}
catch (SQLException ex) {
System.out.println("-------------出现异常在添加宠动物信息时---------------");
}
finally {
DataBaseAccess.closeStatement(cst);
DataBaseAccess.closeConnection(con);
}
return false;
}
public boolean valueToOwnerName() { //EXEC proc_getOwnerName @ownerName OUTPUT,'56315253'
Connection con = DataBaseAccess.getConnection();
CallableStatement cst = null;
String oName = "";
try {
//proc_insertPetInfo @OutID OUTPUT, '咪咪',1,'56315253'
cst = con.prepareCall("{call proc_getOwnerName (?,?)}");
cst.registerOutParameter(1, java.sql.Types.VARCHAR);
cst.setString(2, this.getOwnerID());
cst.execute();
oName = cst.getString(1);
if (oName != null) {
this.setOwnerName(oName);
return true;
}
}
catch (SQLException ex) {
System.out.println("-------------出现异常在获取宠动物所有者姓名时---------------");
}
finally {
DataBaseAccess.closeStatement(cst);
DataBaseAccess.closeConnection(con);
}
return false;
}
public boolean selectPetInfo() {
Connection con = DataBaseAccess.getConnection();
PreparedStatement pst = null;
ResultSet rs = null;
try {
// OwnerID OwnerName PetID PetName PetTypeID PetTypeName
pst = con.prepareCall("SELECT * FROM view_PetInfo WHERE PetID=?");
pst.setString(1, this.getId());
rs = pst.executeQuery();
if (rs.next()) {
this.setPetName(rs.getString("PetName"));
this.setOwnerID(rs.getString("OwnerID"));
this.setOwnerName(rs.getString("OwnerName"));
this.setPetTypeID(rs.getString("PetTypeID"));
this.setPetTypeName(rs.getString("PetTypeName"));
return true;
}
}
catch (SQLException ex) {
System.out.println("-------------出现异常在selectPetInfo()---------------");
}
finally {
DataBaseAccess.closeResultSet(rs);
DataBaseAccess.closeStatement(pst);
DataBaseAccess.closeConnection(con);
}
return false;
}
public boolean selectPet() {
Connection con = DataBaseAccess.getConnection();
PreparedStatement pst = null;
ResultSet rs = null;
boolean flag=false;
try {
// OwnerID OwnerName PetID PetName PetTypeID PetTypeName
pst = con.prepareCall("SELECT * FROM view_PetInfo WHERE PetName=? AND OwnerName =? ");
// pst.setString(1, this.getId());
pst.setString(1,this.getPetName());
pst.setString(2,this.getOwnerName());
rs = pst.executeQuery();
while (rs.next()) {
Pet p=new Pet();
flag=true;
p.setId(rs.getString("PetID"));
p.setPetName(rs.getString("PetName"));
p.setOwnerID(rs.getString("OwnerID"));
p.setOwnerName(rs.getString("OwnerName"));
p.setPetTypeID(rs.getString("PetTypeID"));
p.setPetTypeName(rs.getString("PetTypeName"));
this.pets.addElement(p);
}
}
catch (SQLException ex) {
System.out.println("-------------出现异常在selectPet()---------------");
}
finally {
DataBaseAccess.closeResultSet(rs);
DataBaseAccess.closeStatement(pst);
DataBaseAccess.closeConnection(con);
}
return flag;
}
public boolean updatePetInfo() {
Connection con = DataBaseAccess.getConnection();
PreparedStatement pst = null;
try {
pst = con.prepareStatement(
"UPDATE PetInfo SET PetName=?,PetTypeID=?, OwnerID=? WHERE ID=?");
pst.setString(1, this.getPetName());
pst.setInt(2, Integer.parseInt(this.getPetTypeID()));
pst.setString(3, this.getOwnerID());
pst.setString(4, this.getId());
if (pst.executeUpdate()>0) {
return true;
}
}
catch (SQLException ex) {
System.out.println(
"-------------出现异常在updatePetInfo()中---------------");
}
finally {
DataBaseAccess.closeStatement(pst);
DataBaseAccess.closeConnection(con);
}
return false;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -