观传智播客李勇JDBC视频之《释放连接》所惑
去年看他的视频时没觉得有什么问题,那时自己也不懂。
今天忽然想起这个问题,做了个小小的测试,结果发现他的方法好像有错误。
现来请教下大家。
//释放链接 public static void free(ResultSet rs, Statement st, Connection conn) { try { if (rs != null) rs.close(); } catch (SQLException e) { e.printStackTrace(); } finally { try { if (st != null) st.close(); } catch (SQLException e) { e.printStackTrace(); } finally { if (conn != null) try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } } public void insert(String name, String passwd) { try { conn = DriverManager.getConnection(dbUrl, dbUser, dbPassword); ps = conn .prepareStatement("insert into user(username,password) values(?,?)"); ps.setString(1, name); ps.setString(2, passwd); ps.executeUpdate(); } catch (Exception e) { System.out.println(e.toString()); } }