java JDBC (八) 连接池 DBCP
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 (八) 连接池 DBCP的更多相关文章
- jdbc数据连接池dbcp要导入的jar包
jdbc数据连接池dbcp要导入的jar包 只用导入commons-dbcp-x.y.z.jarcommons-pool-a.b.jar
- c3p0、dbcp、tomcat jdbc pool 连接池配置简介及常用数据库的driverClass和驱动包
[-] DBCP连接池配置 dbcp jar包 c3p0连接池配置 c3p0 jar包 jdbc-pool连接池配置 jdbc-pool jar包 常用数据库的driverClass和jdbcUrl ...
- JDBC数据源连接池(3)---Tomcat集成DBCP
此文续<JDBC数据源连接池(2)---C3P0>. Apache Tomcat作为一款JavaWeb服务器,内置了DBCP数据源连接池.在使用中,只要进行相应配置即可. 首先,确保Web ...
- DBCP,C3P0与Tomcat jdbc pool 连接池的比较
hibernate开发组推荐使用c3p0; spring开发组推荐使用dbcp(dbcp连接池有weblogic连接池同样的问题,就是强行关闭连接或数据库重启后,无法reconnect,告诉连接被重置 ...
- JDBC之 连接池
JDBC之 连接池 有这样的一种现象: 用java代码操作数据库,需要数据库连接对象,一个用户至少要用到一个连接.现在假设有成千上百万个用户,就要创建十分巨大数量的连接对象,这会使数据库承受极大的压力 ...
- JDBC数据源连接池(4)---自定义数据源连接池
[续上文<JDBC数据源连接池(3)---Tomcat集成DBCP>] 我们已经 了解了DBCP,C3P0,以及Tomcat内置的数据源连接池,那么,这些数据源连接池是如何实现的呢?为了究 ...
- JDBC数据源连接池(2)---C3P0
我们接着<JDBC数据源连接池(1)---DBCP>继续介绍数据源连接池. 首先,在Web项目的WebContent--->WEB-INF--->lib文件夹中添加C3P0的j ...
- 连接池dbcp
连接池dbcp DBCP:apache组织 使用步骤: 1.导入jar包(commons-dbcp-1.4.jar和commons-pool-1.5.6.jar.commons-logging-1.2 ...
- 关于JDBC和连接池我学到的(转载保存)
1.JDBC数据库连接池的必要性 在使用开发基于数据库的web程序时,传统的模式基本是按以下步骤: 在主程序(如servlet.beans)中建立数据库连接. 进行sql操作 断开数据库连接. 这种模 ...
随机推荐
- JavaScript Scroll家族以及封装
JavaScript Scroll家族以及封装 scrollTop & scrollLeft 别卷去的值,就是当滑动滚轮浏览网页的时候,网页隐藏在屏幕上方或左侧的距离 获得scrollTop ...
- hdoj:2032
#include <iostream> #include <string> using namespace std; int main() { int n; ][]; ; i ...
- Halcon 之dyn_threshold与threshold区别与用法
相同点:都是为了选择想要的灰度区域 dyn_threshold (OrigImage, MeanImage, SmallRaw, 3, 'light') //动态阈值分割 threshold()// ...
- .NET解决[Serializable] Attribute引发的Json序列化k_BackingField
在WebAPI中的WebApiConfig直接加入如下配置 有问题找谷歌
- Python 字符串转JSON; 先装字典在转JSON; json.dumps(d)
#-*- coding:UTF-8 -*- import os; import json class MysqlUtil(): def __init__(self): pass if __name__ ...
- python中,如何将字符串转换为数字(将数字转换为整型),字符串的10转换为整型的10,10.5转换为10
说明: 在实际的应用过程中,有的时候可能会遇到字符串的10,需要将字符串的10转换为数字的10 在此记录下,通过int函数转换的过程. 操作过程: 1.将字符串转换为整型的10 >>> ...
- Nginx-配置https虚拟服务(访问http时自动跳转https)
https口令文件和nginx配置文件位置关系: nginx配置文件内容如下: #user nobody; worker_processes 1; #设置工作进程数 pid logs/nginx.pi ...
- 使用Curator操作ZooKeeper
Curator是Netflix公司开源的一个ZooKeeper client library,用于简化ZooKeeper客户端编程.它包含如下模块: Framework:Framework是ZooKe ...
- A - Cable master
Inhabitants of the Wonderland have decided to hold a regional programming contest. The Judging Commi ...
- webpack构建工具快速上手指南
最近在研究react项目,接触到webpack打包工具.刚接触的时候一脸茫然,经过最近的学习,下面我来带大家开启webpack入门之旅. webpack是什么 webpack是近期最火的一款模块加载器 ...