Java 创建 MySQL 数据库连接

package com.imooc.db;

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

public class DBUtil
{
    private static String URL = "jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8";
    private static String USER = "root";
    private static String PASSWORD = "root";
    private static Connection conn = null;
    
    static
    {
        try
        {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(URL, USER, PASSWORD);
        }
        catch (ClassNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
    }
    
    public static Connection getConnection()
    {
        return conn;
    }
}

你可能感兴趣的:(Java 创建 MySQL 数据库连接)