JDBC建立连接的五种方式 及其推荐使用

package com.hspedu.jdbc;

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

import java.beans.Transient;
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;

/**
 * @author: guorui fu
 * @versiion: 1.0
 * 分析java连接MySQL的五种方式
 */
public class jdbcConn {
    @Test
    public void connect01() throws SQLException {
        //1.注册驱动
        Driver driver = new Driver();
        //2.得到连接
        String url =  "jdbc:mysql://localhost:3306/hsp_db02";
        // 将用户名和密码封装到properties对象 user与password 是规定好的key,值自己写
        Properties properties = new Properties();
        properties.setProperty("user","root");//用户
        properties.setProperty("password","fgr");//密码
        // connect 是连接
        Connection connect = driver.connect(url, properties);
        //3.执行sql
        String sql = "delete from actor where id = 1";
        //用于执行静态sql语句并返回生成结果的对象
        Statement statement = connect.createStatement();
        int row = statement.executeUpdate(sql);//如果是dml语句,返回的是影响的行数
        System.out.println(row > 0 ? "成功":"失败");
        //4.关闭连接资源
        statement.close();
        connect.close();
    }
    //方式2
    @Test
    public void connect02() throws IllegalAccessException, InstantiationException, ClassNotFoundException, SQLException {
        //1.反射加载Driver类,动态加载,减少了依赖性
        Class<?> cls = Class.forName("com.mysql.jdbc.Driver");
        Driver driver = (Driver)cls.newInstance();
        //2.得到连接
        String url =  "jdbc:mysql://localhost:3306/hsp_db02";
        // 将用户名和密码封装到properties对象 user与password 是规定好的key,值自己写
        Properties properties = new Properties();
        properties.setProperty("user","root");//用户
        properties.setProperty("password","fgr");//密码

        Connection connect = driver.connect(url, properties);
        System.out.println("方式2" + connect);

    }
    //方式3 使用Drivermanager 替代 Driver 进行统一管理
    @Test
    public void connect03() throws IllegalAccessException, InstantiationException, ClassNotFoundException, SQLException {
        Class<?> cls = Class.forName("com.mysql.jdbc.Driver");
        Driver driver = (Driver)cls.newInstance();

        //创建url 和user ,password
        String url =  "jdbc:mysql://localhost:3306/hsp_db02";
        String user = "root";//用户
        String password = "fgr";//密码

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

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

    //【推荐】方式4,使用Class.forname 自动完成注册驱动,简化代码
    @Test
    public void connect04() throws ClassNotFoundException, SQLException {
        //使用反射加载 Driver类
        //在加载Driver 类是,完成注册,底层注册Driver在静态代码块中进行
        Class<?> cls = Class.forName("com.mysql.jdbc.Driver");

        //创建url 和user ,password
        String url =  "jdbc:mysql://localhost:3306/hsp_db02";
        String user = "root";//用户
        String password = "fgr";//密码
        Connection connection = DriverManager.getConnection(url, user, password);

        System.out.println("第四种方式" + connection);
    }

    //[推荐,简洁]方式5 在方式4基础上改进,使用配置文件,连接数据库更加灵活
    @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("方式5" + connection);

        Statement statement = connection.createStatement();
        String sql = "insert into ";
        int i = statement.executeUpdate(sql);
        System.out.println(i > 0 ? "成功" : "失败");

        statement.close();
        connection.close();

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值