package ink.wgink.database.service.connection.impl;

import ink.wgink.database.service.connection.IConnection;

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

/**
 * @ClassName: MySqlConnectionImpl
 * @Description: mysql连接
 * @Author: wanggeng
 * @Date: 2021/10/26 11:57 下午
 * @Version: 1.0
 */
public class MySqlConnectionImpl implements IConnection {

    private String host;
    private String port;
    private String username;
    private String password;

    public MySqlConnectionImpl(String host, String port, String username, String password) {
        this.host = host;
        this.port = port;
        this.username = username;
        this.password = password;
    }

    static {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    @Override
    public Connection connect() throws SQLException {
        return DriverManager.getConnection(String.format("jdbc:mysql://%s:%s?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8&autoReconnect=true&failOverReadOnly=false&useSSL=false&serverTimezone=UTC&nullCatalogMeansCurrent=true", host, port), username, password);
    }

}
