This repository has been archived on 2025-01-14. You can view files and clone it, but cannot push or open issues/pull-requests.
computer-web/src/main/java/example/dao/ComputerDao.java

56 lines
1.5 KiB
Java
Raw Normal View History

2024-12-21 01:22:44 +08:00
package example.dao;
import example.model.Computer;
import example.utils.DBUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import java.sql.SQLException;
import java.util.List;
public class ComputerDao {
private final QueryRunner queryRunner = new QueryRunner(DBUtils.getDataSource());
public int addComputer(String name, Double price, int stock) {
String sql = "insert into computer(name, price, stock) values(?, ?, ?)";
try {
return queryRunner.update(sql, name, price, stock);
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
public int updateComputer(String name, Double price, int stock, int id) {
String sql = "update computer set name=?, price=?, stock=? where id=?";
try {
return queryRunner.update(sql, name, price, stock, id);
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
public int deleteComputer(int id) {
String sql = "delete from computer where id=?";
try {
return queryRunner.update(sql, id);
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
public List<Computer> getAllComputer() throws SQLException {
String sql = "select * from computer";
return queryRunner.query(sql,new BeanListHandler<>(Computer.class));
}
}