看了“大家晒下java开发代码的小技巧啊!”,感觉自己写的代码实在不够规范,请问我的这一段代码,对于connection、resultset的处理中,链接关闭、结果集、空值处理等是否都合理呢? public SalePerson getSalePerson(Long personid) {
SalePerson sp = null;//一个bean
DbConnection dc = null;//数据库连接
String sql = "select * from sale_person where personid=" + personid;
try {
dc = new DbConnection();
ResultSet rs = dc.executeQuery(sql);
if (rs.next()) {
sp = new SalePerson();
sp.setPersonName(rs.getString("personname"));
sp.setSex(rs.getInt("sex"));
sp.setGongZuoDanWei(rs.getString("gongzuodanwei"));
sp.setDeptName(rs.getString("deptname"));
sp.setIdentityCard(rs.getString("identitycard"));
sp.setIsMarry(rs.getInt("ismarry"));
sp.setIsVIP(rs.getInt("isvip"));
sp.setPersonMobile(rs.getString("personmobile"));
}
rs.close();
dc.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (dc != null) {
try {
dc.close();
} catch (Exception e) {
e.printStackTrace();
}
}
} return sp;
}
SalePerson sp = null;//一个bean
DbConnection dc = null;//数据库连接
String sql = "select * from sale_person where personid=" + personid;
try {
dc = new DbConnection();
ResultSet rs = dc.executeQuery(sql);
if (rs.next()) {
sp = new SalePerson();
sp.setPersonName(rs.getString("personname"));
sp.setSex(rs.getInt("sex"));
sp.setGongZuoDanWei(rs.getString("gongzuodanwei"));
sp.setDeptName(rs.getString("deptname"));
sp.setIdentityCard(rs.getString("identitycard"));
sp.setIsMarry(rs.getInt("ismarry"));
sp.setIsVIP(rs.getInt("isvip"));
sp.setPersonMobile(rs.getString("personmobile"));
}
rs.close();
dc.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (dc != null) {
try {
dc.close();
} catch (Exception e) {
e.printStackTrace();
}
}
} return sp;
}
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null; try {
con = ConnectionFactory.getConnection(); // 直接从连接工厂中获得连接,从而进行解耦
String sql = "SELECT * FROM sale_person WHERE personid= ?"; ps = con.preparedStatement(sql);
ps.setLong(1, personId); ResultSet rs = ps.executeQuery(); if (rs.next()) {
sp = new SalePerson();
sp.setPersonName(rs.getString("personname"));
sp.setSex(rs.getInt("sex"));
sp.setGongZuoDanWei(rs.getString("gongzuodanwei"));
sp.setDeptName(rs.getString("deptname"));
sp.setIdentityCard(rs.getString("identitycard"));
sp.setIsMarry(rs.getInt("ismarry"));
sp.setIsVIP(rs.getInt("isvip"));
sp.setPersonMobile(rs.getString("personmobile"));
} } catch (Exception e) {
e.printStackTrace(); // 异常除了输出堆栈之外,最好再能处理一下,下面的也是
} finally {
if(rs != null) try { rs.close() } catch(Exception e) { e.printStackTrace(); }
if(ps != null) try { ps.close() } catch(Exception e) { e.printStackTrace(); }
if(con != null) try { con.close() } catch(Exception e) { e.printStackTrace(); }
}
return sp;
}
private final String sql = "SELECT * FROM sale_person WHERE personid= ?";--SQL语句
public SalePerson getSalePerson(Long personId) { SalePerson sp = null; //一个bean
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null; try {
con = ConnectionFactory.getConnection(); // 直接从连接工厂中获得连接,从而进行解耦
ps = con.preparedStatement(sql);
ps.setLong(1, personId); ResultSet rs = ps.executeQuery(); if (rs.next()) {
sp = new SalePerson();
sp.setPersonName(rs.getString("personname"));
sp.setSex(rs.getInt("sex"));
sp.setGongZuoDanWei(rs.getString("gongzuodanwei"));
sp.setDeptName(rs.getString("deptname"));
sp.setIdentityCard(rs.getString("identitycard"));
sp.setIsMarry(rs.getInt("ismarry"));
sp.setIsVIP(rs.getInt("isvip"));
sp.setPersonMobile(rs.getString("personmobile"));
} } catch (Exception e) {
e.printStackTrace(); // 异常除了输出堆栈之外,最好再能处理一下,下面的也是
} finally {
if(rs != null) try { rs.close() } catch(Exception e) { e.printStackTrace(); }
if(ps != null) try { ps.close() } catch(Exception e) { e.printStackTrace(); }
if(con != null) try { con.close() } catch(Exception e) { e.printStackTrace(); }
}
return sp;
}
}