1. package com.xf.utils;
    2. import java.sql.Connection;
    3. import java.sql.DriverManager;
    4. import java.sql.SQLException;
    5. //对数据库进行连接
    6. public class DBManager {
    7. private static final String DRIVER = "com.mysql.cj.jdbc.Driver";
    8. //所连接的数据库地址
    9. private static final String URL="jdbc:mysql://localhost:3306/qq?serverTimezone=GMT&useSSL=false";
    10. //用户名密码
    11. private static final String USERNAME="root";
    12. private static final String PASSWORD="admin123";
    13. //加载驱动
    14. static {
    15. try {
    16. Class.forName(DRIVER);
    17. } catch (ClassNotFoundException e) {
    18. e.printStackTrace();
    19. }
    20. }
    21. //获取连接的方法
    22. public static Connection getConn(){
    23. //尝试获取连接
    24. try {
    25. //connection
    26. Connection connection = DriverManager.getConnection(URL,USERNAME,PASSWORD);
    27. System.out.println(connection);
    28. return connection;
    29. } catch (SQLException e) {
    30. e.printStackTrace();
    31. }
    32. return null;
    33. }
    34. public static void main(String[] args) {
    35. getConn();
    36. }
    37. }