Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package fbcn.model.entity.Company;
- import fbcn.Values;
- import fbcn.model.DBConnection;
- import fbcn.model.OrderedList;
- import fbcn.model.entity.User.User;
- import java.sql.ResultSet;
- public class CompanyDAO implements iCompanyDAO{
- private DBConnection db;
- private ResultSet result;
- public CompanyDAO() throws Exception{
- db = new DBConnection();
- }
- @Override
- public OrderedList<Company> findAll() throws Exception {
- db.prepare(Values.sqlCompanyFindAll);
- result = db.pstmt.executeQuery();
- return dump(result);
- }
- @Override
- public OrderedList<Company> findById(int id) throws Exception {
- db.prepare(Values.sqlCompanyFindById);
- db.pstmt.setInt(1, id);
- result = db.pstmt.executeQuery();
- return dump(result);
- }
- @Override
- public OrderedList<Company> findByName(String name ) throws Exception{
- db.prepare(Values.sqlCompanyFindByName);
- db.pstmt.setString(1,name);
- result = db.pstmt.executeQuery();
- return dump(result);
- }
- @Override
- public int findIdByName(String name) throws Exception{
- db.prepare(Values.sqlCompanyGetNameById);
- db.pstmt.setString(1,name);
- result = db.pstmt.executeQuery();
- if (result.next()){
- return result.getInt(1);
- }
- else return 0;
- }
- @Override
- public void updateAllByDeleteId(int id) throws Exception {
- db.prepare(Values.sqlCompanyUpdateAllById);
- db.pstmt.setInt(1,id);
- db.pstmt.executeUpdate();
- db.pstmtClose();
- }
- @Override
- public void insert(Company company) throws Exception {
- db.prepare(Values.sqlCompanyInsert);
- db.pstmt.setString(1, company.getName());
- db.pstmt.setString(2, company.getType());
- db.pstmt.setFloat(3, company.getCapital());
- db.pstmt.executeUpdate();
- db.pstmtClose();
- }
- @Override
- public void update(Company company) throws Exception {
- db.prepare(Values.sqlCompanyUpdate);
- db.pstmt.setString(1, company.getName());
- db.pstmt.setString(2, company.getType());
- db.pstmt.setFloat(3, company.getCapital());
- db.pstmt.setInt(4, company.getId());
- db.pstmt.executeUpdate();
- db.pstmtClose();
- }
- @Override
- public void delete(Company company) throws Exception {
- db.prepare(Values.sqlCompanyDelete);
- db.pstmt.setInt(1, company.getId());
- db.pstmt.executeUpdate();
- db.pstmtClose();
- }
- @Override
- public OrderedList<Company> dump(ResultSet result) throws Exception {
- OrderedList<Company> list = new OrderedList<Company>();
- while(result.next()){
- Company company;
- company = new Company(
- result.getInt("id"),
- result.getString("name"),
- result.getString("type"),
- result.getFloat("capital")
- );
- list.add(company);
- }
- db.pstmtClose();
- return list;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement