package edu.kh.jdbc.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import edu.kh.jdbc.dto.Employee1;
public class SelectDepartmentTitleDAO {
public List<Employee1> select(String departmentTitle) {
List<Employee1> empList = new ArrayList<>();
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
String type = "jdbc:oracle:thin:@";
String ip = "115.90.212.22";
String port = ":9000";
String dbName = ":XE";
String user = "kh_kkw";
String pw = "oracle_kkw123A";
conn = DriverManager.getConnection(type+ip+port+dbName, user, pw);
String condition = null;
if(departmentTitle.toLowerCase().equals("null")) {
condition = "WHERE DEPT_TITLE IS NULL ";
}else {
condition = "WHERE DEPT_TITLE = '" + departmentTitle + "'";
}
String sql = "SELECT EMP_ID, EMP_NAME, SALARY, DEPT_TITLE "
+ "FROM EMPLOYEE "
+ "LEFT JOIN DEPARTMENT ON(DEPT_CODE = DEPT_ID) "
+ condition
+ " ORDER BY EMP_ID";
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
while(rs.next()) {
String empId = rs.getString(1);
String empName = rs.getString(2);
int salary = rs.getInt(3);
String title = rs.getString(4);
Employee1 emp = new Employee1(empId, empName, salary, title);
empList.add(emp);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if( rs !=null) rs.close();
if( stmt !=null) rs.close();
if( conn !=null) rs.close();
}catch(SQLException e) {
e.printStackTrace();
}
}
return empList;
}
}