My objective is to get the DeptNames of certain employees from the Emp table. But the number of employees is varying. Please tell if the below approach is correct and which of the below approach is better performance wise. Or is there a better way to go about it?
PreparedStatement pstmt = conn.prepareStatement("SELECT Dept from Emp WHERE EmpNum = ? ");
for (int i = 0, len = empNumberList.size(); i < len; i++) {
pstmt.setLong (1, empNumberList.get(i));
ResultSet rs = pstmt.executeQuery();
}
Or
StringBuffer buffer = new StringBuffer();
buffer.append("SELECT Dept from Emp WHERE EmpNum IN(?");
for (int i = 0, len = empNumberList.size(); i < len; i++) {
buffer.append(",?");
}
buffer.append(")");
PreparedStatement pstmt = con.prepareStatement(buffer.toString());
for(int i = 0; i < len; i++) {
pstmt .setLong(i, empNumberList.get(i));
}
ResultSet rs = pstmt .executeQuery();
Or is there any other better way to do this? Please advice. Thanks in advance!
Ravilla