Java JDBC ConnectionFactory类1.0

说明

建立数据库连接工厂类,调用静态方法,传入参数,快速创建 Connection

package top.malaoshi.jdbc;

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

public class ConnectionFactory {

    //连接数据库
    public static Connection connect(String driver,String url,String username,String password) throws ClassNotFoundException, SQLException {
        Class.forName(driver);
        Connection connection = DriverManager.getConnection(url, username, password);

        return connection;
    }


}

测试

Connection conn = ConnectionFactory.connect("com.mysql.jdbc.Driver" 
                , "jdbc:mysql://127.0.0.1:3308/scott?useSSL=false&useUnicode=true&characterEncoding=utf-8"
                ,"root"
                ,"");

原文出处:/show_1IX6xhSsZGhc.html