我的數據庫中有這些表。一個是employee
,第二個是employee_Project
,第三個是employee_Reporting
。每個表格都有一個共同的employee_Number
作爲主鍵,並且它們之間存在一對多的關係,因此員工有許多項目和報告日期。jdbc性能
我在這有方法fillResultSet(Result set)
和List<T> getData()
三個數據持有者類運行select * from employee
,select * from employee_project
,select * from employee_reporting
。這是基於SqlDbEngine
類和runQuery(PreparedStatement,DataHolder)
方法,並且實現已完成。
現在我必須設計一個getAllEmployee()
方法以及使用JDBC在java中優化代碼的項目和報告細節。我已經使用了一個迭代器,但這個解決方案是不可接受的;現在我必須使用foreach循環。
這是我做了什麼:
public List<Employee> getAllEmployees() {
EmployeeDataHolderImpl empdataholder = new EmployeeDataHolderImpl();
List<Employee> list_Employee_Add = null;
try {
Connection connection = mySqlDbConnection.getConnection();
PreparedStatement preparedStatement = connection
.prepareStatement(GET_ALL_EMPLOYEE_DETAILS);
mySqlDBEngineImpl.runQuery(preparedStatement, empdataholder);
} catch (SQLException e) {
e.printStackTrace();
}
for (Employee employee : empdataholder.getData()) {
new EmployeeDAOImpl().getProject(employee);
new EmployeeDAOImpl.getReport(employee);
}
list_Employee_Add = empdataholder.getData();
return list_Employee_Add;
}
,使另一種方法
public void getProject(Employee emp) {
EmployeeProjectDataHolderImpl employeeProjectHolder = new EmployeeProjectDataHolderImpl();
try {
Connection connection = mySqlDbConnection.getConnection();
PreparedStatement preparedStatement = connection
.prepareStatement(GET_ALL_PROJECT_DETAILS);
mySqlDBEngineImpl
.runQuery(preparedStatement, employeeProjectHolder);
} catch (SQLException e) {
e.printStackTrace();
}
for (EmployeeProject employee_Project : employeeProjectHolder.getData()) {
if (employee_Project.getEmployeeNumber() == emp.getEmpNumber()) {
emp.getProjects().add(employee_Project);
}
}
}
public void getReport(Employee emp) {
EmployeeReportDataHolderImpl employeeReportHolder = new EmployeeReportDataHolderImpl();
try {
Connection connection = mySqlDbConnection.getConnection();
PreparedStatement preparedStatement = connection
.prepareStatement(GET_ALL_REPORT_DETAILS);
mySqlDBEngineImpl
.runQuery(preparedStatement, employeeReportHolder);
} catch (SQLException e) {
e.printStackTrace();
}
for (EmployeeReport employee_Report : employeeReportHolder.getData()) {
if (employee_Report.getEmployeeNumber() == emp.getEmpNumber()) {
emp.getProjects().add(employee_Project);
}
}
}
}
爲員工報告
和相同的,但這樣做,這樣的表現是要decrease.no關於關閉連接體憂我會去做
請告訴我怎樣才能改善我的解決方案..
我很困惑你的問題。你說你已經完成了'select * from employee'的實現。這不是基本上會成爲你的'getAllEmployees()'方法嗎?另外,你提到你的基於迭代器的解決方案是不可接受的,所以你需要使用foreach循環。Foreach **是一個迭代器 - 它只是語法糖!什麼是真正的*爲什麼你需要使用foreach? –