windows - idea提示錯(cuò)誤:java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
問題描述
碰見了一個(gè)問題,一直無法解決,希望大家?guī)蛶兔Α_@是連接mysql的JdbcUtils代碼:
public class JdbcUtils { // 配置文件 private static Properties props = null; // 靜態(tài)塊保證只加載一次 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('驅(qū)動類加載失敗'); throw new RuntimeException(e);} } public static Connection getConnection() throws SQLException {return DriverManager.getConnection(props.getProperty('url'),props.getProperty('username'), props.getProperty('password')); }}
用下面的這個(gè)測試類證明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是為了獲得數(shù)據(jù)庫連接:
項(xiàng)目也已經(jīng)引入數(shù)據(jù)庫驅(qū)動包,如圖:
但當(dāng)用tomcat跑時(shí)提示錯(cuò)誤:錯(cuò)誤提示,驅(qū)動類加載失敗,也就是:java.lang.ClassNotFoundException: com.mysql.jdbc.Driver,如圖:
問題解答
回答1:如果你是直接把jar包拷貝進(jìn)去的,右擊jar點(diǎn)擊add library,如果是jsp項(xiàng)目 需要拷貝到web下的lib下面。
回答2:把jar包同時(shí)也要放到tomcat的lib目錄下
相關(guān)文章:
1. javascript - 用js實(shí)現(xiàn)遠(yuǎn)程js調(diào)用時(shí)出現(xiàn)時(shí)間機(jī)制問題怎樣解決?2. javascript - 如何合并數(shù)組里某個(gè)key值一樣的對象,合并后把不相同的值放到一個(gè)數(shù)組3. javascript - 關(guān)于微信公眾號開發(fā)的一個(gè)trouble!4. html5 - z-index在瀏覽器調(diào)試有效 手機(jī)測試無效5. javascript - 微信公眾號網(wǎng)頁使用redux如何管理用戶刷新?6. javascript - 微信小程序 如何實(shí)現(xiàn)這種左滑動出現(xiàn)刪除的辦法?有相關(guān)api嗎?7. css3 - 求clearfix使用方法8. html5 - svg標(biāo)簽如何兼容IE89. html5 - webapp中將錄下的視頻,照片上傳服務(wù)器之前,需要如何處理(轉(zhuǎn)變格式?),降低對服務(wù)器的壓力?10. javascript - vue.js 中2個(gè)v-if 怎么處理?
