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 void deleteComputer(int id) { String sql = "delete from computer where id=?"; try { queryRunner.update(sql, id); } catch (Exception e) { e.printStackTrace(); } } public List getAllComputer() throws SQLException { String sql = "select * from computer"; return queryRunner.query(sql,new BeanListHandler<>(Computer.class)); } }