• 数据库连接池(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中需要使用 实体符号 &替换
    	 * jdbc:mysql://localhost:3306/db2210?serverTimezone=Asia/Shanghai&userSSL=false
    	 * 
    	 * @throws Exception
    	 */
    	public static void test2() throws Exception {
    		// 自动读取src下的c3p0-config.xml
    		// 默认加载中的配置
    		ComboPooledDataSource dataSource = new ComboPooledDataSource();
    		
    		// 获取连接对象
    		Connection connection = dataSource.getConnection();
    		System.out.println(connection);
    		
    		// “关闭”连接对象,将连接对象还回连接池
    		connection.close();
    	}
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    
    <c3p0-config>
    	<default-config>
    		 <property name="driverClass">com.mysql.cj.jdbc.Driverproperty>
    		 <property name="jdbcUrl">jdbc:mysql://localhost:3306/db2210?serverTimezone=Asia/Shanghai&useSSL=falseproperty>
    		 <property name="user">rootproperty>
    		 <property name="password">rootproperty>
    		 
    		 <property name="initialPoolSize">3property>
    		 <property name="maxPoolSize">6property>
    		 <property name="maxIdleTime">2000property>
    	default-config>
    	
    	<named-config name="myconfig">
    		 <property name="driverClass">com.mysql.jdbc.Driverproperty>
    		 <property name="jdbcUrl">jdbc:mysql:///supermarketproperty>
    		 <property name="user">rootproperty>
    		 <property name="password">rootproperty>
    		 
    		 <property name="initialPoolSize">3property>
    		 <property name="maxPoolSize">6property>
    		 <property name="maxIdleTime">2000property>
    	
    	named-config>
    	
    c3p0-config>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    结合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);
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    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);
    	
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    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);
    	}
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    # \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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    结合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);
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    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 验证是否可以正常访问

  • 相关阅读:
    python基础语法快速复习
    [go]golang中“var“与“:=“的区别
    MySQL如何对SQL做prepare预处理(解决IN查询SQL预处理仅能查询出一条记录的问题)
    7、架构-架构的安全性
    08 图形学——Bezier曲线与曲面
    【HMS Core】构建SplitBill应用集成多个HMS Core服务,助力您更好的了解华为生态组成
    el-input一些校验 & 事件
    XML的使用
    Java之JavaConfig
    服装行业如何做数字化转型?
  • 原文地址:https://blog.csdn.net/weixin_51423778/article/details/126812838