xerga 2 år sedan
förälder
incheckning
967d0c09d4

+ 8 - 0
02_MySql/.idea/.gitignore

@@ -0,0 +1,8 @@
+# Default ignored files
+/shelf/
+/workspace.xml
+# Datasource local storage ignored files
+/dataSources/
+/dataSources.local.xml
+# Editor-based HTTP Client requests
+/httpRequests/

+ 6 - 0
02_MySql/.idea/encodings.xml

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="Encoding" defaultCharsetForPropertiesFiles="UTF-8">
+    <file url="PROJECT" charset="UTF-8" />
+  </component>
+</project>

+ 10 - 0
02_MySql/.idea/libraries/libs.xml

@@ -0,0 +1,10 @@
+<component name="libraryTable">
+  <library name="libs">
+    <CLASSES>
+      <root url="file://$PROJECT_DIR$/jdbc-day01/libs" />
+    </CLASSES>
+    <JAVADOC />
+    <SOURCES />
+    <jarDirectory url="file://$PROJECT_DIR$/jdbc-day01/libs" recursive="false" />
+  </library>
+</component>

+ 6 - 0
02_MySql/.idea/misc.xml

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
+    <output url="file://$PROJECT_DIR$/out" />
+  </component>
+</project>

+ 9 - 0
02_MySql/.idea/modules.xml

@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="ProjectModuleManager">
+    <modules>
+      <module fileurl="file://$PROJECT_DIR$/02_MySql.iml" filepath="$PROJECT_DIR$/02_MySql.iml" />
+      <module fileurl="file://$PROJECT_DIR$/jdbc-day01/jdbc-day01.iml" filepath="$PROJECT_DIR$/jdbc-day01/jdbc-day01.iml" />
+    </modules>
+  </component>
+</project>

+ 10 - 0
02_MySql/.idea/runConfigurations.xml

@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="RunConfigurationProducerService">
+    <option name="ignoredProducers">
+      <set>
+        <option value="com.android.tools.idea.compose.preview.runconfiguration.ComposePreviewRunConfigurationProducer" />
+      </set>
+    </option>
+  </component>
+</project>

+ 6 - 0
02_MySql/.idea/vcs.xml

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="VcsDirectoryMappings">
+    <mapping directory="$PROJECT_DIR$/.." vcs="Git" />
+  </component>
+</project>

+ 11 - 0
02_MySql/02_MySql.iml

@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<module type="JAVA_MODULE" version="4">
+  <component name="NewModuleRootManager" inherit-compiler-output="true">
+    <exclude-output />
+    <content url="file://$MODULE_DIR$">
+      <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
+    </content>
+    <orderEntry type="inheritedJdk" />
+    <orderEntry type="sourceFolder" forTests="false" />
+  </component>
+</module>

+ 22 - 0
02_MySql/jdbc-day01/jdbc-day01.iml

@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<module type="JAVA_MODULE" version="4">
+  <component name="NewModuleRootManager" inherit-compiler-output="true">
+    <exclude-output />
+    <content url="file://$MODULE_DIR$">
+      <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
+    </content>
+    <orderEntry type="inheritedJdk" />
+    <orderEntry type="sourceFolder" forTests="false" />
+    <orderEntry type="module-library">
+      <library name="JUnit4">
+        <CLASSES>
+          <root url="jar://$MAVEN_REPOSITORY$/junit/junit/4.13.1/junit-4.13.1.jar!/" />
+          <root url="jar://$MAVEN_REPOSITORY$/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar!/" />
+        </CLASSES>
+        <JAVADOC />
+        <SOURCES />
+      </library>
+    </orderEntry>
+    <orderEntry type="library" name="libs" level="project" />
+  </component>
+</module>

BIN
02_MySql/jdbc-day01/libs/druid-1.1.21.jar


BIN
02_MySql/jdbc-day01/libs/mysql-connector-java-5.1.36-bin.jar


+ 152 - 0
02_MySql/jdbc-day01/src/com/lovecoding/jdbc/TestJdbc01.java

