JDBC-快速入门和五种连接方式

本文详细介绍了如何通过JDBC在Java中使用五种不同的方式连接MySQL数据库,包括静态加载Driver、反射加载、DriverManager管理以及使用配置文件。
摘要由CSDN通过智能技术生成

快速入门

在这里插入图片描述

 // 将mysql.jar拷贝到libs,假如到项目中
        //1. 注册驱动
        Driver driver = new Driver();

        //2.得到连接
        // jdbc:mysql:// -> 规定好的协议,通过jdbc连接mysql
        // localhost -> ip
        // 3306 -> mysql监听的端口
        // ybj_db01 -> 连接到哪个数据库
        String url = "jdbc:mysql://localhost:3306/ybj_db02?characterEncoding=UTF-8";
        Properties properties = new Properties();
        properties.setProperty("user","root");
        properties.setProperty("password", "ybj");
        // 根据url和用户密码连接数据库
        Connection connect = driver.connect(url, properties);

        //3.执行mysql
        String sql = "insert into actor values(null,'ybj','男','2000-07-13','110')";
        // statement用来执行静态sql语句并得到返回的结果
        Statement statement = connect.createStatement();
        int rows = statement.executeUpdate(sql); // dml语句,返回的是影响的行数

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

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

五种连接方式

public class JdbcConn {
    @Test
    public void connect01() throws SQLException {
        // 静态加载
        // 静态加载:编译时就加载相关的类,如果程序中不存在该类则编译报错,依赖性太强。
        Driver driver = new Driver();
        String url = "jdbc:mysql://localhost:3306/ybj_db02?characterEncoding=UTF-8";
        // 使用properties类储存用户名和密码,底层为hashtable
        Properties properties = new Properties();
        properties.setProperty("user","root");
        properties.setProperty("password", "ybj");
        Connection connect = driver.connect(url, properties);
        System.out.println(connect);
    }

    @Test
    public void connect02() throws Exception {
        // 反射加载Driver类,动态加载更灵活
        // 运行时加载相关的类,即使程序中不存在该类,但如果运行时未使用到该类,也不会编译错误,依赖性较弱。
        Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
        Driver driver = (Driver)aClass.newInstance();
        String url = "jdbc:mysql://localhost:3306/ybj_db02?characterEncoding=UTF-8";
        Properties properties = new Properties();
        properties.setProperty("user","root");
        properties.setProperty("password", "ybj");
        Connection connect = driver.connect(url, properties);
        System.out.println(connect);
    }

    // 使用DriverManager 管理
    @Test
    public void connect03()  throws Exception{
        Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
        Driver driver = (Driver) aClass.newInstance();
        String url = "jdbc:mysql://localhost:3306/ybj_db02?characterEncoding=UTF-8";
        String user = "root";
        String password = "ybj";
        // 注册driver驱动
        DriverManager.registerDriver(driver);
        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println(connection);
    }

    @Test
    // 使用Class.forName()自动完成注册驱动,简化代码
    public void connect04() throws  Exception {
        // 静态代码块在类加载时会执行一次
        // Driver在底层自己调用了DriverManager注册
        Class.forName("com.mysql.jdbc.Driver");
        // 不写也可以,因为mysql驱动5.1.6可以无需Class
        // jdk1.5以后使用JDBC4,自动调用jar包下注册
        // 但建议写上,更加明确
        String url = "jdbc:mysql://localhost:3306/ybj_db02?characterEncoding=UTF-8";
        String user = "root";
        String password = "ybj";
        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println(connection);
    }

    @Test
    // 改进4,增加配置文件mysql.properties
    public void connect05() throws Exception {
        // 通过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);

    }
}

  • 13
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值