mysql tomcat连接池_tomcat中使用mysql连接池的配置

本文详细介绍了如何在Tomcat中配置MySQL连接池,包括下载必要的jar包,创建context.xml配置文件,设置my.ini,修改web.xml以及展示Java代码示例来连接和操作数据库。此外,还提及了Python中实现MySQL连接池的方法。
摘要由CSDN通过智能技术生成

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文件输入如下内容:

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"/>

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,添加如下内容

Connection Pool

jdbc/Mysql

javax.sql.DataSource

Container

Shareable

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;

}

}

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文件中配置以下内容

Tomcat配置MySql连接池问题

配置过程如下: 1.修改Tomcat—>conf目录下的context.xml文件

classfication中使用图像金字塔和sliding windows提高准确率

之前对imagenet的预训模型进行finetune,找出了很多样本选择时的注意事项,当时在测试如下这张照片时,效果不好,我认为是物体过小造成的,因此尝试使用图像金字塔的方法: 当时结果如下: 一开始 ...

src引入js时添加时间戳参数方法

代码如下:

微信小程序: rpx与px,rem相互转换

官方上规定屏幕宽度为20rem,规定屏幕宽为750rpx,则1rem=750/20rpx. 微信官方建议视觉稿以iPhone 6为标准:在 iPhone6 上,屏幕宽度为375px,共有750个物理像 ...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值