@@ -0,0 +1,152 @@
+package com.lovecoding.jdbc;
+
+import org.junit.Test;
+
+import java.sql.*;
+
+public class TestJdbc01 {
+
+    /*
+        # 设置事务 手动
+        set autocommit = 0;
+
+        # 地区
+        update t_area  set areaname =  'aa' where id = 1;
+
+        # 提交之后改变
+        commit;
+     */
+
+    // 查询部门表
+    @Test
+    public void test01 () throws ClassNotFoundException, SQLException {
+        //连接mysql
+
+        //1 加载驱动
+        Class.forName("com.mysql.jdbc.Driver"); //8.0 需要加cj
+        //2 创建连接  需要指定 端口 地址  用户名 密码
+        //public static Connection getConnection(String url,String user, String password)
+        //localhost 数据库地址 3306 端口 lovecoding 数据库 8.0之后serverTimezone=UTC
+        String url = "jdbc:mysql://localhost:3306/lovecoding?serverTimezone=UTC";
+        //网络编程
+        Connection connection = DriverManager.getConnection(url, "root", "root");
+        //3 获取操作sql对象
+        Statement statement = connection.createStatement();
+        //4 编写sql
+        String sql = " select * from t_area  ";
+        //5 执行sql
+        ResultSet resultSet = statement.executeQuery(sql);
+        //6 遍历结果
+        while (resultSet.next()){
+            //根据索引获取 索引从1 开始
+            String id = resultSet.getString(1);
+            String name = resultSet.getString(2);
+            System.out.println(id+"-"+name);
+        }
+
+        //关闭资源
+        resultSet.close();
+        statement.close();
+        connection.close();
+    }
+
+    /**
+     * 添加数据
+     * @throws ClassNotFoundException
+     * @throws SQLException
+     */
+    @Test
+    public void test02 () throws Exception {
+        //连接mysql
+        //1 加载驱动
+        Class.forName("com.mysql.jdbc.Driver"); //8.0 需要加cj
+        //2 创建连接  需要指定 端口 地址  用户名 密码
+        String url = "jdbc:mysql://localhost:3306/lovecoding?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC&useSSL=false";
+        //网络编程
+        Connection connection = DriverManager.getConnection(url, "root", "root");
+        //3 获取操作sql对象
+        Statement statement = connection.createStatement();
+        //4 编写sql 添加
+        String sql = " INSERT INTO `lovecoding`.`t_employee` " +
+                "( `ename`, `salary`, `commission_pct`, `birthday`,  `tel`, `email`, `address`,  " +
+                "`hiredate`, `job_id`, `mid`, `did`) " +
+                "VALUES ( '牛世恒', 12000, 0.65, '1998-10-08', '13789098765', 'shl@lovecoding', '北京',  " +
+                "'2021-07-28', 1, 1, 1);\n ";
+        //5 执行sql
+        int i = statement.executeUpdate(sql);
+        //6 判断
+        if (i > 0) {
+            System.out.println("执行成功");
+        }else{
+            System.out.println("执行失败");
+        }
+
+        //关闭资源
+        statement.close();
+        connection.close();
+    }
+
+    /**
+     * 修改数据
+     * @throws ClassNotFoundException
+     * @throws SQLException
+     */
+    @Test
+    public void test03 () throws Exception {
+        //连接mysql
+        //1 加载驱动
+        Class.forName("com.mysql.jdbc.Driver"); //8.0 需要加cj
+        //2 创建连接  需要指定 端口 地址  用户名 密码
+        String url = "jdbc:mysql://localhost:3306/lovecoding?serverTimezone=UTC";
+        //网络编程
+        Connection connection = DriverManager.getConnection(url, "root", "root");
+        //3 获取操作sql对象
+        Statement statement = connection.createStatement();
+        //4 编写sql 修改
+        String sql = " UPDATE `lovecoding`.`t_employee` SET `ename` = 'zs' WHERE `eid` = 29;\n ";
+        //5 执行sql
+        int i = statement.executeUpdate(sql);
+        //6 判断
+        if (i > 0) {
+            System.out.println("执行成功");
+        }else{
+            System.out.println("执行失败");
+        }
+
+        //关闭资源
+        statement.close();
+        connection.close();
+    }
+
+
+    /**
+     * 删除数据
+     * @throws ClassNotFoundException
+     * @throws SQLException
+     */
+    @Test
+    public void test04 () throws Exception {
+        //连接mysql
+        //1 加载驱动
+        Class.forName("com.mysql.jdbc.Driver"); //8.0 需要加cj
+        //2 创建连接  需要指定 端口 地址  用户名 密码
+        String url = "jdbc:mysql://localhost:3306/lovecoding?serverTimezone=UTC";
+        //网络编程
+        Connection connection = DriverManager.getConnection(url, "root", "root");
+        //3 获取操作sql对象
+        Statement statement = connection.createStatement();
+        //4 编写sql 删除
+        String sql = " delete from  `lovecoding`.`t_employee`  WHERE `eid` = 29;\n ";
+        //5 执行sql
+        int i = statement.executeUpdate(sql);
+        //6 判断
+        if (i > 0) {
+            System.out.println("执行成功");
+        }else{
+            System.out.println("执行失败");
+        }
+        //关闭资源
+        statement.close();
+        connection.close();
+    }
+}

