tomcat中使用mysql连接池的配置


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

需要下载的包主要有commons-pool2-2.2commons-dbcp2-2.0.1-srccommons-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;
}
}

优质内容筛选与推荐>>
1、C#生成缩略图,加文字或图片水印
2、封装一个简单的原生js焦点轮播图插件
3、系统吞吐量(TPS)、用户并发量、性能测试概念和公式
4、无聊写的压位高精加
5、WPF报表自定义通用可筛选列头-WPF特工队内部资料


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号