栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java连接mysql数据库(Idea)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java连接mysql数据库(Idea)

一.导入mysql的jdbc驱动(mysql 8) 1.右击项目,新建一个Dictionary,命名为lib(最好是lib)

2.下载jdbc驱动并导入java项目

下载地址: https://dev.mysql.com/downloads/connector/j/.

将下载的jar文件复制到lib目录下
首先进入Project Sttructure

在Dependencies点击加号

选择之前复制到lib目录的mysql驱动进行添加

应用即可。

二.连接数据库
public class ConnectionTest {

    //方式一
    @Test
    public void testConnection1() throws SQLException {
        //初始化驱动
        Driver driver = new com.mysql.cj.jdbc.Driver();

        //协议(jdbc:mysql) + ip地址(loaclhost) + 端口号(3306) + 数据库(test)
        //记得添加东八区时间  serverTimezone=GMT,否则会报异常
        String url = "jdbc:mysql://localhost:3306/test?serverTimezone=GMT";

        //基本配置,至少包括user和password
        Properties info = new Properties();
        info.setProperty("user", "root");
        info.setProperty("password", "zq740208");

        Connection conn = driver.connect(url, info);
        System.out.println(conn);
    }

    //方式二:对方式一的迭代
    @Test
    public void testConnection2() throws Exception {
        Class aClass = Class.forName("com.mysql.cj.jdbc.Driver");
        Driver driver = (Driver) aClass.newInstance();
        String url = "jdbc:mysql://localhost:3306/test?serverTimezone=GMT";

        Properties info = new Properties();
        info.setProperty("user", "root");
        info.setProperty("password", "zq740208");

        Connection connect = driver.connect(url, info);
        System.out.println(connect);
    }

    //方式三:使用DriverManager替代Driver
    @Test
    public void testConnection3() throws Exception {
        Class aClass = Class.forName("com.mysql.cj.jdbc.Driver");
        Driver driver = (Driver) aClass.newInstance();
        String url = "jdbc:mysql://localhost:3306/test?serverTimezone=GMT";
        String user = "root";
        String password = "zq740208";

        //注册驱动
        DriverManager.registerDriver(driver);

        //获取连接
        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println(connection);
    }

    //方式四:
    @Test
    public void testConnection4() throws Exception {
        String url = "jdbc:mysql://localhost:3306/test?serverTimezone=GMT";
        String user = "root";
        String password = "zq740208";

        //加载Driver
        Class.forName("com.mysql.cj.jdbc.Driver");
        //省略注册驱动的操作,因为在mysql的Driver类中声明了如下操作:
//        static {
//            try {
//                DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
//            } catch (SQLException var1) {
//                throw new RuntimeException("Can't register driver!");
//            }
//        }

        //获取连接
        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println(connection);
    }

    //常用方式
    @Test
    public void testConnection5() throws Exception {
        Properties properties = new Properties();
        properties.load(new FileReader("jdbc.properties"));
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String url = properties.getProperty("url");
        String driverClass = properties.getProperty("driverClass");

        Class.forName(driverClass);
        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println(connection);
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/346386.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号