我想访问我的申请人数据库,这就是为什么我为此创建一个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;
}
最佳答案
我看到的巨大的,明显的问题:
DAO是单例。为什么?
您没有使用EntityManager
。还有,为什么呢?
如果您实际上使用的是Java EE(带有问题的标签),而不是J2EE(这是围绕Java 1.4构建的规范),那么DAO模式就完全没有必要了。 EntityManager
是新的DAO。
看看The Java EE 6 Tutorial - Persistence的前几节。
我们需要使用J2ee .. :(
好的,所以您仍然需要修复单例实现。考虑到该对象不存储任何内部状态,因此完全没有理由仅创建该对象的一个实例。有一个简单的解决方法:
完全删除private static ApplicantDAO me = null;
将getInstance()
实现更改为
public static ApplicantDAO getInstance() {
return new ApplicantDAO();
}
其他气味:
您几乎总是要声明
List
而不是ArrayList
,因此请更改声明,例如public ArrayList<Applicant> getApplicants() throws SQLException
// to
public List<Applicant> getApplicants() throws SQLException
// and
ArrayList<Applicant> applicantList = null;
// to
List<Applicant> applicantList = null;
关于jakarta-ee - 如何改善我的DAO? -Java EE,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6331757/