我用JPA的設定檔,persistence.xml,設定以下的資料庫連線資訊:
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">
<persistence-unit name="mydb" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>com.test.vo.Customer</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="oracle.jdbc.driver.OracleDriver" />
<property name="javax.persistence.jdbc.url" value="jdbc:oracle:thin://localhost:1521/orcl" />
<property name="javax.persistence.jdbc.user" value="cuser" />
<property name="javax.persistence.jdbc.password" value="cuser" />
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="none" />
</properties>
</persistence-unit>
</persistence>
然後在DAO讀取這個設定檔:
EntityManagerFactory emf = Persistence.createEntityManagerFactory("mydb");
EntityManager em = emf.createEntityManager();
而我已經有在Maven repository加入了ojdbc8.jar:
https://i.imgur.com/FGIlQxg.png
也有確定有在專案內引入ojdbc8.jar:
https://i.imgur.com/bkXg65L.png
pom.xml也有加入相關設定:
https://i.imgur.com/koleE8Y.png
但是要連線到資料庫時,仍然會跳出java.lang.ClassNotFoundException: Could not load requested class : oracle.jdbc.driver.OracleDriver
在Maven dependecy的ojdbc8.jar,底下的class確實有oracle.jdbc.driver.OracleDriver
那麼為何還會跳出這樣的錯誤??
結果我將ojdbc8.jar改放到專案內部的lib資料夾內,再到java build path去add jar,把ojdbc8.jar加進來,就好了。