为了账号安全,请及时绑定邮箱和手机立即绑定

无法从 persistence.xml 连接到 JDBC

无法从 persistence.xml 连接到 JDBC

大话西游666 2021-10-28 15:59:35
我有普通的 java 课本注释为 jpa 的实体。我配置了持久性 xml,但我有很大的堆栈跟踪,但我不明白。另外,我在 javax.persistence 中找不到 jdbc 包。关我什么事?这是我的实体package Java.JPA;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;@Entitypublic class Book{  @Id @GeneratedValue  private long id;  private String title;  public Book()  {  }  public Book(String title) {    this.title = title;  }  public String getTitle()  {    return title;  }  public void setTitle(String title) {    this.title = title;  }}这是我的主要课程package Java;import Java.JPA.Book;import java.sql.SQLException;import javax.persistence.EntityManager;import javax.persistence.EntityManagerFactory;import javax.persistence.EntityTransaction;import javax.persistence.Persistence;public class main{  public static void main(String[] args) throws SQLException, ClassNotFoundException {    EntityManagerFactory emf = Persistence.createEntityManagerFactory("example");    EntityManager em = emf.createEntityManager();    Book book = new Book("TestJPA");    EntityTransaction tx = em.getTransaction();    tx.begin();    em.persist(book);    tx.commit();    em.close();    emf.close();  }}这是我的persistence.xml<?xml version="1.0" encoding="UTF-8" ?><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="example">    <!--<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>-->    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 118 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信