You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
test/Supermarket/lib/dao/impl/SupplierInfImpl.java

266 lines
12 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package com.lingnan.supermarket.dao.impl;
import com.lingnan.supermarket.dao.SupplierInfService;
import com.lingnan.supermarket.dto.SupplierInf;
import com.lingnan.supermarket.utils.JDBCUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Vector;
/**
* 供应商信息服务实现类
* @cwf 2210461197 2024/12/1
*/
// 实现供应商信息服务接口的类
public class SupplierInfImpl implements SupplierInfService{
// 查询所有供应商信息的方法
@Override
// 该方法用于查找所有未被标记为删除的供应商信息,返回一个供应商信息的向量
// 此方法连接数据库,执行查询,并将结果封装为供应商信息对象的向量
public Vector<SupplierInf> findAllSupplierInf() {
Vector<SupplierInf> supplierInfs = new Vector<SupplierInf>(); // 创建一个向量用于存储供应商信息
Connection conn = JDBCUtil.getConn(); // 获取数据库连接
PreparedStatement preparedStatement = null; // 初始化预编译语句对象
ResultSet resultSet=null; // 初始化结果集对象
String SQL = "select * from supplierInf where delmark = 1"; // SQL查询语句选择未被标记为删除的供应商信息
try {
preparedStatement = conn.prepareStatement(SQL); // 准备SQL语句
resultSet = preparedStatement.executeQuery(); // 执行查询
while(resultSet.next()) { // 遍历结果集
SupplierInf supplierInf = new SupplierInf(); // 创建一个新的供应商信息对象
supplierInf.setId(resultSet.getInt("id")); // 设置供应商ID
supplierInf.setName(resultSet.getString("name")); // 设置供应商名称
supplierInf.setContact(resultSet.getString("contact")); // 设置供应商联系人
supplierInf.setEmail(resultSet.getString("email")); // 设置供应商邮箱
supplierInf.setAddress(resultSet.getString("address")); // 设置供应商地址
supplierInf.setDelmark(resultSet.getInt("delmark")); // 设置删除标记
supplierInfs.add(supplierInf); // 将供应商信息对象添加到向量中
}
} catch (SQLException e) { // 捕获SQL异常
e.printStackTrace(); // 打印异常堆栈信息
} finally { // 无论是否发生异常,都会执行的代码块
// 关闭数据库连接和相关资源
JDBCUtil.close(resultSet, preparedStatement, conn); // 关闭结果集、预编译语句和数据库连接
}
return supplierInfs; // 返回存储所有供应商信息的向量
}
// 根据供应商名称查询供应商信息的方法
@Override
// 定义一个查找供应商信息的方法,根据传入的供应商对象进行查询
/**
* 根据供应商名称查找供应商信息
* @param supplierInf 包含供应商名称的供应商信息对象
* @return 返回包含查询到的供应商信息的向量
*/
public Vector<SupplierInf> findByNameSupplierInf(SupplierInf supplierInf) {
//SupplierInf supplierInf = new SupplierInf();
Connection conn = JDBCUtil.getConn(); // 获取数据库连接
Vector<SupplierInf> v = new Vector<>(); // 创建一个向量用于存储供应商信息
System.out.println(supplierInf.getName()); // 打印供应商名称
PreparedStatement preparedStatement = null; // 初始化预编译语句对象
ResultSet resultSet = null; // 初始化结果集对象
try {
if(!supplierInf.getName().equals("")){ // 如果供应商名称不为空
String s='%'+supplierInf.getName()+'%'; // 构建模糊查询字符串
preparedStatement = conn.prepareStatement("select * from supplierInf where name like ? and delmark = 1"); // 准备SQL查询语句
preparedStatement.setString(1, s); // 设置查询参数
}else
preparedStatement = conn.prepareStatement("select * from supplierInf where delmark = 1"); // 准备SQL查询语句选择未被标记为删除的供应商信息
resultSet = preparedStatement.executeQuery(); // 执行查询
while (resultSet.next()) { // 遍历结果集
supplierInf = new SupplierInf(); // 创建一个新的供应商信息对象
supplierInf.setId(resultSet.getInt("id")); // 设置供应商ID
supplierInf.setName(resultSet.getString("name")); // 设置供应商名称
supplierInf.setAddress(resultSet.getString("address")); // 设置供应商地址
supplierInf.setContact(resultSet.getString("contact")); // 设置供应商联系人
supplierInf.setEmail(resultSet.getString("email")); // 设置供应商邮箱
supplierInf.setDelmark(1); // 设置删除标记
v.add(supplierInf); // 将供应商信息对象添加到向量中
}
Iterator<SupplierInf> it=v.iterator(); // 获取向量的迭代器
while(it.hasNext()){ // 遍历向量
supplierInf=it.next(); // 获取下一个供应商信息对象
System.out.println(supplierInf.getId()+" "+supplierInf.getName()+" "+supplierInf.getAddress()+" "+supplierInf.getContact()+" "+supplierInf.getEmail()+" "+supplierInf.getDelmark()+" "); // 打印供应商信息
}
} catch (SQLException e) { // 捕获SQL异常
e.printStackTrace(); // 打印异常堆栈信息
} finally { // 无论是否发生异常,都会执行的代码块
JDBCUtil.close(resultSet, preparedStatement, conn); // 关闭结果集、预编译语句和数据库连接
}
return v; // 返回存储供应商信息的向量
}
// 添加供应商信息的方法
/**
* 添加供应商信息
*
* @param supplierInf 供应商信息对象
* @return 插入结果标志1表示成功0表示失败
*/
@Override
public int addSupplierInf(SupplierInf supplierInf) {
int flag = 0; // 初始化标志为0
Connection conn = JDBCUtil.getConn(); // 获取数据库连接
PreparedStatement preparedStatement = null; // 初始化预编译语句对象
try {
preparedStatement = conn.prepareStatement
("insert into supplierInf values (null,?,?,?,?,?)"); // 准备SQL插入语句
//preparedStatement.setInt(1, supplierInf.getId());
preparedStatement.setString(1, supplierInf.getName()); // 设置供应商名称
preparedStatement.setString(2, supplierInf.getAddress()); // 设置供应商地址
preparedStatement.setString(3, supplierInf.getContact()); // 设置供应商联系人
preparedStatement.setString(4, supplierInf.getEmail()); // 设置供应商邮箱
preparedStatement.setInt(5, 1); // 设置删除标记
preparedStatement.executeUpdate(); // 执行插入操作
flag = 1; // 设置标志为1表示插入成功
} catch (SQLException e) { // 捕获SQL异常
e.printStackTrace(); // 打印异常堆栈信息
} finally { // 无论是否发生异常,都会执行的代码块
JDBCUtil.close(null,preparedStatement, conn); // 关闭预编译语句和数据库连接
}
return flag; // 返回插入结果标志
}
// 删除供应商信息的方法
/**
* 删除指定ID的供应商信息
*
* @param id 要删除的供应商ID
* @return 删除操作的结果标志1表示成功0表示失败
*/
@Override
public int deleteSupplierInf(int id) {
int flag = 0; // 初始化标志为0
Connection conn = JDBCUtil.getConn(); // 获取数据库连接
PreparedStatement preparedStatement = null; // 初始化预编译语句对象
try {
preparedStatement = conn.prepareStatement
("delete from supplierInf where id = ?"); // 准备SQL删除语句
preparedStatement.setInt(1, id); // 设置供应商ID
preparedStatement.executeUpdate(); // 执行删除操作
flag = 1; // 设置标志为1表示删除成功
} catch (SQLException e) { // 捕获SQL异常
e.printStackTrace(); // 打印异常堆栈信息
} finally { // 无论是否发生异常,都会执行的代码块
JDBCUtil.close(null,preparedStatement, conn); // 关闭预编译语句和数据库连接
}
return flag; // 返回删除结果标志
}
// 更新供应商信息的方法
/**
* 更新供应商信息
*
* @param supplierInf 供应商信息对象
* @return 更新结果标志1表示成功0表示失败
*/
@Override
public int updateSupplierInf(SupplierInf supplierInf) {
int flag=0; // 初始化标志为0
Connection conn = JDBCUtil.getConn(); // 获取数据库连接
PreparedStatement preparedStatement = null; // 初始化预编译语句对象
try {
preparedStatement = conn.prepareStatement("update supplierInf set name=?,address=?,contact=?,email=?,delmark=? where id = ?"); // 准备SQL更新语句
preparedStatement.setString(1,supplierInf.getName()); // 设置供应商名称
preparedStatement.setString(2,supplierInf.getAddress()); // 设置供应商地址
preparedStatement.setString(3,supplierInf.getContact()); // 设置供应商联系人
preparedStatement.setString(4, supplierInf.getEmail()); // 设置供应商邮箱
preparedStatement.setInt(5, supplierInf.getDelmark()); // 设置删除标记
preparedStatement.setInt(6,supplierInf.getId()); // 设置供应商ID
preparedStatement.executeUpdate(); // 执行更新操作
flag = 1; // 设置标志为1表示更新成功
} catch (SQLException e) { // 捕获SQL异常
e.printStackTrace(); // 打印异常堆栈信息
} finally { // 无论是否发生异常,都会执行的代码块
JDBCUtil.close(null, preparedStatement, conn); // 关闭预编译语句和数据库连接
}
return flag; // 返回更新结果标志
}
// 获取所有供应商名称的方法
// 该方法从数据库中查询所有供应商的名称,并将其存储在一个列表中返回
@Override
public ArrayList<String> findNameSupplier() {
Connection conn=JDBCUtil.getConn(); // 获取数据库连接
ArrayList<String> v = new ArrayList<String>(); // 创建一个列表用于存储供应商名称
SupplierInf supplierInf; // 声明一个供应商信息对象
PreparedStatement pstmt = null; // 初始化预编译语句对象
ResultSet resultSet=null; // 初始化结果集对象
v.add("全部"); // 添加默认选项“全部”
try {
pstmt = conn.prepareStatement("select * from supplierInf"); // 准备SQL查询语句
resultSet = pstmt.executeQuery(); // 执行查询
while(resultSet.next()){ // 遍历结果集
v.add(resultSet.getString("name")); // 将供应商名称添加到列表中
}
} catch (SQLException e) { // 捕获SQL异常
// TODO Auto-generated catch block
e.printStackTrace(); // 打印异常堆栈信息
}finally{ // 无论是否发生异常,都会执行的代码块
JDBCUtil.close(resultSet, pstmt, conn); // 关闭结果集、预编译语句和数据库连接
}
return v; // 返回存储供应商名称的列表
}
// 根据供应商名称查找其ID的方法
/**
* 根据给定的供应商名称查找对应的供应商ID。
* 如果名称为“全部”返回默认ID0
* 如果未找到对应的供应商,返回-1作为标志。
*
* @param name 供应商名称
* @return 供应商ID或标志
*/
@Override
public int findIdSupplierByName(String name) {
int flag = -1; // 初始化标志为-1
Connection conn = JDBCUtil.getConn(); // 获取数据库连接
PreparedStatement preparedStatement = null; // 初始化预编译语句对象
ResultSet resultSet= null; // 初始化结果集对象
int id=0; // 初始化供应商ID为0
try {
if(name.equals("全部")) // 如果供应商名称为“全部”
return id; // 返回默认ID
preparedStatement = conn.prepareStatement
("select * from supplierInf where name=?"); // 准备SQL查询语句
preparedStatement.setString(1, name); // 设置查询参数
resultSet=preparedStatement.executeQuery(); // 执行查询
if(resultSet.next()){ // 如果找到对应记录
return resultSet.getInt("id"); // 返回供应商ID
}
} catch (SQLException e) { // 捕获SQL异常
e.printStackTrace(); // 打印异常堆栈信息
} finally { // 无论是否发生异常,都会执行的代码块
JDBCUtil.close(null,preparedStatement, conn); // 关闭预编译语句和数据库连接
}
return flag; // 返回查询结果标志
}
}