300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > idea mysql错误提示_idea提示错误:java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

idea mysql错误提示_idea提示错误:java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

时间:2019-06-22 21:17:12

相关推荐

idea mysql错误提示_idea提示错误:java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

碰见了一个问题,一直无法解决,希望大家帮帮忙。

这是连接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,如图:

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。