tomcat中使用mysql连接池的配置

1、下载相应的jar包,添加到工程中

需要下载的包主要有commons-pool2-2.2 commons-dbcp2-2.0.1-src commons-dbcp2-2.0.1  commons-collections4-4.0

2、tomcat的相关配置

在WebContent的META-INF下新建context.xml文件输入如下内容:

<?xml version="1.0" encoding="UTF-8"?>
<context>
<Resource name="jdbc/Mysql"
auth="Container"
driverClassName="com.mysql.jdbc.Driver"
type="javax.sql.DataSource"
url="jdbc:mysql://127.0.0.1:3306/my_report"
username="root"
password="21424019"
maxActive="100"
maxIdle="30"
maxWait="1000"/>
</context>

3.在C盘目录下新建my.ini文件

文件内容如下:

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.

[mysqld]

# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M

# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin

# These are commonly set, remove the # and set as required.
basedir = E:\develope_software\MySQL\mysql-advanced-5.6.20-winx64
datadir = E:\develope_software\MySQL\mysql-advanced-5.6.20-winx64\data
# port = .....
# server_id = .....


# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
join_buffer_size = 128M
sort_buffer_size = 2M
read_rnd_buffer_size = 2M

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

当tomcat启动使用mysql连接池时会读取c盘根目录下的my.ini

4、修改web.xml,添加如下内容

<resource-ref>
<description>Connection Pool</description>
<res-ref-name>jdbc/Mysql</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>

5、连接池操作

package com;

//import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;


import javax.sql.DataSource;

 

import java.sql.*;

 

public class Mysql {

private static final String DRIVER="com.mysql.jdbc.Driver";
private static final String URL = "jdbc:mysql://127.0.0.1:3306/my_report";
private static final String USERNAME = "root";
private static final String PASSWORD = "21424019";
Connection con = null;
ResultSet result = null;
PreparedStatement statement = null;
PreparedStatement stateInsert = null;
PreparedStatement stateUnlock = null;

public Connection getConnection() throws SQLException //获取数据库连接
{
DataSource datapool = ConnectionPool.getDataSource();
if(datapool!=null)
{
con = datapool.getConnection();
return con;
}
else
System.out.println("数据库连接池对象为空");
return null;
}

public ResultSet executeQuery(String sql) { //负责数据查询
try {
con = this.getConnection();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if(con!=null)
{
try {
statement = (PreparedStatement) con.prepareStatement(sql);
result = statement.executeQuery();
return result;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("query data failed");
}
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}

public int executeUpdate(String sql)//负责数据更新
{
try {
con = this.getConnection();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
statement = (PreparedStatement) con.prepareStatement(sql);
int result = statement.executeUpdate(sql);
return result;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("query data failed");
}
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return -1;
}
return 0;
}
}

posted @ 2014-09-29 15:56  Earendil  阅读(479)  评论(0编辑  收藏  举报