数据库连接池(C3P0、Druid的使用)java

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介: 数据库连接池(C3P0、Druid的使用)java

数据库连接池(C3P0、Druid的使用)java


数据库连接池

项目中肯定会使用数据库连接池

项目中一般使用druid

C3P0

基本用法

package com.qfedu.c3p0;
import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class App {
  public static void main(String[] args) throws Exception {
    // TODO Auto-generated method stub
//    test1();
    test2();
  }
  public static void test1() throws Exception {
    // 创建c3p0的数据库池连接对象
    ComboPooledDataSource dataSource = new ComboPooledDataSource();
    // 配置
    dataSource.setDriverClass("com.mysql.cj.jdbc.Driver");
    dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/db2210?serverTimezone=Asia/Shanghai&useSSL=false");
    dataSource.setUser("root");
    dataSource.setPassword("root");
    // 初始连接数
    dataSource.setInitialPoolSize(3);
    // 最大连接数
    dataSource.setMaxPoolSize(10);
    // 最大空闲时间,单位毫秒
    dataSource.setMaxIdleTime(2000);
    // 通过连接池获取连接对象
    Connection connection = dataSource.getConnection();
    System.out.println(connection);
    // 将连接对象还回连接池
    connection.close();
  }
  /**
   * 读取c3p0的配置文件
   * 正常的url jdbc:mysql://localhost:3306/db2210?serverTimezone=Asia/Shanghai&useSSL=false
   * &在xml中属于特殊符号,所以在xml中需要使用 实体符号 &替换
   * <property name="jdbcUrl">jdbc:mysql://localhost:3306/db2210?serverTimezone=Asia/Shanghai&amp;userSSL=false</property>
   * 
   * @throws Exception
   */
  public static void test2() throws Exception {
    // 自动读取src下的c3p0-config.xml
    // 默认加载<default-config>中的配置
    ComboPooledDataSource dataSource = new ComboPooledDataSource();
    // 获取连接对象
    Connection connection = dataSource.getConnection();
    System.out.println(connection);
    // “关闭”连接对象,将连接对象还回连接池
    connection.close();
  }
}
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
  <default-config>
     <property name="driverClass">com.mysql.cj.jdbc.Driver</property>
     <property name="jdbcUrl">jdbc:mysql://localhost:3306/db2210?serverTimezone=Asia/Shanghai&amp;useSSL=false</property>
     <property name="user">root</property>
     <property name="password">root</property>
     <property name="initialPoolSize">3</property>
     <property name="maxPoolSize">6</property>
     <property name="maxIdleTime">2000</property>
  </default-config>
  <named-config name="myconfig">
     <property name="driverClass">com.mysql.jdbc.Driver</property>
     <property name="jdbcUrl">jdbc:mysql:///supermarket</property>
     <property name="user">root</property>
     <property name="password">root</property>
     <property name="initialPoolSize">3</property>
     <property name="maxPoolSize">6</property>
     <property name="maxIdleTime">2000</property>
  </named-config>
</c3p0-config>

结合DBUtils

package com.qfedu.utils;
import org.apache.commons.dbutils.QueryRunner;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class C3P0Utils {
  // ComboPooledDataSource 实现 jdbc中的DataSource接口
  private static ComboPooledDataSource dataSource = null;
  // 创建连接池对象
  static {
    dataSource = new ComboPooledDataSource();
  }
  public static QueryRunner getQueryRunner() {
    // javax.sql.DataSource
    // QueryRunner(DataSource ds)
    // 将DBUtils和数据库连接池产生了关联
    // QueryRunner内部维护连接对象
    return new QueryRunner(dataSource);
  }
}
public static void test2() throws Exception {
    QueryRunner qr = C3P0Utils.getQueryRunner();
    String sql = "select count(*) from t_person";
    // QueryRunner内部维护连接对象,调用query,不需要connection参数
    // 执行完,也不需要手动关闭connection对象
    Long count = qr.query(sql, new ScalarHandler<Long>());
    System.out.println(count);
  }

Druid

基本用法

