gpt4 book ai didi

jakarta-ee - 我怎样才能改进我的 DAO? - Java EE

转载 作者:行者123 更新时间:2023-12-03 09:55:52 24 4
gpt4 key购买 nike

我想访问我的申请人数据库,这就是我为其创建 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;
}

最佳答案

我看到的巨大、明显的问题:

  1. DAO 是一个单例。为什么?
  2. 你没有使用 EntityManager .还有,为什么?

如果您实际上使用的是 Java EE(正如问题所标记的那样)而不是 J2EE(这是一个围绕 Java 1.4 构建的悲惨规范),那么 DAO 模式就完全没有必要了。 EntityManager 是新的 DAO。

看看 The Java EE 6 Tutorial - Persistence 的前几节.


we are required to use 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com