【发布时间】:2011-06-13 14:26:36
【问题描述】:
我想访问我的申请人数据库,这就是我为其创建 DAO 类的原因。
我觉得我有很多代码味道,因为我不断重复一些代码。那么我可以做些什么来简化我的代码以减少代码异味呢?我违反了什么规则?我怎样才能改进我的代码?谢谢。
我的代码如下:
public class ApplicantDAO {
private static ApplicantDAO me = null;
private ApplicantDAO(){};
public static synchronized ApplicantDAO getInstance() {
if(me == null) {
me = new ApplicantDAO();
}
return me;
}
public Applicant getApplicant(int applicantNumber) throws SQLException {
Applicant applicant = null;
Connection conn = null;
Statement statement= null;
String query = null;
ResultSet rs = null;
try {
conn = ConnectionManager.getConnection();
statement = conn.createStatement();
query = "SELECT * FROM applicant WHERE applicant_no = '" + applicantNumber +"'"; //check applicant_number
rs = statement.executeQuery(query);
while(rs.next()){
applicant = new Applicant();
applicant.setApplicantNumber(rs.getInt("applicant_no"));
applicant.setApplicationDate(rs.getString("applicant_date"));
applicant.setfName(rs.getString("first_name"));
applicant.setlName(rs.getString("last_name"));
applicant.setmName(rs.getString("middle_name"));
applicant.setAge(rs.getInt("age"));
applicant.setGender(rs.getString("gender"));
applicant.setEmail(rs.getString("email_address"));
applicant.setContactNumber(rs.getString("contact_no"));
applicant.setCity(rs.getString("city"));
applicant.setSchool(rs.getString("school"));
applicant.setCourse(rs.getString("course"));
applicant.setYearGraduated(rs.getInt("year_graduated"));
applicant.setYearWorkExp(rs.getInt("year_work_exp"));
applicant.setSourceChannel(rs.getString("source_channel"));
applicant.setStatus_id(rs.getInt("status_id"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if (rs != null) try { rs.close(); } catch (SQLException logOrIgnore) {}
if (statement != null) try { statement.close(); } catch (SQLException logOrIgnore) {}
if (conn!= null) try { conn.close(); } catch (SQLException logOrIgnore) {}
}
return applicant;
}
public ArrayList<Applicant> getApplicants() throws SQLException{
ArrayList<Applicant> applicantList = null;
Applicant applicant = null;
Connection conn = null;
Statement statement= null;
String query = null;
ResultSet rs = null;
try {
conn = ConnectionManager.getConnection();
statement = conn.createStatement();
query = "select * from applicant";
rs = statement.executeQuery(query);
while(rs.next()){
if(applicantList == null){
applicantList = new ArrayList<Applicant>();
}
applicant = new Applicant();
applicant.setApplicantNumber(rs.getInt("applicant_no"));
applicant.setApplicationDate(rs.getString("applicant_date"));
applicant.setfName(rs.getString("first_name"));
applicant.setlName(rs.getString("last_name"));
applicant.setmName(rs.getString("middle_name"));
applicant.setAge(rs.getInt("age"));
applicant.setGender(rs.getString("gender"));
applicant.setEmail(rs.getString("email_address"));
applicant.setContactNumber(rs.getString("contact_no"));
applicant.setCity(rs.getString("city"));
applicant.setSchool(rs.getString("school"));
applicant.setCourse(rs.getString("course"));
applicant.setYearGraduated(rs.getInt("year_graduated"));
applicant.setYearWorkExp(rs.getInt("year_work_exp"));
applicant.setSourceChannel(rs.getString("source_channel"));
applicant.setStatus_id(rs.getInt("status_id"));
applicantList.add(applicant);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if (rs != null) try { rs.close(); } catch (SQLException logOrIgnore) {}
if (statement != null) try { statement.close(); } catch (SQLException logOrIgnore) {}
if (conn!= null) try { conn.close(); } catch (SQLException logOrIgnore) {}
}
return applicantList;
}
【问题讨论】:
标签: jakarta-ee dao