• Java连接Mysql数据库的五种方法


    说明: 本文通过maven构建,连接的数据库是mysql 数据库

    pom.xml

     <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.38</version>
        </dependency>
    pom.xml

    数据库连接方式一

     public static void Connection1() throws SQLException {
            Driver drive=new com.mysql.jdbc.Driver();
            String  url="jdbc:mysql://localhost:3306/test";
            Properties info=new Properties();
            info.setProperty("user","root");
            info.setProperty("password","root");
            Connection connect = drive.connect(url, info);
            System.out.println(connect);
        }
    获取数据库连接的方式一

    数据库连接方式二

     public static void Connection2() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
            // 1.获取Driver实现类对象:使用反射
            Class clazz = Class.forName("com.mysql.jdbc.Driver");
            Driver driver = (Driver) clazz.newInstance();
            // 2.提供要连接的数据库
            String url = "jdbc:mysql://localhost:3306/test";
            // 3.提供连接需要的用户名和密码
            Properties info = new Properties();
            info.setProperty("user", "root");
            info.setProperty("password", "root");
            // 4.获取连接
            Connection conn = driver.connect(url, info);
            System.out.println(conn);
        }
    数据库连接方式二

    数据库连接方式三

     // 1.获取Driver实现类的对象
            Class clazz = Class.forName("com.mysql.jdbc.Driver");
            Driver driver = (Driver) clazz.newInstance();
            // 2.提供另外三个连接的基本信息:
            String url = "jdbc:mysql://localhost:3306/test";
            String user = "root";
            String password = "root";
            // 注册驱动
            DriverManager.registerDriver(driver);
            // 获取连接
            Connection conn = DriverManager.getConnection(url, user, password);
            System.out.println(conn);
    数据库连接方式三

    数据库连接方式四

    String url = "jdbc:mysql://localhost:3306/test";
            String user = "root";
            String password = "root";
    
            // 2.加载Driver
            Class.forName("com.mysql.jdbc.Driver");
            // 3.获取连接
            Connection conn = DriverManager.getConnection(url, user, password);
            System.out.println(conn);
    数据库连接方式四

    数据库连接方式五 非常推荐使用

    好处:

    • 1.实现了数据与代码的分离。实现了解耦

    • 2.如果需要修改配置文件信息,可以避免程序重新打包。

    user=root
    password=root
    url=jdbc:mysql://localhost:3306/test?rewriteBatchedStatements=true
    driverClass=com.mysql.jdbc.Driver
    配置文件jdbc.properties
            InputStream is = connDate.class.getClassLoader().getResourceAsStream("jdbc.properties");
            Properties pros = new Properties();
            pros.load(is);
            String user = pros.getProperty("user");
            String password = pros.getProperty("password");
            String url = pros.getProperty("url");
            String driverClass = pros.getProperty("driverClass");
            //2.加载驱动
            Class.forName(driverClass);
            //3.获取连接
            Connection conn = DriverManager.getConnection(url, user, password);
            System.out.println(conn);
    数据库连接方式五
  • 相关阅读:
    每周必写
    每周必写
    每周必写
    中国历史上成功的两人合作, 改进, 提高的例子
    每周必写(3)
    结对编程进度及自己的理解
    每周必写
    IT行业的“创新”、“模仿”
    工作时间内容,感想和思考
    周阅读内容
  • 原文地址:https://www.cnblogs.com/zhenqk/p/11101630.html
Copyright © 2020-2023  润新知