package com.qfedu.druid;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.qfedu.utils.DruidUtils;
public class App {
  public static void main(String[] args) throws Exception {
    // TODO Auto-generated method stub
//    test1();
    test2();
  }
  public static void test1() throws Exception {
    // 通过Properties加载properties文件中内容
    FileInputStream inputStream = new FileInputStream("src/druid.properties");
    Properties prop = new Properties();
    prop.load(inputStream);
    // 获取连接池对象
    // javax.sql.DataSource
    DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
    Connection connection = dataSource.getConnection();
    System.out.println(connection);
    connection.close();
  }
  public static void test2() throws Exception {
    QueryRunner queryRunner = DruidUtils.getQueryRunner();
    String sql = "select count(*) from t_person";
    Long count = queryRunner.query(sql, new ScalarHandler<Long>());
    System.out.println(count);
  }
}
# \u6587\u4EF6\u540D druid.properties \u5B58\u50A8\u5728src\u76EE\u5F55\u4E0B
driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/db2210?serverTimezone=Asia/Shanghai&useSSL=true
username=root
password=root
# \u521D\u59CB\u5316\u6570\u636E\u5E93\u8FDE\u63A5\u6C60\u4E2D\u8FDE\u63A5\u4E2A\u6570
initialSize=5
# \u6700\u5927\u4E2A\u6570
maxActive=20
# TimeOut \u7B49\u5F85\u8D85\u65F6\u65F6\u95F4
maxWait=2000

结合DButils

package com.qfedu.utils;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.commons.dbutils.QueryRunner;
import com.alibaba.druid.pool.DruidDataSourceFactory;
public class DruidUtils {
  private static DataSource dataSource = null;
  static {
    try {
      FileInputStream inputStream = new FileInputStream("src/druid.properties");
      Properties prop = new Properties();
      prop.load(inputStream);
      // 创建连接池对象
      dataSource = DruidDataSourceFactory.createDataSource(prop);
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
  /**
   * 创建QueryRunner对象,一定要注意,参数使用连接池对象
   * @return
   */
  public static QueryRunner getQueryRunner() {
    return new QueryRunner(dataSource);
  }
}

Web服务器

C/S Client/Server

B/S Browser/Server 浏览器/服务端

借助Http协议

Http协议介绍 了解

请求-响应协议

浏览器端发送http请求,服务端根据请求,向浏览器端返回响应的数据

请求

请求行 请求头 请求体(post)

提交方式:主要get post

get方式将提交的数据放在url,不安全;post方式将提交的数据放在请求体中

get方法提交的数据大小有限制;post方式提交的数据,大小无限制

get方式相对快,缓存静态资源

get一般用于查询,post一般用于修改和添加

响应

响应行 响应头 响应体

响应状态:200 302 404 500

Tomcat基本介绍

更适合中小项目

使用tomcat,一定要配置 JAVA_HOME环境变量

验证:bin下面运行startup.bat ,启动tomcat服务器

浏览器中 http://localhost:8080 验证是否可以正常访问

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
1天前
|
SQL Java 数据库连接
java使用jdbc操作数据库
java使用jdbc操作数据库
|
1天前
|
SQL druid Java
java mysql druid mybatis-plus里使用多表删除出错的一种处理方式
java mysql druid mybatis-plus里使用多表删除出错的一种处理方式
10 0
|
1天前
|
Oracle 关系型数据库 Java
java操作多数据源将oracle数据同步达梦数据库
java操作多数据源将oracle数据同步达梦数据库
|
1天前
|
SQL Java 数据库连接
JDBC Java标准库提供的一些api(类+方法) 统一各种数据库提供的api
JDBC Java标准库提供的一些api(类+方法) 统一各种数据库提供的api
10 0
|
1天前
|
SQL Java 数据库连接
Java数据库编程实践:连接与操作数据库
Java数据库编程实践:连接与操作数据库
11 0
|
1天前
|
SQL Java 数据库连接
17:数据库连接池与Servlet整合-Java Web
17:数据库连接池与Servlet整合-Java Web
23 3
|
1天前
|
Java 关系型数据库 测试技术
Java代码一键生成数据库文档(案例详解)
Screw是一个自动化数据库文档生成工具,能根据数据库表结构快速生成简洁、多格式(HTML、Word、Markdown)的文档,支持MySQL、MariaDB等多数据库。它使用Freemarker模板,允许用户自定义样式。依赖包括HikariCP数据库连接池和对应JDBC驱动。通过在Java代码或Maven插件中配置,可方便生成文档。示例代码展示了如何在测试用例中使用Screw。文档效果依赖于数据库中的表和字段注释。
|
1天前
|
Java 关系型数据库 MySQL
【JDBC编程】基于MySql的Java应用程序中访问数据库与交互数据的技术
【JDBC编程】基于MySql的Java应用程序中访问数据库与交互数据的技术
|
1天前
|
Java 数据库
Java实现超市管理系统(含数据库)
Java实现超市管理系统(含数据库)
|
1天前
|
SQL Java 数据库连接
Java从入门到精通:2.3.1数据库编程——学习JDBC技术,掌握Java与数据库的交互
ava从入门到精通:2.3.1数据库编程——学习JDBC技术,掌握Java与数据库的交互