+ 126 - 0
02_MySql/jdbc-day01/src/com/lovecoding/jdbc/TestJdbc02.java

@@ -0,0 +1,126 @@
+package com.lovecoding.jdbc;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import java.sql.*;
+
+public class TestJdbc02 {
+
+    //声明对象
+    Statement statement = null;
+    //连接对象
+    Connection connection = null;
+
+    @Before
+    public void before() throws ClassNotFoundException, SQLException {
+        //连接mysql
+        //1 加载驱动
+        Class.forName("com.mysql.jdbc.Driver"); //8.0 需要加cj
+        //2 创建连接  需要指定 端口 地址  用户名 密码
+        //public static Connection getConnection(String url,String user, String password)
+        //localhost 数据库地址 3306 端口 lovecoding 数据库 8.0之后serverTimezone=UTC
+        String url = "jdbc:mysql://localhost:3306/lovecoding?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC&useSSL=false";
+        //网络编程
+        connection = DriverManager.getConnection(url, "root", "root");
+        //3 获取操作sql对象
+        statement = connection.createStatement();
+    }
+
+    // 查询部门表
+    @Test
+    public void test01 () throws ClassNotFoundException, SQLException {
+
+        //4 编写sql
+        String sql = " select * from t_area  ";
+        //5 执行sql
+        ResultSet resultSet = statement.executeQuery(sql);
+        //6 遍历结果
+        while (resultSet.next()){
+            //根据索引获取 索引从1 开始
+            String id = resultSet.getString(1);
+            String name = resultSet.getString(2);
+            System.out.println(id+"-"+name);
+        }
+        //关闭资源
+        resultSet.close();
+        statement.close();
+        connection.close();
+    }
+
+    /**
+     * 添加数据
+     * @throws ClassNotFoundException
+     * @throws SQLException
+     */
+    @Test
+    public void test02 () throws Exception {
+
+        //4 编写sql 添加
+        String sql = " INSERT INTO `lovecoding`.`t_employee` " +
+                "( `ename`, `salary`, `commission_pct`, `birthday`,  `tel`, `email`, `address`,  " +
+                "`hiredate`, `job_id`, `mid`, `did`) " +
+                "VALUES ( '白家起', 12000, 0.65, '1998-10-08', '13789098765', 'shl@lovecoding', '北京',  " +
+                "'2021-07-28', 1, 1, 1);\n ";
+        //5 执行sql
+        int i = statement.executeUpdate(sql);
+        //6 判断
+        if (i > 0) {
+            System.out.println("执行成功");
+        }else{
+            System.out.println("执行失败");
+        }
+
+        //关闭资源
+        statement.close();
+        connection.close();
+    }
+
+    /**
+     * 修改数据
+     * @throws ClassNotFoundException
+     * @throws SQLException
+     */
+    @Test
+    public void test03 () throws Exception {
+
+        //4 编写sql 修改
+        String sql = " UPDATE `lovecoding`.`t_employee` SET `ename` = 'zs' WHERE `eid` = 29;\n ";
+        //5 执行sql
+        int i = statement.executeUpdate(sql);
+        //6 判断
+        if (i > 0) {
+            System.out.println("执行成功");
+        }else{
+            System.out.println("执行失败");
+        }
+
+        //关闭资源
+        statement.close();
+        connection.close();
+    }
+
+
+    /**
+     * 删除数据
+     * @throws ClassNotFoundException
+     * @throws SQLException
+     */
+    @Test
+    public void test04 () throws Exception {
+
+        //4 编写sql 删除
+        String sql = " delete from  `lovecoding`.`t_employee`  WHERE `eid` = 31; ";
+        //5 执行sql
+        int i = statement.executeUpdate(sql);
+        //6 判断
+        if (i > 0) {
+            System.out.println("执行成功");
+        }else{
+            System.out.println("执行失败");
+        }
+        //关闭资源
+        statement.close();
+        connection.close();
+    }
+}

