Compare commits
1 Commits
fa98c66334
...
fu
| Author | SHA1 | Date | |
|---|---|---|---|
| c3a91ecca5 |
158
src/com/rhizome/example/dao/DepartmentDAO.java
Normal file
158
src/com/rhizome/example/dao/DepartmentDAO.java
Normal file
@@ -0,0 +1,158 @@
|
||||
package src.com.rhizome.example.dao;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import src.com.rhizome.example.dto.DepartmentDTO;
|
||||
import src.com.rhizome.example.dto.EmployeeDTO;
|
||||
import src.com.rhizome.example.util.DbUtil;
|
||||
|
||||
public class DepartmentDAO {
|
||||
private static final String TBL_NAME = "department";
|
||||
private static final String ID_EMPLOYEE = "id_department";
|
||||
private static final String NM_EMPLOYEE = "nm_department";
|
||||
|
||||
|
||||
/** DBコネクション */
|
||||
public Connection con;
|
||||
/** DBステートメント */
|
||||
PreparedStatement stmt;
|
||||
/** 検索結果 */
|
||||
ResultSet rs;
|
||||
|
||||
/**
|
||||
* インスタンス生成時DBコネクション
|
||||
*
|
||||
* @throws ClassNotFoundException
|
||||
* @throws SQLException
|
||||
*/
|
||||
public DepartmentDAO(Connection con) {
|
||||
this.con = con;
|
||||
}
|
||||
|
||||
/**
|
||||
* 社員テーブルの情報を全件取得する
|
||||
*
|
||||
* @return 検索結果
|
||||
* @throws SQLException
|
||||
* @throws ClassNotFoundException
|
||||
*/
|
||||
public List<DepartmentDTO> selectAll() throws SQLException, ClassNotFoundException {
|
||||
List<EmployeeDTO> rtnList = new ArrayList<>();
|
||||
StringBuilder sql = new StringBuilder();
|
||||
sql.append(" SELECT ");
|
||||
sql.append(" " + "*");
|
||||
sql.append(" FROM ");
|
||||
sql.append(" " + TBL_NAME);
|
||||
try {
|
||||
|
||||
this.stmt = con.prepareStatement(sql.toString());
|
||||
rs = stmt.executeQuery();
|
||||
while (rs.next()) {
|
||||
EmployeeDTO dto =
|
||||
new EmployeeDTO();
|
||||
|
||||
dto.setIdDepartment(
|
||||
rs.getInt("ID_DEPARTMENT"));
|
||||
|
||||
dto.setNmDepartment(
|
||||
rs.getString("NM_DEPARTMENT"));
|
||||
|
||||
|
||||
|
||||
rtnList.add(dto);
|
||||
}
|
||||
} finally {
|
||||
DbUtil.closeStatement(this.stmt);
|
||||
}
|
||||
return rtnList;
|
||||
}
|
||||
public List<DepartmentDTO> selectById(int departmentId) throws SQLException{
|
||||
List<DepartmentDTO>rtnList = new ArrayList<>();
|
||||
|
||||
java.lang.String sql =
|
||||
"SELECT * FROM department WHERE id_department = ?";
|
||||
try {
|
||||
stmt = con.prepareStatement(sql);
|
||||
stmt.setInt(1, departmentId);
|
||||
|
||||
rs = stmt.executeQuery();
|
||||
|
||||
while (rs.next()) {
|
||||
|
||||
DepartmentDTO dto = new DepartmentDTO();
|
||||
|
||||
dto.setIdDepartment(rs.getInt(ID_DEPARTMENT));
|
||||
dto.setNmDepartment(rs.getString(NM_DEPARTMENT));
|
||||
|
||||
|
||||
rtnList.add(dto);
|
||||
}
|
||||
|
||||
} finally {
|
||||
DbUtil.closeStatement(stmt);
|
||||
}
|
||||
|
||||
return rtnList;
|
||||
}
|
||||
public int depDelete (int departmentId)throws SQLException{
|
||||
java.lang.String sql =
|
||||
"DELETE FROM department WHERE id_department = ?";
|
||||
try {
|
||||
stmt = con.prepareStatement(sql);
|
||||
stmt.setInt(1, departmentId);
|
||||
|
||||
return stmt.executeUpdate();
|
||||
} finally {
|
||||
DbUtil.closeStatement(stmt);
|
||||
|
||||
}
|
||||
}
|
||||
public int depInsert(String departmentName,String departmentKn)throws SQLException{
|
||||
java.lang.String sql =
|
||||
"INSERT INTO employee " +
|
||||
"(nm_department) " +
|
||||
"VALUES (?, ?, ?, ?, ?)";
|
||||
try {
|
||||
stmt = con.prepareStatement(sql);
|
||||
stmt.setString(1, departmentName);
|
||||
stmt.setString(2, departmentKn);
|
||||
|
||||
|
||||
return stmt.executeUpdate();
|
||||
|
||||
} finally {
|
||||
DbUtil.closeStatement(stmt);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public int empUpdate(int departmentId,String departmentName)throws SQLException{
|
||||
java.lang.String sql =
|
||||
"UPDATE department " +
|
||||
"SET nm_department = ?, " +
|
||||
|
||||
"WHERE id_employee = ?";
|
||||
|
||||
try {
|
||||
|
||||
stmt = con.prepareStatement(sql);
|
||||
|
||||
stmt.setString(1, departmentName);
|
||||
|
||||
stmt.setInt(5, departmentId);
|
||||
|
||||
return stmt.executeUpdate();
|
||||
|
||||
} finally {
|
||||
DbUtil.closeStatement(stmt);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
26
src/com/rhizome/example/dto/DepartmentDTO.java
Normal file
26
src/com/rhizome/example/dto/DepartmentDTO.java
Normal file
@@ -0,0 +1,26 @@
|
||||
package src.com.rhizome.example.dto;
|
||||
|
||||
|
||||
public class DepartmentDTO {
|
||||
private int idDepartment;
|
||||
private String nmDepartment;
|
||||
|
||||
|
||||
public int getIdDepartment() {
|
||||
return idDepartment;
|
||||
}
|
||||
public void setIdDepartment(int idDepartment) {
|
||||
this.idDepartment = idDepartment;
|
||||
}
|
||||
public String getNmDepartment() {
|
||||
return nmDepartment;
|
||||
}
|
||||
public void setNmDepartment(String Department) {
|
||||
this.nmDepartment= nmDepartment;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user