OutputStreamWriter out = ... java.sql.Connection conn = ... try { Statement stat = conn.createStatement(); ResultSet rs = stat.executeQuery( "select uid, name from user"); while (rs.next()) { out.println("ID:" + rs.getString("uid") + ",姓名: " + rs.getString("name")); } } catch(SQLException sqlex) { out.println("警告:数据不完整"); throw new ApplicationException("读取数据时出现SQL错误", sqlex); } catch(IOException ioex) { throw new ApplicationException("写入数据时出现IO错误", ioex); } finally { if (conn != null) { try { conn.close(); } catch(SQLException sqlex2) { System.err(this.getClass().getName() + ".mymethod - 不能关闭数据库连接: " + sqlex2.toString()); } }
if (out != null) { try { out.close(); } catch(IOException ioex2) { System.err(this.getClass().getName() + ".mymethod - 不能关闭输出文件" + ioex2.toString()); } } } |