首页 > 编程语言 > 详细

java—c3p0utils封装

时间:2019-08-05 01:07:26      阅读:204      评论:0      收藏:0      [点我收藏+]

封装C3P0简化代码量

public class C3P0utils {
    private static ComboPooledDataSource dataSource = new ComboPooledDataSource();

    public static DataSource getDataSource(){
        return dataSource;
    }
    public static Connection getConnection(){
        try {
            return dataSource.getConnection();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}

测试类

public class Test_c3p0util {
    @Test
    public void test2() {
        Connection conn = null;
        PreparedStatement pstmt = null;

        try {
            // 1.获取连接
            conn = C3P0utils.getConnection();
            // 2.编写sql语句
            String sql = "insert t1 (id,name) value (?,?)";
            // 3.获取执行sql语句对象
            pstmt = conn.prepareStatement(sql);
            // 4.设置参数
            pstmt.setInt(1, 7);
            pstmt.setString(2, "wuwuww");
            // 5.执行删除操作
            int row = pstmt.executeUpdate();
            if (row > 0) {
                System.out.println("删除成功!");
            } else {
                System.out.println("删除失败!");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            // 6.释放资源
            JBDC_V2.release(conn, pstmt, null);
        }
    }
}

 

java—c3p0utils封装

原文:https://www.cnblogs.com/zhuzhiwei-2019/p/11300587.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!