美文网首页Java
Java 创建 MySQL 数据库连接

Java 创建 MySQL 数据库连接

作者: 春泥村雨 | 来源:发表于2018-03-04 10:38 被阅读31次
    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 数据库连接

        本文链接:https://www.haomeiwen.com/subject/cbsffftx.html