+ 318 - 0
02_MySql/jdbc-day01/src/com/lovecoding/jdbc/TestJdbc03.java

@@ -0,0 +1,318 @@
+package com.lovecoding.jdbc;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import java.sql.*;
+import java.util.Scanner;
+
+public class TestJdbc03 {
+
+
+    //连接对象
+    Connection connection = null;
+
+    Statement statement = null;
+
+    @Before
+    public void before() throws ClassNotFoundException, SQLException {
+        //连接mysql
+        //1 加载驱动
+        Class.forName("com.mysql.jdbc.Driver"); //8.0 需要加cj
+        //2 创建连接  需要指定 端口 地址  用户名 密码
+        //public static Connection getConnection(String url,String user, String password)
+        //localhost 数据库地址 3306 端口 lovecoding 数据库 8.0之后serverTimezone=UTC
+        String url = "jdbc:mysql://localhost:3306/lovecoding?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC&rewriteBatchedStatements=true&useSSL=false";
+        //网络编程
+        connection = DriverManager.getConnection(url, "root", "root");
+
+        statement = connection.createStatement();
+
+    }
+
+    // sql注入问题 万能密码
+    // 批处理
+    // 事务
+    @Test
+    public void test01() throws Exception {
+        // sql拼接
+        String ename = "李四";
+        double salary = 15000;//15000
+        String birthday = "1990-1-1";//1990-1-1
+        String tel = "13578595685";//13578595685
+        String email = "'zhangsan,','1'";//'zhangsan,','1'
+        //sql执行对象
+        //4 编写sql 添加
+        // 原因1 需要加单引号  2 hiredate 非空 需要给值
+        String sql = " INSERT INTO `lovecoding`.`t_employee` ( ename, salary, birthday, tel, email) VALUES ( '"+ename+"' , '"+salary+"' , '"+birthday+"' , '"+tel+"' ,'"+email+"' ); ";
+        // VALUES ( '李四' , 15000 , '1990-1-1' , '13578595685' ,''zhangsan,','1'' );
+        int i = statement.executeUpdate(sql);
+        //6 判断
+        if (i > 0) {
+            System.out.println("执行成功");
+        }else{
+            System.out.println("执行失败");
+        }
+        //关闭资源
+        statement.close();
+        connection.close();
+
+    }
+
+    //SQL拼接
+    @Test
+    public void test02() throws Exception {
+        // sql拼接
+        String ename = "李四";
+        double salary = 15000;//15000
+        String birthday = "1990-1-1";//1990-1-1
+        String tel = "13578595685";//13578595685
+        String email = "'zhangsan,','1'";//'zhangsan,','1'
+        //sql执行对象
+        //4 编写sql 添加
+        // 原因1 需要加单引号  2 hiredate 非空 需要给值
+        String sql = " INSERT INTO `lovecoding`.`t_employee` " +
+                " ( ename, salary, birthday, tel, email) " +
+                " VALUES ( ? , ? , ? , ? , ? ); ";
+
+        //PreparedStatement 可解决sql拼接问题
+        //'zhangsan,','1'
+        // Statement 拼接 sql
+        // PreparedStatement 解析?  setString参数字符串去处理。
+        PreparedStatement preparedStatement = connection.prepareStatement(sql);
+
+        //注入参数 ?
+        // 索引从 1 开始
+        preparedStatement.setString(1,ename);
+        preparedStatement.setDouble(2,salary);
+        preparedStatement.setString(3,birthday);
+        preparedStatement.setString(4,tel);
+        preparedStatement.setString(5,email);
+
+        int i = preparedStatement.executeUpdate();
+        //6 判断
+        if (i > 0) {
+            System.out.println("执行成功");
+        }else{
+            System.out.println("执行失败");
+        }
+        //关闭资源
+        preparedStatement.close();
+        connection.close();
+
+    }
+
+    // 解决
+    // PreparedStatement
+    // 设置 参数 替换 拼接的sql
+    // 参数 用 ? 索引  设置 ? 的值。
+
+    //sql注入
+    //select * from t_employee where eid = 值;
+    @Test
+    public void test03() throws Exception {
+        String eid = "35 or 1=1"; // 35 or 1=1 万能密码  select where name=? ,password = ?  or 1=1
+        // " select * from t_employee where eid = 35 or 1=1 "
+        String sql = " select * from t_employee where eid =  "+ eid;
+        //执行sql
+        ResultSet rs = statement.executeQuery(sql);
+        //遍历
+        while (rs.next()){
+            String id = rs.getString(1);
+            String ename = rs.getString(2);
+
+            System.out.println(id + "-" +ename);
+        }
+
+        rs.close();
+        statement.close();
+        connection.close();
+
+    }
+
+    @Test
+    public void test04() throws Exception {
+        String eid = "a34or 1=1";
+        // " select * from t_employee where eid = '35 or 1=1' "
+        String sql = " select * from t_employee where eid = ? "; //34or 1=1 转换数字 数字直接转 34
+        //执行sql
+        PreparedStatement preparedStatement = connection.prepareStatement(sql);
+
+        //preparedStatement设置值
+
+        preparedStatement.setString(1,eid);
+        //执行查询
+        ResultSet rs = preparedStatement.executeQuery();
+        //遍历
+        while (rs.next()){
+            String id = rs.getString(1);
+            String ename = rs.getString(2);
+
+            System.out.println(id + "-" +ename);
+        }
+
+        rs.close();
+        statement.close();
+        connection.close();
+
+    }
+
+    //获取自增长键值
+    @Test
+    public void test05() throws Exception {
+        // sql拼接
+        String ename = "李四1";
+        double salary = 15000;//15000
+        String birthday = "1990-1-1";//1990-1-1
+        String tel = "13578595685";//13578595685
+        String email = "'zhangsan,','1'";//'zhangsan,','1'
+        //sql执行对象
+        //4 编写sql 添加
+        // 原因1 需要加单引号  2 hiredate 非空 需要给值
+        String sql = " INSERT INTO `lovecoding`.`t_employee` " +
+                " ( ename, salary, birthday, tel, email) " +
+                " VALUES ( ? , ? , ? , ? , ? ); ";
+        //PreparedStatement
+        //获取自增主键参数 Statement.RETURN_GENERATED_KEYS
+        //getGeneratedKeys();
+        PreparedStatement preparedStatement = connection.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
+        //注入参数
+        preparedStatement.setString(1,ename);
+        preparedStatement.setDouble(2,salary);
+        preparedStatement.setString(3,birthday);
+        preparedStatement.setString(4,tel);
+        preparedStatement.setString(5,email);
+
+        int i = preparedStatement.executeUpdate();
+        //自增主键
+        ResultSet generatedKeys = preparedStatement.getGeneratedKeys();
+        while (generatedKeys.next()){
+            Object id = generatedKeys.getObject(1);
+            System.out.println(id);
+        }
+
+        //6 判断
+        if (i > 0) {
+            System.out.println("执行成功");
+        }else{
+            System.out.println("执行失败");
+        }
+        //关闭资源
+        preparedStatement.close();
+        connection.close();
+
+    }
+
+    //通过 preparedStatement 完成 添加或者修改。
+
+    //批处理(了解)
+    @Test
+    public void test06() throws Exception {
+        //添加1000记录
+        long l1 = System.currentTimeMillis();
+        // sql拼接
+        String ename = "测试用户";
+        double salary = 15000;//15000
+        String birthday = "1990-1-1";//1990-1-1
+        String tel = "13578595685";//13578595685
+        String email = "'zhangsan,','1'";//'zhangsan,','1'
+        //sql执行对象
+        //4 编写sql 添加
+        // 原因1 需要加单引号  2 hiredate 非空 需要给值
+        String sql = " INSERT INTO `lovecoding`.`t_employee` " +
+                " ( ename, salary, birthday, tel, email) " +
+                " VALUES ( ? , ? , ? , ? , ? ); ";
+
+        PreparedStatement preparedStatement = null;
+        for (int i = 0; i < 1000; i++) {
+
+            preparedStatement = connection.prepareStatement(sql);
+            //注入参数
+            preparedStatement.setString(1,ename+i);
+            preparedStatement.setDouble(2,salary);
+            preparedStatement.setString(3,birthday);
+            preparedStatement.setString(4,tel);
+            preparedStatement.setString(5,email);
+            //执行
+            preparedStatement.executeUpdate();
+        }
+
+        long l2 = System.currentTimeMillis();
+        System.out.println(l2-l1); //736
+        //关闭资源
+        preparedStatement.close();
+        connection.close();
+    }
+
+    //连接参数 在url后面再加一个参数 rewriteBatchedStatements=true
+    //添加到批处理 addBatch()
+    // INSERT INTO 表名 values(值0,值1)
+    // 分号 去掉;
+    @Test
+    public void test07() throws Exception {
+        //添加1000记录
+        long l1 = System.currentTimeMillis();
+        // sql拼接
+        String dname = "批处理部门";
+        String desc = "部门介绍";
+        //sql执行对象
+        //4 编写sql 添加
+        String sql = " INSERT INTO `lovecoding`.`t_department`  VALUES ( null , ? , ?  ) ";
+
+        PreparedStatement  preparedStatement = connection.prepareStatement(sql);
+
+        for (int i = 0; i < 1000; i++) {
+            //注入参数
+            preparedStatement.setObject(1,dname+i);
+            preparedStatement.setObject(2,desc);
+            //添加
+            preparedStatement.addBatch();
+        }
+        //整体执行
+        preparedStatement.executeBatch();
+
+        long l2 = System.currentTimeMillis();
+        System.out.println(l2-l1); // 31
+        //关闭资源
+        preparedStatement.close();
+        connection.close();
+    }
+
+
+    //事务
+    /*
+    update t_department set description = 'xx' where did = 2;
+	update t_department set description = 'yy' where did = 3;
+
+	故意把其中一条sql语句写错。
+
+    update t_department set description = 'xx' where did = 2;
+	update t_department set description = 'yy' what did = 3;  #what是错误的
+     */
+    @Test
+    public void test08() throws Exception {
+        //sql对象
+        Statement statement = connection.createStatement();
+        //转账
+        String sql1 = " update t_department set description = 'aa' where did = 2 ";
+        String sql2 = " update t_department set description = 'bb' wher did = 3 ";
+
+        //手动事务
+        connection.setAutoCommit(false);
+        try {
+            statement.executeUpdate(sql1);
+            statement.executeUpdate(sql2);
+            //正常提交代码
+            connection.commit();
+        } catch (SQLException e) {
+            //回滚
+            connection.rollback();
+            e.printStackTrace();
+        }
+        //释放资源
+        statement.close();
+        connection.close();
+    }
+
+
+}

