1. 程式人生 > >java JDBC (八) 連接池

java JDBC (八) 連接池

runt line hand tcl cto ner user rom 容器

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 (八) 連接池