码迷,mamicode.com
首页 > 数据库 > 详细

java JDBC (八) 连接池

时间:2019-01-18 15:10:34      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:public   name   dbutils   class   报错   price   ble   put   list   

package cn.sasa.demo1;

import javax.sql.DataSource;

import org.apache.commons.dbcp2.BasicDataSource;

public class DBUtils {
    private static BasicDataSource datasource = new BasicDataSource();
    
    static {
        /**
         * 连接池:
         * 存放连接的容器
         * 不需要每次都创建连接、释放连接
         * 
         * DataSource接口
         * java为数据库连接池提供的公共接口:javax.sql.DataSource
         * 常用的连接池: DBCP C3P0
         * 
         * 导入jar包
         * DBCP连接池:http://commons.apache.org/proper/commons-dbcp/download_dbcp.cgi
         * Commons Pool:http://commons.apache.org/proper/commons-pool/download_pool.cgi
         * DBCP依赖于Pool
         * 只导入上面两个包运行如下代码会报错:java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
         * 还需导入包commons-logging:http://commons.apache.org/proper/commons-logging/download_logging.cgi
         */
        //数据库连接信息,必须
        datasource.setDriverClassName(DBProperties.driver);
        datasource.setUrl(DBProperties.url);
        datasource.setUsername(DBProperties.user);
        datasource.setPassword(DBProperties.pwd);
        //连接池的配置,可选信息
        datasource.setInitialSize(10);//初始化的连接数
        //....
    }
    
    public static DataSource getDataSource() {
        return datasource;
    }    
}

 

package cn.sasa.demo1;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

public class ConnectionPoolDemo {
    static QueryRunner query = new QueryRunner(DBUtils.getDataSource());
    public static void main(String[] args) {
        select();
    }
    static void select() {
        String sql = "SELECT * FROM product";
        try {
            List<Product> plist = query.query(sql, new BeanListHandler<Product>(Product.class));
            for(Product product : plist) {
                System.out.print(product.getPname()+"="+product.getPrice()+"\t");
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

 

package cn.sasa.demo1;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class DBProperties {
    public static String driver = "";
    public static String url = "";
    public static String user = "";
    public static String pwd = "";
    
    static {
        try {
            InputStream input = DBProperties.class.getClassLoader().getResourceAsStream("database.properties");
            Properties properties = new Properties();
            properties.load(input);
            driver = properties.getProperty("driver");
            url = properties.getProperty("url");
            user = properties.getProperty("user");
            pwd = properties.getProperty("pwd");
        }catch(IOException ex){
//            ex.printStackTrace();
            throw new RuntimeException("数据库信息获取失败");
        }
    }
}

 

package cn.sasa.demo1;

public class Product {
    private int pid;
    private String pname;
    private double price;
    private String ptype;
    private String create_tm;
    
    public Product() {}
    public Product(int pid, String pname,double price,
            String ptype, String create_tm) {
        this.pid = pid;
        this.pname = pname;
        this.price = price;
        this.ptype = ptype;
        this.create_tm = create_tm;
    }
    public int getPid() {
        return pid;
    }
    public void setPid(int pid) {
        this.pid = pid;
    }
    public String getPname() {
        return pname;
    }
    public void setPname(String pname) {
        this.pname = pname;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }
    public String getPtype() {
        return ptype;
    }
    public void setPtype(String ptype) {
        this.ptype = ptype;
    }
    public String getCreate_tm() {
        return create_tm;
    }
    public void setCreate_tm(String create_tm) {
        this.create_tm = create_tm;
    }
}

 

java JDBC (八) 连接池

标签:public   name   dbutils   class   报错   price   ble   put   list   

原文地址:https://www.cnblogs.com/SasaL/p/10287438.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!