@@ -35,12 +35,9 @@ public int update(String sql,Object...args){
3535 //4.发送SQL语句,并返回影响行数
3636 return queryRunner .update (connection ,sql ,args );
3737 } catch (SQLException e ) {
38- e .printStackTrace ();
39- }finally {
40- //5.回收资源
41- JdbcUtils .close (connection );
38+ e .printStackTrace ();//捕获异常
39+ throw new RuntimeException (e );//抛出异常 给调用者 , 层层往上抛 , 这样上层才能回滚事务
4240 }
43- return -1 ;
4441 }
4542
4643 /**
@@ -61,13 +58,10 @@ public<T> T queryForOne(Class<T>type, String sql,Object...args){
6158 //3.占位符赋值
6259 //4.发送SQL语句 并返回 结果集
6360 return queryRunner .query (connection ,sql ,new BeanHandler <T >(type ),args );
64- } catch (Exception e ) {
65- e .printStackTrace ();
66- }finally {
67- //5.回收连接
68- JdbcUtils .close (connection );
61+ } catch (SQLException e ) {
62+ e .printStackTrace ();//捕获异常
63+ throw new RuntimeException (e );//抛出异常 给调用者 , 层层往上抛 , 这样上层才能回滚事务
6964 }
70- return null ;
7165 }
7266
7367
@@ -89,12 +83,10 @@ public<T> List<T> queryForList(Class<T>type, String sql,Object...args){
8983 //3.占位符赋值
9084 //4.执行SQL语句 , 返回结果集
9185 return queryRunner .query (connection ,sql ,new BeanListHandler <T >(type ),args );
92- } catch (Exception e ) {
93- e .printStackTrace ();
94- }finally {
95- JdbcUtils .close (connection );
86+ } catch (SQLException e ) {
87+ e .printStackTrace ();//捕获异常
88+ throw new RuntimeException (e );//抛出异常 给调用者 , 层层往上抛 , 这样上层才能回滚事务
9689 }
97- return null ;
9890 }
9991
10092 /**
@@ -111,11 +103,9 @@ public Object queryForSingleValue(String sql,Object...args){
111103 //3.占位符赋值
112104 //4.执行SQL语句,返回结果集
113105 return queryRunner .query (connection ,sql ,new ScalarHandler (),args );
114- } catch (Exception e ) {
115- e .printStackTrace ();
116- }finally {
117- JdbcUtils .close (connection );
106+ } catch (SQLException e ) {
107+ e .printStackTrace ();//捕获异常
108+ throw new RuntimeException (e );//抛出异常 给调用者 , 层层往上抛 , 这样上层才能回滚事务
118109 }
119- return null ;
120110 }
121111}
0 commit comments