节选自北大青鸟S2项目-宠物商店
希望这个可以帮助到大家,先感谢大家的阅读和点赞。
DAO层
Account
package cn.bdqn.dao;
import java.util.List;
import cn.bdqn.entity.Account;
public interface AccountDao {
public abstract int updateAccount(String sql,Object[] param);
public abstract List<Account> getPetStoreAccount(String sql,Object[] param);
}
BaseDao
package cn.bdqn.dao;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
public class BaseDao {
public static String driver;
public static String url;
public static String user;
public static String password;
Connection conn = null;
static {
init();
}
public static void init() {
Properties param = new Properties();
String configFile = "database.properties";
InputStream is = BaseDao.class.getClassLoader().getResourceAsStream(configFile);
try {
param.load(is);
} catch (IOException e) {
e.printStackTrace();
}
url = param.getProperty("url");
user = param.getProperty("user");
password = param.getProperty("password");
}
public Connection getConn() {
Connection conn = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(url,user,password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public void closeAll(Connection conn,PreparedStatement pstmt,ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
* sql语句
*/
public int executeSQL(String preparedSql,Object[] param) {
Connection conn = null;
PreparedStatement pstmt = null;
int num = 0;
try {
conn = getConn();
pstmt = conn.prepareStatement(preparedSql);
if (param != null) {
for (int i = 0; i < param.length; i++) {
pstmt.setObject(i+1, param[i]);
}
}
num = pstmt.executeUpdate();
}catch (ClassCastException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally {
this.closeAll(conn, pstmt, null);
}
return num;
}
}
PetDao
package cn.bdqn.dao;
import java.util.List;
import cn.bdqn.entity.Pet;
public interface PetDao {
public abstract List<Pet> getAllPet();
public abstract List<Pet> selectPet(String sql,String[] param);
public abstract int updatePet(String sql,Object[] param);
}
PetOwnerDao
package cn.bdqn.dao;
import java.util.List;
import cn.bdqn.entity.PetOwner;
public interface PetOwnerDao {
public abstract List<PetOwner> getAllOwner();
public abstract int updateOwner(String sql,String[] param);
public abstract PetOwner selectOwner(String sql,String[] param);
}
PetStoreDao
package cn.bdqn.dao;
import java.util.List;
import cn.bdqn.entity.PetStore;
public interface PetStoreDao {
public abstract List<PetStore> getAllStore();
public abstract PetStore getPetStore(String sql,String[] param);
public abstract int updateStore(String sql,Object[] param);
}
DaoImpl实现
AccountDaoImpl
package cn.bdqn.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import cn.bdqn.dao.AccountDao;
import cn.bdqn.dao.BaseDao;
import cn.bdqn.entity.Account;
public class AccountDaoImpl extends BaseDao implements AccountDao{
private Connection conn = null;
private PreparedStatement pstmt = null;
private ResultSet rs = null;
private List<Account> accountList;
@Override
public int updateAccount(String sql, Object[] param) {
int count = super.executeSQL(sql, param);
return count;
}
@Override
public List<Account> getPetStoreAccount(String sql, Object[] param) {
List<Account> accounts = new ArrayList<Account>();
try {
conn = getConn();
pstmt = conn.prepareStatement(sql);
if (param != null) {
for (int i = 0; i < param.length; i++) {
pstmt.setString(i+1, (String) param[i]);
}
}
rs = pstmt.executeQuery();
Account account = null;
while (rs.next()) {
account = new Account();
account.setId(rs.getInt(1));
account.setDealType(rs.getInt(2));
account.setPetId(rs.getInt(3));
account.setSellerId(rs.getInt(4));
account.setBuyerId(rs.getInt(5));
account.setPrice(rs.getDouble(6));
account.setDealTime(rs.getDate(7));
accountList.add(account);
}
} catch (SQLException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}finally {
super.closeAll(conn, pstmt, rs);
}
return accountList;
}
}
PetDaoImpl
package cn.bdqn.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import cn.bdqn.dao.BaseDao;
import cn.bdqn.dao.PetDao;
import cn.bdqn.entity.Pet;
/**
* 宠物数据库操作类
*/
public class PetDaoImpl extends BaseDao implements PetDao {
private Connection conn = null; // 保存数据库连接
private PreparedStatement pstmt = null; // 用于执行SQL语句
private ResultSet rs = null; // 用户保存查询结果集
/* (non-Javadoc)
* @see cn.jbit.epetShop.dao.impl.PetDao#getAllPet()
*/
public List<Pet> getAllPet() {
List<Pet> petList = new ArrayList<Pet>();
try {
String preparedSql = "select id,name,typeName,health,love,birthday,owner_id,store_id from pet ";
conn = getConn(); // 得到数据库连接
pstmt = conn.prepareStatement(preparedSql); // 得到PreparedStatement对象
rs = pstmt.executeQuery(); // 执行SQL语句
while (rs.next()) {
Pet pet = new Pet();
pet.setId(rs.getInt(1));
pet.setName(rs.getString(2));
pet.setTypeName(rs.getString(3));
pet.setHealth(rs.getInt(4));
pet.setLove(rs.getInt(5));
pet.setBirthday(rs.getDate(6));
pet.setOwnerId(rs.getInt(7));
pet.setStoreId(rs.getInt(8));
petList.add(pet);
}
} catch (SQLException e) {
e.printStackTrace();
}catch (ClassCastException e) {
// TODO: handle exception
}
finally {
super.closeAll(conn, pstmt, rs);
}
return petList;
}
/* (non-Javadoc)
* @see cn.jbit.epetShop.dao.impl.PetDao#selectPet(java.lang.String, java.lang.String[])
*/
public List<Pet> selectPet(String sql, String[] param) {
List<Pet> petList = new ArrayList<Pet>();
try {
conn = getConn(); // 得到数据库连接
pstmt = conn.prepareStatement(sql); // 得到PreparedStatement对象
if (param != null) {
for (int i = 0; i < param.length; i++) {
pstmt.setString(i + 1, param[i]); // 为预编译sql设置参数
}
}
rs = pstmt.executeQuery(); // 执行SQL语句
while (rs.next()) {
Pet pet = new Pet();
pet.setId(rs.getInt(1));
pet.setName(rs.getString(2));
pet.setTypeName(rs.getString(3));
pet.setHealth(rs.getInt(4));
pet.setLove(rs.getInt(5));
pet.setBirthday(rs.getDate(6));
pet.setOwnerId(rs.getInt(7));
pet.setStoreId(rs.getInt(8));
petList.add(pet);
}
} catch (SQLException e) {
e.printStackTrace();
}catch (ClassCastException e) {
e.printStackTrace();
} finally {
super.closeAll(conn, pstmt, rs);
}
return petList;
}
/*
* (non-Javadoc)
*
* @see cn.jbit.epetShop.dao.impl.PetDao#updatePet(java.lang.String,
* java.lang.Object[])
*/
public int updatePet(String sql, Object[] param) {
int count = super.executeSQL(sql, param);
return count;
}
}
PetOwnerDaoImpl
package cn.bdqn.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import cn.bdqn.dao.BaseDao;
import cn.bdqn.dao.PetOwnerDao;
import cn.bdqn.entity.PetOwner;
/**
*宠物主人数据库操作类
*/
public class PetOwnerDaoImpl extends BaseDao implements PetOwnerDao {
private Connection conn = null; // 保存数据库连接
private PreparedStatement pstmt = null; // 用于执行SQL语句
private ResultSet rs = null; // 用户保存查询结果集
/* (non-Javadoc)
* @see cn.jbit.epetShop.dao.impl.PetOwnerDao#getAllOwner()
*/
public List<PetOwner> getAllOwner() {
List<PetOwner> ownerList = new ArrayList<PetOwner>();
try {
String preparedSql = "select * from petowner ";
conn = getConn(); // 得到数据库连接
pstmt = conn.prepareStatement(preparedSql); // 得到PreparedStatement对象
rs = pstmt.executeQuery(); // 执行SQL语句
while (rs.next()) {
PetOwner petOwner = new PetOwner();
petOwner.setId(rs.getInt(1));
petOwner.setName(rs.getString(2));
petOwner.setPassword(rs.getString(3));
petOwner.setMoney(rs.getDouble(4));
ownerList.add(petOwner);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
super.closeAll(conn, pstmt, rs);
}
return ownerList;
}
/* (non-Javadoc)
* @see cn.jbit.epetShop.dao.impl.PetOwnerDao#updateOwner(java.lang.String, java.lang.String[])
*/
public int updateOwner(String sql, String[] param) {
int count = super.executeSQL(sql, param);
return count;
}
/* (non-Javadoc)
* @see cn.jbit.epetShop.dao.impl.PetOwnerDao#selectOwner(java.lang.String, java.lang.String[])
*/
public PetOwner selectOwner(String sql, String[] param) {
PetOwner owner = null;
try {
conn = getConn(); // 得到数据库连接
pstmt = conn.prepareStatement(sql); // 得到PreparedStatement对象
if (param != null) {
for (int i = 0; i < param.length; i++) {
pstmt.setString(i + 1, param[i]); // 为预编译sql设置参数
}
}
rs = pstmt.executeQuery(); // 执行SQL语句
while (rs.next()) {
owner = new PetOwner();
owner.setId(rs.getInt(1));
owner.setName(rs.getString(2));
owner.setPassword(rs.getString(3));
owner.setMoney(rs.getDouble(4));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
super.closeAll(conn, pstmt, rs);
}
return owner;
}
}
PetStoreDaoImpl
package cn.bdqn.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import