- 论坛徽章:
- 0
|
我也改改试试
- OutputStreamWriter out = ...
- java.sql.Connection conn = ...
- StringBuffer stringBuffer;
- try
- {
- try
- {
- Statement stat = conn.createStatement();
- try
- {
- ResultSet rs = stat.executeQuery("select uid, name from user");
- try
- {
- try
- {
- while (rs.next())
- {
- stringBuffer = new StringBuffer("ID : ").
- append(rs.getString("uid")).
- append(" , 姓名 : ").
- append(rs.getString("name"));
- out.println(stringBuffer.toString());
- //out.flush();
- }
- }finally{
- out.close();
- }
- }finally{
- rs.close();
- }
- }finally{
- stat.close();
- }
- }finally{
- conn.close();
- }
- }catch(SQLException e)
- {
- //System.err.println("访问数据库发生异常");
- throw new RuntimeException(e);
- //或者抛出其他的异常
- }catch(IOException e){
- //System.err.println("发生IO异常");
- throw new RuntimeException(e);
- //或者抛出其他的异常
- }
复制代码
我是菜鸟!!!!!!!!!!!!
感觉上对于这种IOException和除非SQL语法问题所引发的非常不可思议的问题( 比如说关闭资源时失败... 关闭连接时失败...) 等, 应该属于外界原因(代码本身没问题, 问题出在环境上)
我自己学习的时候一般是给打包成DaoException, 不管是IO异常好 还是SQL异常
然后事务处理里面再细分"NoSuchUserIDException" "GetUserIOException" (GetUserIOException继承Exception,不是IOException),我想把数据读写这里发生的超出意外的异常归为数据操做异常~ 理应在开发的第一步就剔出, 在使用期间作为"错误"处理(记录下来、 保存完整的追踪信息 一般是写一个拦截器处理了)
我没有开发经验 以上只是偏见, 还请各位高手嘴下留情, 如果愿意纠正错误, 在下十分感激!
[ 本帖最后由 =SAGA=DPurple 于 2008-12-22 08:14 编辑 ] |
|