DAO层
package cn.happy.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class BaseDao {
Connection con = null;
PreparedStatement ps = null;
public ResultSet rs=null;
private static final String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static final String url = "jdbc:sqlserver://localhost:1433;DatabaseName=PetShop";
private static final String username = "sa";
private static final String pwd = "1";
public Connection getCon()throws Exception{
Class.forName(driver);
if(con==null||con.isClosed()){
con = DriverManager.getConnection(url, username, pwd);
}
return con;
}
//执行查询
public ResultSet executeQuery(String sql,Object... obje) throws Exception{
con=getCon();
ps=con.prepareStatement(sql);
for(int i=0;i<obje.length;i++){
ps.setObject(i+1, obje[i]);
}
rs=ps.executeQuery();
return rs;
}
//测试修改
//执行修改
public int execteUpdate(String sql,Object...object) throws Exception{
con=getCon();
ps=con.prepareStatement(sql);
for(int i=0;i<object.length;i++){
ps.setObject(i+1, object[i]);
}
int count=ps.executeUpdate();
return count;
}
//单测增加
//执行增加
public int execteadd(String sql,Object...object) throws Exception{
con=getCon();
ps=con.prepareStatement(sql);
ps.executeUpdate();
for(int i=0;i<object.length;i++){
ps.setObject(i+1, object[i]);
}
int count=ps.executeUpdate();
return count;
}
//执行删除
//执行删除
public int exectedelete(String sql,Object...object) throws Exception{
con=getCon();
ps=con.prepareStatement(sql);
ps.executeUpdate();
for(int i=0;i<object.length;i++){
ps.setObject(i+1, object[i]);
}
int count=ps.executeUpdate();
return count;
}
public void closeAll() throws Exception{
if(rs!=null){
rs.close();
}
if(ps!=null){
ps.close();
}
if(con!=null){
con.close();
}
}
//
}
package cn.happy.dao;
import java.util.List;
import cn.happy.entity.Pet;
import cn.happy.entity.PetOwner;
public interface Petdao {
public List<Pet> select1() throws Exception;
public List<Pet> se1() throws Exception;
public boolean up(int id) throws Exception;
public List<Pet> poselect(int id) throws Exception;
public List<Pet> selectquan() throws Exception;
Pet getPetById(int id) throws Exception;
int buyPet(int petid,int ownerid) throws Exception;
int sellPet(int storeid,int petid) throws Exception;
public List<Pet> getOwnerPet(int ownerid) throws Exception ;
}
package cn.happy.dao;
import java.util.List;
import cn.happy.entity.Pet;
import cn.happy.entity.PetOwner;
public interface PetOwnerdao {
//显示宠物主人名称
public List<PetOwner> select2() throws Exception;
public PetOwner DL(String name1) throws Exception;
public boolean update1(int money,int id) throws Exception;
public List<PetOwner> getnamebyid()throws Exception;
public int getIdByName(String name) throws Exception ;
public int updateByName(int money,String name) throws Exception ;
public int updateMoneyByName(int money, String name) throws Exception ;
}
package cn.happy.dao;
import java.util.List;
import cn.happy.entity.PetStore;
public interface PetStoredao {
public List<PetStore> select3() throws Exception;
public PetStore DL(String name1) throws Exception;
}
package cn.happy.dao;
public interface Accountdao {
public boolean add(int id)throws Exception;
int addAccount(int dealtype,int petid,int ownerid) throws Exception;
int addSellAccount(int dealtype,int petid,int ownerid,int storeid) throws Exception;
}
DAO Impl
package cn.happy.Impl;
import cn.happy.dao.Accountdao;
import cn.happy.dao.BaseDao;
public class AccountdaoImpl extends BaseDao implements Accountdao {
@Override
//添加一条台账信息
//宠物主人登录购买的宠物
public boolean add(int id) throws Exception {
boolean flag=false;
String sql="insert into Account values(?,?,?)";
int count=this.execteU