1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
@Test /** * 采用配置文件的方式: */ public void demo2(){ Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; try{ // 获得连接:从连接池中获取: // 创建连接池://创建连接池默认去类路径下查找c3p0-config.xml ComboPooledDataSource dataSource = new ComboPooledDataSource(); // 从连接池中获得连接: conn = dataSource.getConnection(); // 编写SQL: String sql = "select * from account"; // 预编译SQL: pstmt = conn.prepareStatement(sql); // 执行SQL: rs = pstmt.executeQuery(); while(rs.next()){ System.out.println(rs.getInt("id")+" "+rs.getString("name")+" "+rs.getDouble("money")); } }catch(Exception e){ e.printStackTrace(); }finally{ JDBCUtils.release(rs, pstmt, conn); } }
|
近期评论