A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

一、DBCP

DBCP是Apache推出的数据库连接池(Database Connection Pool)。

操作步骤:
- 添加jar包:

commons-dbcp-1.4.jar

commons-pool-1.5.6.jar

添加属性资源文件

在JavaResource目录的src目录下创建一个dbcpconfig.properties文件。



文件内容:

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/db_user
username=root
password=root
initialSize=10
maxActive=50
maxIdle=20
minIdle=5
maxWait=60000
connectionProperties=useUnicode=true;characterEncoding=utf8
defaultAutoCommit=true
defaultReadOnly=
defaultTransactionIsolation=REPEATABLE_READ
1
2
3
4
5
6
7
8
9
10
11
12
13
编写数据源工具类

public class DBUtil {
    public static Connection getConnection(){
        Connection conn = null;

        try {
            conn = getDataSource().getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return conn;
    }

    private static DataSource getDataSource(){
        DataSource dataSource=null;
        Properties p = new Properties();
        try {
            p.load(DBUtil.class.getClassLoader().getResourceAsStream("dbcpconfig.properties"));
            dataSource = BasicDataSourceFactory.createDataSource(p);
        } catch (Exception e) {
            throw new RuntimeException("获取DataSource对象失败");
        }
        return dataSource;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
二、C3P0

操作步骤:

添加jar包

添加 c3p0-0.9.1.2.jar

编写配置文件

在JavaResource目录的src目录下创建一个c3p0-config.xml文件。



c3p0-config.xml文件内容:

<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
  <default-config>
    <property name="driverClass">com.mysql.jdbc.Driver</property>
    <property name="jdbcUrl">jdbc:mysql://localhost:3306/db_user</property>
    <property name="user">root</property>
    <property name="password">root</property>
    <property name="initialPoolSize">10</property>
    <property name="maxIdleTime">30</property>
    <property name="maxPoolSize">100</property>
    <property name="minPoolSize">10</property>
  </default-config>
</c3p0-config>
1
2
3
4
5
6
7
8
9
10
11
12
13
编写数据源工具类

public class DBUtil {
    private static DataSource dataSource = new ComboPooledDataSource();
    public static Connection getConnection(){
        Connection conn = null;
        try {
            conn = dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
三、JavaWeb之Tomcat管理数据源

上面2中方式都需要导入jar包,在JavaWeb服务器Tomcat中其实内置了数据源。所以不需要导入jar包。

Tomcat内置数据源其实也是DBCP,是Tomcat的lib目录下的tomcat-dbcp.jar。

配置数据源的步骤:

拷贝数据库连接的jar(mysql-connector-java-5.1.7-bin.jar)到tomcat/lib目录下



配置数据源XML文件

如果把配置信息写在tomcat下的conf目录下context.xml中。

<Context>
     <Resource name="jdbc/login_register" auth="Container" type="javax.sql.DataSource"
           maxActive="100" maxIdle="30" maxWait="10000"
           username="root" password="root" driverClassName="com.mysql.jdbc.Driver"
           url="jdbc:mysql://localhost:3306/db_user"/>
</Context>
1
2
3
4
5
6
如果是在当前应用的META-INF中创建context.xml,编写数据源,那么只有当前应用可以使用。



<Context>
     <Resource name="jdbc/login_register" auth="Container" type="javax.sql.DataSource"
           maxActive="100" maxIdle="30" maxWait="10000"
           username="root" password="root" driverClassName="com.mysql.jdbc.Driver"
           url="jdbc:mysql://localhost:3306/db_user"/>
</Context>
1
2
3
4
5
6
使用连接池,封装工具类

public class DBUtil {
    public static Connection getConnection(){
        Connection conn = null;
        try {
            Context c = new InitialContext();
            DataSource dataSource = (DataSource) c.lookup("java:/comp/env/jdbc/login_register");//这里的jdbc/login_register和篇配置文件中的name属性一致
            conn = dataSource.getConnection();
            return conn;
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (NamingException e) {
            e.printStackTrace();
        }

        return conn;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
【注意】在使用Javaweb配置数据源时,可能会碰到这样的一个错误:

java.lang.AbstractMethodError: com.mysql.jdbc.Connection.isValid(I)Z
1
这是你的Tomcat放置的 mysql-connector-java-x.x.x-bin.jar 版本比较低。下载新的版本就好,mysql-connector-java-5.1.7-bin.jar下载。
---------------------
【转】
作者:张行之
来源:CSDN
原文:https://blog.csdn.net/qq_33689414/article/details/61204237
版权声明:本文为博主原创文章,转载请附上博文链接!

2 个回复

倒序浏览
奈斯,感谢分享
回复 使用道具 举报
12345678901234567
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马