📄 pettypebean.java
字号:
package Model_PetType;
import DataBase.ConDataBase;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Collection;
public class PetTypeBean
{
public PetTypeBean()
{
}
public boolean InsertPetType(PetType pt)
{
Connection con=ConDataBase.getConnection();
try{
CallableStatement csmt=con.prepareCall("{call INSERTPETTYPE(?,?,?)}");
csmt.setString(1,pt.getPetTypeId());
csmt.setString(2,pt.getPetTypeName());
csmt.registerOutParameter(3,Types.VARCHAR);
csmt.execute();
String re=csmt.getString(3);
csmt.close();
con.close();
if (re.equals("添加成功"))
{
return true;
}
else
{
return false;
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
return false;
}
}
public Collection SelectPetType()
{
Connection con=ConDataBase.getConnection();
ArrayList al=new ArrayList();
try{
Statement smt=con.createStatement();
ResultSet rs=smt.executeQuery("select * from PetType");
while (rs.next())
{
PetType pt=new PetType();
pt.setPetTypeId(rs.getString(1));
pt.setPetTypeName(rs.getString(2));
al.add(pt);
}
smt.close();
con.close();
return al;
}
catch(Exception e)
{
System.out.println(e.getMessage());
return null;
}
}
public PetType FindPetType(String pettypeid)
{
Connection con=ConDataBase.getConnection();
PetType pt=new PetType();
try{
Statement smt=con.createStatement();
ResultSet rs=smt.executeQuery("select * from PetType where TypeId='"+ pettypeid +"'");
while (rs.next())
{
pt.setPetTypeId(rs.getString(1));
pt.setPetTypeName(rs.getString(2));
}
smt.close();
con.close();
return pt;
}
catch(Exception e)
{
System.out.println(e.getMessage());
return null;
}
}
public String UpdatePetType(PetType pt)
{
Connection con=ConDataBase.getConnection();
try{
CallableStatement csmt=con.prepareCall("{call UPDATEPETTYPE(?,?,?)}");
csmt.setString(1,pt.getPetTypeId());
csmt.setString(2,pt.getPetTypeName());
csmt.registerOutParameter(3,Types.VARCHAR);
csmt.execute();
String re=csmt.getString(3);
csmt.close();
con.close();
return re;
}
catch(Exception e)
{
System.out.println(e.getMessage());
return e.getMessage();
}
}
public String DeletePetType(String pettypeid)
{
Connection con=ConDataBase.getConnection();
try{
CallableStatement csmt=con.prepareCall("{call DELETEPETTYPE(?,?)}");
csmt.setString(1,pettypeid);
csmt.registerOutParameter(2,Types.VARCHAR);
csmt.execute();
String re=csmt.getString(2);
csmt.close();
con.close();
return re;
}
catch(Exception e)
{
System.out.println(e.getMessage());
return e.getMessage();
}
}
public static void main(String[] args)
{
/*PetType pt=new PetType("4","猫4"); //测试增加宠物类别
PetTypeBean ptb=new PetTypeBean();
String s=ptb.InsertPetType(pt);
System.out.println(s); //结束
/*PetTypeBean ptb=new PetTypeBean(); //测试查询所有宠物类别
ArrayList al=new ArrayList();
al.addAll(ptb.SelectPetType());
int i=al.size();
System.out.println(i); //结束
*/
/*PetTypeBean ptb=new PetTypeBean(); //测试查询指定宠物编号
PetType pt=ptb.FindPetType("1");
String s=pt.getPetTypeName();
System.out.println(s); //结束
*/
/*PetTypeBean ptb=new PetTypeBean(); //测试修改宠物类别
PetType pt=new PetType("1","猫");
String s=ptb.UpdatePetType(pt);
System.out.println(s); //结束
*/
/*PetTypeBean ptb=new PetTypeBean(); //测试删除宠物类别
String s=ptb.DeletePetType("3");
System.out.println(s); //结束
*/
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -