连接池 connection pool

来源:互联网 发布:数据库系统概论 第5版 编辑:程序博客网 时间:2024/05/21 12:08

连接池 connection pool

 

开车:

(1) 油门,刹车 不停踩

(2) 在很远的距离就停止踩油门

 

网吧:

(1) 来一个人开一台电脑,人走了就关电脑

(2) 网吧一直保持N台电脑开着,人来了就直接使用,走了就保持电脑开着

 

程序:以前的JDBC

需要操作某个功能就打开一个连接,用完,马上关闭

   缓冲池

回顾:xml解析方法:1 DOMdom4j jdom2 sax(xpath)

连接池:

1 c3p0  

2 dbcp

3 jndi  java naming and directory interface

命名和目录接口

 

JNDI连接池:把配置文件全部配置到tomcat中的conf文件夹中的context.xml文件,我们把需要配置的代码写在<Context>标签中的任意位置

 

JNDI步骤:

1 拷贝jdbc jar

2 配置tomcat

 

 <Resource

name="连接池名"

auth="Container"

type="javax.sql.DataSource"  

maxActive="100"   //最大连接数

maxIdle="30" //保持30个活动连接数

maxWait="5000"   //如果已连接的用户在5000秒内没有再次连接,则自动断开连接

username="sa"   

password="123"

    driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"  

    url="jdbc:sqlserver://127.0.0.1:1433;databasename=数据库名"/>

 

mysql:

<Resource

name="连接池名"

auth="Container"

type="javax.sql.DataSource"  

maxActive="100"

maxIdle="30"

maxWait="10000"   

username="root"   

password="123456"

driverClassName="com.mysql.jdbc.Driver"  

url="jdbc:mysql://127.0.0.1:3306/数据库名"/>

 

3 找到项目的web.xml文件

<resource-ref>

<res-ref-name>连接池的名字</res-ref-name>

<res-type>javax.sql.DataSource</res-type>

<res-auth>Container</res-auth>

</resource-ref>

 

4 更新DBHelper

获取DataSource

public DataSource ds;

public DBHelper(){

try {

Context ctx= new InitialContext();

ds=(DataSource)ctx.lookup("java:comp/env/连接池名字");

} catch (Exception e) {

// TODO: handle exception

}

}

conn=ds.getConnection();

 

补充:

JSP页面中使用日期时间格式

1 引用以下标签

<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>

2 使用:

<fmt:formatDate value="${原来的值 }" pattern="yyyy-MM-dd hh:mm:ss"/>

 

回顾:

javadatesqldate

如果sql是年月日时分秒

new java.sql.Timestamp(new Date().getTime())

如果sql是年月日

new java.sql.Date(new Date().getTime())