注册 登录
编程论坛 JAVA论坛

反射,javaweb

hello_error 发布于 2021-04-14 20:12, 1211 次点击
请教个问题,尚硅谷视频里面的一段代码,疑惑的是类的路径是固定的怎么就提高了程序的可移植性了呢
    // 方式一:
    @Test
    public void testConnection1() throws SQLException {
        // 获取Driver实现类对象
        Driver driver = new com.mysql.jdbc.Driver();
    .....
    }

    // 方式二:对方式一的迭代:在如下的程序中不出现第三方的api,使得程序具有更好的可移植性
    @Test
    public void testConnection2() throws Exception {
        // 1.获取Driver实现类对象:使用反射
        Class clazz = Class.forName("com.mysql.jdbc.Driver");
        Driver driver = (Driver) clazz.newInstance();
1 回复
#2
hello_error2021-04-14 21:30
这个路径可以写在配置文件里面
1