天天看點

Java 連接配接mysql 最新版本8.0以上!

ackage com.anbow.jsp;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class utils {
    static {
        try {// 加載驅動程式
            ***Class.forName("com.mysql.cj.jdbc.Driver");***
        } catch (Exception e) {
            // TODO: handle exception
        }
    }

    // 定義一個檔案掃描器 輸入資料

    // 定義公共的資料庫屬性 (url user paaword connection)
    

***private static String url = "jdbc:mysql://localhost:3306/teawork?serverTimezone=GMT";***

    private static String user = "root";
    private static String password = "root";
    public static Connection connection = null;
    // private ImageIcon background;

    public static Connection getConnection() {
        try {
            connection = DriverManager.getConnection(url, user, password);
        } catch (SQLException exception) {
            // TODO: handle exception
            exception.printStackTrace();
        }

        return connection;
    }

    public static void main(String[] args) {


        Connection connection = getConnection();
        if (connection != null) {
            System.out.println("連接配接成功!");

        }
    }
}