日期:2014-05-16  浏览次数:20506 次

一个比较规范的jdbc连接方法类

主要是留意里面的关闭连接释放资源的写法~

public String viewCheck(String id) {
?? Session session = factory.openSession();
?? String sumAmount = "";
?? Transaction tx = session.beginTransaction();
?? String sql = "select * from t_merchant_cz where name='"+id+"'";
?? System.out.println(sql);
?? Connection conn = session.connection();??
?? try {
??? Statement stmt = conn.createStatement();
??? ResultSet rs = stmt.executeQuery(sql);
??? if(rs.next()){
???? sumAmount = (String)rs.getString("sum_amount");
??? }
???
?? } catch (Exception e){
??? try {
???? throw e;
??? } catch (Exception se) {
??? }
?? } finally {
??? try {
???? tx.commit();
???? conn.close();
??? } catch (Exception e) {
??? }
??? session.close();
?? }
?? return sumAmount;
}

?