我在项目中创建数据库时遇到问题。我已在其他topic中读到,我需要将其包含在我的代码中:
Class.forName("org.apache.derby.jdbc.ClientDriver");
所以我做了:
private static EntityManagerFactory factory;
public static Connection getConnection() {
try {
Class.forName("org.apache.derby.jdbc.ClientDriver");
factory = Persistence.createEntityManagerFactory("Zadanie_3PU");
EntityManager em = factory.createEntityManager();
Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/iab;create=true", "", "");
return con;
}
catch(Exception ex) {
System.out.println("Database.getConnection() Error -->" + ex.getMessage());
return null;
}
}
我还确保我在类路径中加载了正确的驱动程序:
我还确保我的persistence.xml
中没有拼写错误:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" 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">
<persistence-unit name="Zadanie_3PU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>model.User</class>
<class>model.Email</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/iab;create=true"/>
<property name="javax.persistence.jdbc.user" value=""/>
<property name="javax.persistence.jdbc.password" value=""/>
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
</properties>
</persistence-unit>
</persistence>
但是,当我尝试运行它时,这会一直在我的glassfish服务器上抛出以下错误:
Info: Database.getConnection() Error -->Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.6.1.v20150605-31e8258): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLException: No suitable driver found for jdbc:derby://localhost:1527/iab;create=true
Error Code: 0
我做错了什么?