+ 80 - 0
02_MySql/jdbc-day01/src/com/lovecoding/jdbc/TestJdbc04.java

@@ -0,0 +1,80 @@
+package com.lovecoding.jdbc;
+
+import com.alibaba.druid.pool.DruidDataSourceFactory;
+import org.junit.Before;
+import org.junit.Test;
+
+import javax.sql.DataSource;
+import java.io.IOException;
+import java.io.InputStream;
+import java.sql.*;
+import java.util.Properties;
+
+public class TestJdbc04 {
+
+    //如何使用德鲁伊数据库连接池
+    // jar  druid-1.1.21.jar
+    // api 使用
+    // 配置文件 读取配置
+    // DruidDataSourceFactory
+    @Test
+    public void test01() throws Exception {
+        //连接信息
+        InputStream inputStream = TestJdbc04.class.getClassLoader()
+                .getResourceAsStream("druid.properties");
+        //Properties
+        Properties properties = new Properties();
+        //读取配置文件
+        properties.load(inputStream);
+        //连接池
+        DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
+        //获取连接
+        Connection connection = dataSource.getConnection();
+        System.out.println(connection);
+        String sql = " select * from t_department ";
+        PreparedStatement preparedStatement = connection.prepareStatement(sql);
+        ResultSet rs = preparedStatement.executeQuery();
+
+        while (rs.next()){
+            String id = rs.getString(1);
+            String name = rs.getString(2);
+            System.out.println(id +"--" +name);
+        }
+        rs.close();
+        connection.close();
+    }
+
+
+    @Test
+    public void test02() throws Exception {
+        //连接信息
+        InputStream inputStream = TestJdbc04.class.getClassLoader().getResourceAsStream("druid.properties");
+        //Properties
+        Properties properties = new Properties();
+        //读取配置文件
+        properties.load(inputStream);
+        //连接池
+        DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
+        //获取连接
+
+        for (int i = 0; i < 10; i++) {
+            new  Thread( ()->{
+                Connection connection = null;
+                try {
+                    connection = dataSource.getConnection();
+                } catch (SQLException e) {
+                    e.printStackTrace();
+                }
+                System.out.println(connection);
+            } ).start();
+        }
+
+        Connection connection = dataSource.getConnection();
+        System.out.println("第11次:"+connection);
+
+
+    }
+
+
+
+}

+ 8 - 0
02_MySql/jdbc-day01/src/druid.properties

@@ -0,0 +1,8 @@
+#key=value
+driverClassName=com.mysql.jdbc.Driver
+url=jdbc:mysql://localhost:3306/lovecoding?serverTimezone=UTC&rewriteBatchedStatements=true
+username=root
+password=root
+initialSize=5
+maxActive=10
+maxWait=1000

BIN
02_MySql/out/production/jdbc-day01/com/lovecoding/jdbc/TestJdbc01.class


BIN
02_MySql/out/production/jdbc-day01/com/lovecoding/jdbc/TestJdbc02.class


BIN
02_MySql/out/production/jdbc-day01/com/lovecoding/jdbc/TestJdbc03.class


BIN
02_MySql/out/production/jdbc-day01/com/lovecoding/jdbc/TestJdbc04.class


+ 8 - 0
02_MySql/out/production/jdbc-day01/druid.properties

@@ -0,0 +1,8 @@
+#key=value
+driverClassName=com.mysql.jdbc.Driver
+url=jdbc:mysql://localhost:3306/lovecoding?serverTimezone=UTC&rewriteBatchedStatements=true
+username=root
+password=root
+initialSize=5
+maxActive=10
+maxWait=1000