JDBC链接的其他方式

本文详细介绍了五种方式连接MySQL数据库:反射创建Driver实例、DriverManager注册、配置文件加载Driver、静态代码块注册及使用DriverManager连接。展示了从代码层面到实践的最佳实践和安全性考虑。
摘要由CSDN通过智能技术生成
@Test
    //方式2,利用反射来调用Driver 并获取实例
    public void test2() throws Exception{
        Class<?> aClass = Class.forName("com.mysql.cj.jdbc.Driver");
        Object o = aClass.newInstance();
        Driver driver = (Driver) o;

        String url = "jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=GMT%2B8";
        //实例化配置
        Properties info=new Properties();
        //设置配置中的账号和密码
        info.setProperty("user","xxxxx");
        info.setProperty("password","xxxxx");
        //建立连接
        Connection connection=driver.connect(url,info);
        System.out.println(connection);

    }
    @Test
    //方式3-利用 DriverManager替换Driver
    public void test3() throws Exception {
        Class<?> aClass = Class.forName("com.mysql.cj.jdbc.Driver");
        Object o = aClass.newInstance();
        Driver driver = (Driver) o;

        //使用 DriverManager进行Driver注册
        DriverManager.registerDriver(driver);

        String url = "jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=GMT%2B8";
        String user="xxxxx";
        String password="xxxxx";

        //建立链接
        Connection connection = DriverManager.getConnection(url, user, password);

        System.out.println(connection);
    }
    @Test
    //方式4
    public void test4() throws Exception {
        String url = "jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=GMT%2B8";
        String user="xxxxx";
        String password="xxxxx";

        /**
         * static {
         *      try {
         *               DriverManager.registerDriver(new Driver());
         *          } catch (SQLException var1) {
         *      throw new RuntimeException("Can't register driver!");
         *      }
         */
        //加载Driver类-其中Driver中包含静态代码块,随着类的加载而加载
        // 因此不需要类的实例化和注册
        //类的加载
        Class.forName("com.mysql.cj.jdbc.Driver");
        /*Object o = aClass.newInstance();
        Driver driver = (Driver) o;

        //使用 DriverManager进行Driver注册
        DriverManager.registerDriver(driver);*/

        //建立链接
        Connection connection = DriverManager.getConnection(url, user, password);

        System.out.println(connection);
    }
    @Test
    //方式5通过配置文件链接数据库,不会将用户名和密码暴露出来,实现数据和代码的分离,解耦
    public void test5() throws Exception {
        //读取配置文件
        /**方式1
         * Properties properties=new Properties();
         * FileInputStream fileInputStream=new FileInputStream("jdbc.properties");
         *
         * //加载对应流文件
         * properties.load(fileInputStream);
         * String name = properties.getProperty("name");
         * String password = properties.getProperty("password");
         *
         * System.out.println(name+password);
         */
        //方式2通过类加载器加载配置文件-类加载器要确保properties在src文件下
        //FileInputStream resource FileInputStream("jdbc.properties");
        InputStream resource = ConnectionTest.class.getClassLoader().getResourceAsStream("jdbc.properties");

        Properties pt=new Properties();
        pt.load(resource);


        String user = pt.getProperty("user");
        String password = pt.getProperty("password");
        String url = pt.getProperty("url");
        String driverClass = pt.getProperty("driverClass");


        Class.forName(driverClass);

        //建立链接
        Connection connection = DriverManager.getConnection(url, user, password);

        System.out.println(connection);
    }
#properties中的配置信息
url=jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=GMT%2B8
user=xxxx
password=xxxx
driverClass=com.mysql.cj.jdbc.Driver

文件路径

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值