81,122
社区成员




public class CampaignDao {
private static Connection conn = null;
private ResultSet rs = null;
private PreparedStatement pstmt = null;
public CampaignDao(){
try {
conn = BaseUtil.getConnection();
} catch (Exception e1) {
e1.printStackTrace();
}
}
public Campaign queryById(int campaignId){
Campaign campaign = null;
log.debug("campaign queryByid() run");
String sql = null;
try {
sql="select * from campaign where campaignId=?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, campaignId);
rs = pstmt.executeQuery();
rs.next();
campaign = new Campaign(campaignId,rs.getString("campaignName"),
rs.getString("contentProvider"),rs.getString("createUser"),rs.getString("description"),
rs.getTimestamp("startDate"),rs.getTimestamp("endDate"));
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
if(rs!=null)rs.close();
if(pstmt!=null)pstmt.close();
if(conn!=null)conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return campaign;
}
private Connection con;
public void test() {
con = ConnectionFactory.getConnection();
....
}
线程 时间片
--------------------------------------------------------
A 获得连接A 关闭
B 获得连接B 关闭
--------------------------------------------------------
con状态 连接A 连接B 连接B关闭 报错
public void query() {
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
con = ConnectionFactory.getConnection();
String sql = "xxxx";
ps = con.preparedSatement(sql);
ps.setXxxxx(1, xxx);
rs = ps.executeQuery();
while(rs.next()) {
Xxxx xxx = rs.getXxxx();
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (rs != null) try { rs.close(); } catch (SQLException e) { e.printStackTrace(); }
if (ps != null) try { ps.close(); } catch (SQLException e) { e.printStackTrace(); }
if (con != null) try { con.close(); } catch (SQLException e) { e.printStackTrace(); }
}
}