赞
踩
1.1 创建一个maven工程 在pom文件中引入Druid 依赖,和mysql驱动依赖
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>8.0.33</version>
- </dependency>
- <!-- com.alibaba/druid -->
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>druid</artifactId>
- <version>1.2.16</version>
- </dependency>
- /*
- * Copyright (c) 2020, 2023, webrx.cn All rights reserved.
- *
- */
- package org.example.huang1;
-
-
- import com.alibaba.druid.pool.DruidDataSource;
- import com.alibaba.druid.pool.DruidPooledConnection;
-
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
-
- public class D1 {
- public static void main(String[] args) throws SQLException {
- // 创建数据库连接池
- DruidDataSource db=new DruidDataSource();
- // 初始化时创建物理连接的个数 默认为0
- db.setInitialSize(10);
- // 最大连接数量 默认为 8
- db.setMaxActive(50);
- db.setMinIdle(15);
- db.setDriverClassName("com.mysql.cj.jdbc.Driver");
- db.setUrl("jdbc:mysql:/mydb");
- db.setUsername("root");
- db.setPassword("");
- DruidPooledConnection c1 = db.getConnection();
- DruidPooledConnection c2 = db.getConnection();
- System.out.println(c1==c2);
- // 利用数据库连接池 查询语句
- PreparedStatement ps = c1.prepareStatement("select * from emp");
- ResultSet rs = ps.executeQuery();
- while (rs.next()){
- String s2=rs.getString(2);
- System.out.println(s2);
- }
-
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。