外卖平台JDBC项目中BusinessDAO层编写示例
在外卖平台JDBC项目中,BusinessDAO层主要负责与数据库进行交互,完成商家相关的数据操作。以下是BusinessDAO层的代码示例:
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class BusinessDAO {
// 查询所有商家
public List<Business> getAllBusiness() {
List<Business> businesses = new ArrayList<>();
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
conn = DBUtil.getConnection();
String sql = 'select * from business';
stmt = conn.prepareStatement(sql);
rs = stmt.executeQuery();
while (rs.next()) {
Business business = new Business();
business.setId(rs.getInt('id'));
business.setName(rs.getString('name'));
business.setAddress(rs.getString('address'));
business.setPhone(rs.getString('phone'));
business.setDeliveryFee(rs.getDouble('delivery_fee'));
businesses.add(business);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, stmt, rs);
}
return businesses;
}
// 根据商家id查询商家
public Business getBusinessById(int id) {
Business business = null;
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
conn = DBUtil.getConnection();
String sql = 'select * from business where id = ?';
stmt = conn.prepareStatement(sql);
stmt.setInt(1, id);
rs = stmt.executeQuery();
if (rs.next()) {
business = new Business();
business.setId(rs.getInt('id'));
business.setName(rs.getString('name'));
business.setAddress(rs.getString('address'));
business.setPhone(rs.getString('phone'));
business.setDeliveryFee(rs.getDouble('delivery_fee'));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, stmt, rs);
}
return business;
}
// 添加商家
public boolean addBusiness(Business business) {
boolean flag = false;
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = DBUtil.getConnection();
String sql = 'insert into business(name, address, phone, delivery_fee) values(?, ?, ?, ?)';
stmt = conn.prepareStatement(sql);
stmt.setString(1, business.getName());
stmt.setString(2, business.getAddress());
stmt.setString(3, business.getPhone());
stmt.setDouble(4, business.getDeliveryFee());
int rows = stmt.executeUpdate();
if (rows > 0) {
flag = true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, stmt);
}
return flag;
}
// 更新商家信息
public boolean updateBusiness(Business business) {
boolean flag = false;
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = DBUtil.getConnection();
String sql = 'update business set name=?, address=?, phone=?, delivery_fee=? where id=?';
stmt = conn.prepareStatement(sql);
stmt.setString(1, business.getName());
stmt.setString(2, business.getAddress());
stmt.setString(3, business.getPhone());
stmt.setDouble(4, business.getDeliveryFee());
stmt.setInt(5, business.getId());
int rows = stmt.executeUpdate();
if (rows > 0) {
flag = true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, stmt);
}
return flag;
}
// 删除商家
public boolean deleteBusiness(int id) {
boolean flag = false;
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = DBUtil.getConnection();
String sql = 'delete from business where id=?';
stmt = conn.prepareStatement(sql);
stmt.setInt(1, id);
int rows = stmt.executeUpdate();
if (rows > 0) {
flag = true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, stmt);
}
return flag;
}
}
以上代码中,getAllBusiness()方法用于查询所有商家信息,并返回一个Business对象列表。getBusinessById()方法根据商家id查询商家信息。addBusiness()方法用于添加商家信息到数据库中。updateBusiness()方法用于更新商家信息。deleteBusiness()方法用于删除商家信息。所有方法都使用了DBUtil类中提供的getConnection()方法获取数据库连接,并在使用完毕后调用close()方法关闭数据库连接。
原文地址: https://www.cveoy.top/t/topic/nGoS 著作权归作者所有。请勿转载和采集!