zoukankan      html  css  js  c++  java
  • 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);
    数据库连接方式五
  • 相关阅读:
    C#打造51CTO自动签到服务领取无忧币之开篇
    Windows8开发实战之文本布局
    设计模式之抽像工厂
    PDM只显示表名称不显示列表名称
    JS 判断中英文字符长度
    C#操作剪贴板实现复制粘贴
    Chrome控制台设置浏览器Cookie
    通过Mysql查询分析器 建库建表语句
    推荐两个VS开发工具插件
    C#经典机试题(猫叫)
  • 原文地址:https://www.cnblogs.com/zhenqk/p/11101630.html
Copyright © 2011-2022 走看看