windows - idea提示错误:java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
PHPz
PHPz 2017-04-18 10:50:53
[Java讨论组]

碰见了一个问题,一直无法解决,希望大家帮帮忙。
这是连接mysql的JdbcUtils代码:

public class JdbcUtils {
    // 配置文件
    private static Properties props = null;
    // 静态块保证只加载一次
    static {
        InputStream in = JdbcUtils.class.getClassLoader().getResourceAsStream(
                "dbconfig.properties");
        props = new Properties();
        try {
            props.load(in);
        } catch (IOException e) {
            System.out.println("加载配置文件失败!");
            throw new RuntimeException(e);
        }

        try {
            Class.forName(props.getProperty("driverClassName"));
        } catch (ClassNotFoundException e) {
            System.out.println("驱动类加载失败");
            throw new RuntimeException(e);
        }
    }

    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(props.getProperty("url"),
                props.getProperty("username"), props.getProperty("password"));
    }
}

用下面的这个测试类证明jdbcutilis没有问题,可以加载com.mysql.jdbc.Driver

public class TestJDBCUserDao {
    @Test
    public  void test (){
        UserDao ud = UserDaoFactory.getUserDao("mysql");
        User user = new User();
        user = ud.findByName("111");
        System.out.println(user.getPassword());
    }
}

UserDao的jindByName是为了获得数据库连接:

项目也已经引入数据库驱动包,如图:

但当用tomcat跑时提示错误:
错误提示,驱动类加载失败,也就是:java.lang.ClassNotFoundException: com.mysql.jdbc.Driver,如图:

PHPz
PHPz

学习是最好的投资!

全部回复(2)
天蓬老师

如果你是直接把jar包拷贝进去的,右击jar点击add library,如果是jsp项目 需要拷贝到web下的lib下面。

PHP中文网

把jar包同时也要放到tomcat的lib目录下

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号