Skip to content

Commit

Permalink
findById implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
LucianoSant006 committed Oct 27, 2024
1 parent d494764 commit c9cdef6
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 6 deletions.
1 change: 1 addition & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions src/app/Program.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 4,16 @@
import model.dao.SellerDao;
import model.entities.Seller;

import java.util.Date;

public class Program {

public static void main(String[] args) {

Seller seller = new Seller(21, "Bob", "[email protected]", new Date(), 3000.0, obj);

SellerDao sellerDao = DaoFactory.createSellerDao();
SellerDao sellerDao = DaoFactory.createSellerDao();
Seller seller = sellerDao.findById(3);
System.out.println(seller);

System.out.println(seller);

}
}
3 changes: 2 additions & 1 deletion src/model/dao/DaoFactory.java
Original file line number Diff line number Diff line change
@@ -1,10 1,11 @@
package model.dao;

import db.Db;
import model.impl.SellerDaoJDBC;

public class DaoFactory {

public static SellerDao createSellerDao(){
return new SellerDaoJDBC();
return new SellerDaoJDBC(Db.getConnection());
}
}
47 changes: 46 additions & 1 deletion src/model/impl/SellerDaoJDBC.java
Original file line number Diff line number Diff line change
@@ -1,12 1,25 @@
package model.impl;

import db.Db;
import db.DbException;
import model.dao.SellerDao;
import model.entities.Department;
import model.entities.Seller;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class SellerDaoJDBC implements SellerDao {

private Connection conn;

public SellerDaoJDBC(Connection conn){
this.conn = conn;
}

@Override
public void insert(Seller obj) {

Expand All @@ -24,9 37,41 @@ public void deleteById(Integer id) {

@Override
public Seller findById(Integer id) {
return null;
PreparedStatement st = null;
ResultSet rs = null;
try{
st = conn.prepareStatement(
"SELECT seller.*,department.Name as DepName "
"FROM seller INNER JOIN department "
"ON seller.DepartmentId = department.Id "
"WHERE seller.Id = ?");
st.setInt(1,id);
rs = st.executeQuery();
if (rs.next()){
Department dp = new Department();
dp.setId(rs.getInt("DepartmentId"));
dp.setName(rs.getString("DepName"));
Seller obj = new Seller();
obj.setId(rs.getInt("Id"));
obj.setName(rs.getString("Name"));
obj.setEmail(rs.getString("Email"));
obj.setBaseSalary(rs.getDouble("BaseSalary"));
obj.setBirthDate(rs.getDate("BirthDate"));
obj.setDepartment(dp);
return obj;
}
return null;
}catch(SQLException e){
throw new DbException(e.getMessage());
}
finally{
Db.closeStatement(st);
Db.closeResultSet(rs);
}

}


@Override
public List<Seller> findAll() {
return List.of();
Expand Down

0 comments on commit c9cdef6

Please sign in to comment.