最原始的连接方式

    1. package com.daijunyi;
    2. import org.junit.Test;
    3. import java.io.InputStream;
    4. import java.sql.Connection;
    5. import java.sql.Driver;
    6. import java.sql.DriverManager;
    7. import java.sql.SQLException;
    8. import java.util.Properties;
    9. public class ConnectionTest {
    10. // 方式一
    11. @Test
    12. public void connection() throws SQLException {
    13. Driver driver = new com.mysql.cj.jdbc.Driver();
    14. Properties properties = new Properties();
    15. properties.put("user","root");
    16. properties.put("password","qwerasdf123");
    17. String url = "jdbc:mysql://localhost:3306/learn?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&serverTimezone=GMT%2b8";
    18. Connection connect = driver.connect(url, properties);
    19. System.out.println(connect);
    20. }
    21. // 方式三
    22. @Test
    23. public void connection1() throws Exception {
    24. Class<?> aClass = Class.forName("com.mysql.cj.jdbc.Driver");
    25. Driver driver = (Driver) aClass.newInstance();
    26. Properties properties = new Properties();
    27. properties.put("user","root");
    28. properties.put("password","qwerasdf123");
    29. String url = "jdbc:mysql://localhost:3306/learn?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&serverTimezone=GMT%2b8";
    30. Connection connect = driver.connect(url, properties);
    31. System.out.println(connect);
    32. }
    33. // 方式三
    34. @Test
    35. public void connection2() throws Exception{
    36. Class<?> aClass = Class.forName("com.mysql.cj.jdbc.Driver");
    37. Driver driver = (Driver) aClass.newInstance();
    38. DriverManager.registerDriver(driver);
    39. Properties properties = new Properties();
    40. properties.put("user","root");
    41. properties.put("password","qwerasdf123");
    42. String url = "jdbc:mysql://localhost:3306/learn?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&serverTimezone=GMT%2b8";
    43. Connection connection = DriverManager.getConnection(url, properties);
    44. System.out.println(connection);
    45. }
    46. // 方式四 我们只需要加载驱动到内存 ,因为驱动一加载类的时候 静态代码块会自动注册驱动
    47. // */
    48. // public class Driver extends NonRegisteringDriver implements java.sql.Driver {
    49. // //
    50. // // Register ourselves with the DriverManager
    51. // //
    52. // static {
    53. // try {
    54. // java.sql.DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
    55. // } catch (SQLException E) {
    56. // throw new RuntimeException("Can't register driver!");
    57. // }
    58. // }
    59. @Test
    60. public void connection3() throws Exception{
    61. Class.forName("com.mysql.cj.jdbc.Driver");
    62. // Driver driver = (Driver) aClass.newInstance();
    63. // DriverManager.registerDriver(driver);
    64. Properties properties = new Properties();
    65. properties.put("user","root");
    66. properties.put("password","qwerasdf123");
    67. String url = "jdbc:mysql://localhost:3306/learn?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&serverTimezone=GMT%2b8";
    68. Connection connection = DriverManager.getConnection(url, properties);
    69. System.out.println(connection);
    70. }
    71. @Test
    72. public void connection4() throws Exception{
    73. InputStream resourceAsStream = ConnectionTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
    74. Properties properties = new Properties();
    75. properties.load(resourceAsStream);
    76. String driver = properties.getProperty("driver");
    77. String url = properties.getProperty("url");
    78. String user = properties.getProperty("user");
    79. String password = properties.getProperty("password");
    80. Class.forName(driver);
    81. Connection connection = DriverManager.getConnection(url, user, password);
    82. System.out.println(connection);
    83. }
    84. }
    1. url=jdbc:mysql://localhost:3306/learn?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&serverTimezone=GMT%2b8
    2. user=root
    3. password=qwerasdf123
    4. driver=com.mysql.cj.jdbc.Driver

    image.png