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;
}
}
tomcat中使用mysql连接池的配置的更多相关文章
- python中实现mysql连接池
python中实现mysql连接池 import pymysql from DBUtils.PooledDB import PooledDB MYSQL_HOST = 'localhost' USER ...
- Hibernate的配置中,c3p0连接池相关配置
一.配置c3p0 1.导入 hibernate-c3po连接池包,Maven地址是:http://mvnrepository.com/artifact/org.hibernate/hibernate- ...
- Spring+Tomcat的JNDI数据源连接池简单配置
使用Tomcat JNDI数据源与Spring一起使用步骤如下: 1.将数据库驱动复制到Tomcat的lib文件夹下面 2.配置Tomcat的server.xml配置文件,在GlobalNamingR ...
- JNDI实现服务器(tomcat)与数据库(mysql)连接的数据源配置以及获取连接的java代码
->首先将mysql的jar包导入到tomcat/lib文件夹下 ->然后在tomcat/conf/context.xml文件中配置以下内容 <Resource name=" ...
- Tomcat配置MySql连接池问题
配置过程如下: 1.修改Tomcat—>conf目录下的context.xml文件 <Context path="/DBTest" docBase="DBTe ...
- Java Mysql连接池配置和案例分析--超时异常和处理
前言: 最近在开发服务的时候, 发现服务只要一段时间不用, 下次首次访问总是失败. 该问题影响虽不大, 但终究影响用户体验. 观察日志后发现, mysql连接因长时间空闲而被关闭, 使用时没有死链检测 ...
- 如何在 Swoole 中优雅的实现 MySQL 连接池
如何在 Swoole 中优雅的实现 MySQL 连接池 一.为什么需要连接池 ? 数据库连接池指的是程序和数据库之间保持一定数量的连接不断开, 并且各个请求的连接可以相互复用, 减少重复连接数据库带来 ...
- JAVA中事物以及连接池
一.事物 什么是事物? 事务,一般是指要做的或所做的事情.在计算机术语中是指访问并可能更新数据库中各种数据项的一个程序执行单元.这些单元要么全都成功,要么全都不成功. 做一件事情,这个一件事情中有多个 ...
- springboot中的那些连接池
hello~各位读者新年好! 回想起前几天在部署springboot项目到正线时,线上环境要求jdk7,可项目是基于jdk8开发的,springboot也是用的springboot2以上的版本,可以说 ...
随机推荐
- 在php代码中执行liunx命令
- 76、django之内置Admin
本篇导航: 配置路由 定制Admin Django内置的Admin是对于model中对应的数据表进行增删改查提供的组件,使用方式有: 依赖APP: django.contrib.auth django ...
- node.js之fs模块
一.fs模块的mkdir函数,创建文件夹 var http = require("http"); var fs = require("fs"); var ser ...
- snprintf 返回值陷阱 重新封装
snprintf()函数用于将格式化的数据写入字符串,其原型为: int snprintf(char *str, int n, char * format [, argument, ...]); st ...
- 【NOIP2014提高组】飞扬的小鸟
https://www.luogu.org/problem/show?pid=1941 从某一点开始飞直到飞出地图最少点击屏幕的次数,显然只和该点的坐标唯一相关,没有后效性,考虑用DP解.令f(i,j ...
- Python的字典
1. Python的字典 1.1. 字典的定义 在Python中,字典是一种key-value的数据类型,也是唯一的映射类型:字典还是另一种可变容器类型,且可存储任意类型对象,其中也可包括其他容器 ...
- 读取 DisplayName和Display(Name='')
public class UserClass { [DisplayName("名称")] //DisplayName public ...
- EGOCache缓存框架具体解说
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/ ...
- Highcharts使用CSV格式数据绘制图表
Highcharts使用CSV格式数据绘制图表 CSV(Comma-Separated Values,逗号分隔值文本格式)是採用逗号切割的纯文本数据.通常情况下.每一个数据之间使用逗号切割,几个相关数 ...
- 利用 Docker 备份、迁移数据库
原文地址:https://zeeko.1503.run/Article/17 最近在把腾讯云的国内主机迁移到香港主机,因为之前使用的 MySql 跟 MongoDb 都是基于 Docker 部署的,所 ...