druid1.2.8源码阅读:第一天
文章目录
一、背景
2022年5月7日,看到秦老师在群里发消息说要组织源码活动。我之前没有源码阅读的经历,为了让自己也具备源码阅读的经验,积极的参与的这次活动。
二、在本地启动MySQL,并简单测试JDBC
2.1 在本地启动MySQL
我使用的是macOS系统,本地启动MySQL的命令如下:
/usr/local/mysql
$ ./support-files/mysql.server stop
$ ./support-files/mysql.server start
$ ./support-files/mysql.server restart
创建测试库和用户:
$ mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.16 MySQL Community Server - GPL
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> create database druid_test_db;
Query OK, 1 row affected (0.01 sec)
mysql> create user 'druid_test' identified with mysql_native_password by 'druid_test';
Query OK, 0 rows affected (0.01 sec)
mysql> grant all privileges on druid_test_db.* to druid_test;
Query OK, 0 rows affected (0.00 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
测试druid_test
用户:
$ mysql -udruid_test -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.16 MySQL Community Server - GPL
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| druid_test_db |
| information_schema |
+--------------------+
2 rows in set (0.03 sec)
create table druid_test_db.t_product_item
(
pi_id bigint(20) primary key AUTO_INCREMENT,
product_name varchar(128) not null COMMENT '产品名称',
product_version varchar(128) not null COMMENT '产品版本'
)engine=InnoDB DEFAULT CHARSET=utf8mb4;
2.2 JDK11 之后没有jre文件夹,手动生成jre文件夹
$ sudo bin/jlink --module-path jmods --add-modules java.desktop --output jre
2.3 简单测试JDBC
JDBC的步骤如下:
-
第一步:加载JDBC驱动
Class.forName("com.mysql.cj.jdbc.Driver");
-
第二步:创建数据库连接
Connection con = DriverManager.getConnection(MyDBConf.URL, MyDBConf.USERNAME, MyDBConf.PASSWORD);
-
第三步:获取Statement, 用于执行SQL语句
PreparedStatement stat = con.prepareStatement("insert into druid_test_db.t_product_item(product_name, product_version) values (?, ?)");
-
第四步:如果有返回值,从ResultSet取出返回值
ResultSet resultSet = statement.executeQuery();
测试插入数据:
@