JDBC 数据库连接五种方式

五种方式

package com.fspedu.jdbc;

import com.mysql.jdbc.Driver;
import org.junit.jupiter.api.Test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 * 分析Java 连接mysql的5种方式
 */
public class JdbcConn {

    //方式一
    @Test
    public void connect01() throws SQLException {
        //前置工作:在项目下创建一个文件夹 比如 libs
        //将mysql.jar 拷贝到该目录下,点击 add as project ..加入到项目

        //1.注册驱动
        Driver driver = new Driver();//创建driver对象

        //2.得到连接
        //(1)jdbc:mysql:// 规定好表示协议,通过jdbc的方式连接mysql
        //(2)localhost 主机,可以是ip地址
        //(3)3306 表示mysql监听的端口
        //(4)hsp_db02 连接到mysql dbms 的哪个数据库
        //(5)mysql的连接本质就是前面学过的socket连接
        String url = "jdbc:mysql://localhost:3306/hsp_db02?useUnicode=true&characterEncoding=utf8";
        //将 用户名和密码放入到Properties 对象
        Properties properties = new Properties();
        //说明 user和Password 是规定好的,后面的值根据实际情况写
        properties.setProperty("user","root");//用户
        properties.setProperty("password","root");//密码
        Connection connect = driver.connect(url, properties);//可以理解成网络连接
        System.out.println("方式一:" + connect);

        //3.执行SQL
        String sql = "insert into actor values(null,'刘德华','男','1970-11-11','1100')";
        //statement 用于执行静态SQL语句并返回其生成的结果的对象
        Statement statement = connect.createStatement();
        int rows = statement.executeUpdate(sql);//如果是dml语句,返回的就死影响的行数

        System.out.println(rows > 0? "成功" : "失败");

        //4.关闭连接资源
        statement.close();
        connect.close();
    }

    //方式二
    @Test
    public void connect02() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
        //使用反射加载Driver类,动态加载,更加灵活,减少依赖性
        Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
        Driver driver = (Driver) aClass.newInstance();

        String url = "jdbc:mysql://localhost:3306/hsp_db02?useUnicode=true&characterEncoding=utf8";
        //将 用户名和密码放入到Properties 对象
        Properties properties = new Properties();
        //说明 user和Password 是规定好的,后面的值根据实际情况写
        properties.setProperty("user","root");//用户
        properties.setProperty("password","root");//密码
        Connection connect = driver.connect(url, properties);
        System.out.println("方式二:" + connect);
    }

    //方式三   使用DriverManger 代替 Diver 进行统一管理
    @Test
    public void connect03() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {

        //使用反射加载 Diver
        Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
        Driver driver = (Driver) aClass.newInstance();

        //创建url 和 user 和 password
        String url = "jdbc:mysql://localhost:3306/hsp_db02?useUnicode=true&characterEncoding=utf8";
        String user = "root";
        String password = "root";

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

        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println("方式三:" + connection);
    }

    //方式四(使用最多)   使用Class.forName 自动完成注册驱动,简化代码
    @Test
    public void connect04() throws ClassNotFoundException, SQLException {
        //使用反射加载了 Driver 类
        //在加载 Driver类时,完成注册
        /*
            源码: 1.静态代码块,在类加载时,会执行一次
                 2.DriverManager.registerDriver(new Driver());
                 3.因此注册driver的工作已经完成
            static {
                try {
                    DriverManager.registerDriver(new Driver());
                } catch (SQLException var1) {
                    throw new RuntimeException("Can't register driver!");
                }
            }
         */
        Class.forName("com.mysql.jdbc.Driver");
        //mysql驱动5.1.6可以无需Class.forName(“com.mysql.jdbc.Driver”)
        //从jdk1.5以后使用了jdbc4,不再需要显示调用Class.forName()注册驱动而是自动调用驱动jar包下META-INF\services\java.sql.Driver文本中的类名称去注册
        //建议写上Class.forName("com.mysql.jdbc.Driver");更加明确

        //创建url 和 user 和 password
        String url = "jdbc:mysql://localhost:3306/hsp_db02?useUnicode=true&characterEncoding=utf8";
        String user = "root";
        String password = "root";

        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println("方式四:" + connection);
    }

    //方式五(在方式四基础上进行优化,增加配置文件,让连接mysql更加灵活)(推荐使用)
    @Test
    public void connect05() throws IOException, ClassNotFoundException, SQLException {

        //通过Properties对象获取配置文件的信息
        Properties properties = new Properties();
        properties.load(new FileInputStream("src\\mysql.properties"));
        //获取相关的值
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String driver = properties.getProperty("driver");
        String url = properties.getProperty("url");

        Class.forName(driver);//建议写上

        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println("方式五:" + connection);

    }
}

方式五的配置文件(mysql.properties)

user=root
password=root
url=jdbc:mysql://localhost:3306/hsp_db02?useUnicode=true&characterEncoding=utf8
driver=com.mysql.jdbc.Driver
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值