java中的 jdbc 设置连接池 增删改查方法

本文介绍了一个Java项目的数据库连接池配置方法,包括从属性文件读取数据库配置信息、初始化连接池参数,并展示了如何使用该连接池执行SQL查询及更新操作。
    private static BasicDataSource dataSource;
    private static Connection con=null;
    private static PreparedStatement ps =null;
    private static ResultSet  rs = null;
    static {
        dataSource = new BasicDataSource();
        initparameter();
    }
    private static void initparameter() {
        Properties prop = new Properties();
        try {
            prop.load(ContactJdbcUtil.class.getClassLoader().getResourceAsStream("jdbc.properties"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        dataSource.setDriverClassName(prop.getProperty("driver"));
        dataSource.setUrl(prop.getProperty("url"));
        dataSource.setUsername(prop.getProperty("username"));
        dataSource.setPassword(prop.getProperty("password"));
        dataSource.setMaxActive(Integer.parseInt(prop.getProperty("maxActive")));
        dataSource.setMinIdle(Integer.parseInt(prop.getProperty("min")));
        dataSource.setInitialSize(Integer.parseInt(prop.getProperty("size")));
        dataSource.setMaxWait(Integer.parseInt(prop.getProperty("maxwait")));
    }
    public static Connection getConnection() throws SQLException{
        return dataSource.getConnection();

    }

//调用该方法前写一SQL语句  将?参数的值通过Object数组进行赋值 没有?参数时写为(sql,null)

    public static ResultSet doQuery(String sql,Object[] obj){
        try {
            con = dataSource.getConnection();
            ps = con.prepareStatement(sql);
            if(obj!=null){
                for(int i=0;i<obj.length;i++){
                    ps.setObject(i+1, obj[i]);
                }
            }
            return ps.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;

    }

//增 删 改 的方法

    public static int doUpdata(String sql,Object[] obj){
        try {
            con = dataSource.getConnection();
            ps = con.prepareStatement(sql);
            if(obj!=null){
                for(int i=0;i<obj.length;i++){
                    ps.setObject(i+1, obj[i]);
                }
            }
            return ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        
        return 0;

    }

//在调用以上的方法后调用该方法来关闭

    public static void doClose(){
            try {
                if(con!=null){
                    con.close();
                    con=null;
                }
                if(ps!=null){
                    ps.close();
                    ps=null;
                }
                if(rs!=null){
                    rs.close();
                    rs=null;
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }

    }

//日期的一个转换

    public static Date checkdate(String haridate){
        Date date = null;
        try {
            date=new Date(new SimpleDateFormat("yyyy-MM-dd").parse(haridate).getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值