java - 使用 DataNucleus 从 MySQL 到 XLS (java)

标签 java mysql excel datanucleus

我正在尝试创建一个小应用程序,将 MySQL 数据导出到 .XLS。

MySQL 架构来自这里:http://www.mysqltutorial.org/mysql-sample-database.aspx

一些来源:

persistence.xml
我正在尝试定义与我的计算机上的 MySQL 数据库的 MySQL 连接。还有一个 xls 文件。

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
<persistence-unit name="JPA-SAMPLE" transaction-type="RESOURCE_LOCAL">

  <class>com.example.poc.entities.Customer</class>
  <exclude-unlisted-classes/>
  <properties>
    <property name="datanucleus.ConnectionDriverName" value="com.mysql.jdbc.Driver"/>
    <property name="datanucleus.ConnectionURL" value="jdbc:mysql://localhost/datanucleustest"/>
    <property name="datanucleus.ConnectionUserName" value="root"/>
    <property name="datanucleus.ConnectionPassword" value=""/>
    <property name="datanucleus.autoCreateSchema" value="true"/>
    <property name="datanucleus.validateTables" value="false"/>
    <property name="datanucleus.validateConstraints" value="false"/>
  </properties>

</persistence-unit>

<persistence-unit name="excel">
  <class>com.example.poc.entities.Customer</class>
  <exclude-unlisted-classes/>

  <properties>
    <property name="javax.persistence.jdbc.url" value="excel:file:tutorial.xls"/>
    <property name="datanucleus.schema.autoCreateAll" value="true"/>
    <property name="datanucleus.schema.validateTables" value="false"/>
    <property name="datanucleus.schema.validateConstraints" value="false"/>
  </properties>
</persistence-unit>

Customer.java
这是我的实体

package com.example.poc.entities;


import lombok.Data;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Data
@Table(name = "CUSTOMERS")
public class Customer {

    @Id
    @Column(name = "customerNumber")
    private Integer id;

    @Column(name = "customerName")
    private String name;

    @Column(name = "contactLastName")
    private String contactLastName;

    @Column(name = "contactFirstName")
    private String contactFirstName;

    //...
    //and so on, all the attributes defined here

}

Main.java 一个主类,开始一切

package com.example.poc;

import com.example.poc.daos.CustomerDao;

import javax.persistence.*;

public class Main {

    private static EntityManager mysqlManager;
    private static EntityManager xlsManager;

    public static void main(String[] args) {
        EntityManagerFactory mysqlManagerFactory = Persistence.createEntityManagerFactory("JPA-SAMPLE");
        EntityManagerFactory xlsManagerFactory = Persistence.createEntityManagerFactory("excel");
        mysqlManager = mysqlManagerFactory.createEntityManager();
        xlsManager = xlsManagerFactory.createEntityManager();

        CustomerDao customerDao = new CustomerDao(mysqlManager, xlsManager);
        customerDao.exportAllCustomerToXls();
    }

}

CustomerDao.java
我有一个 DAO 类,用于阅读、写作。

package com.example.poc.daos;

import com.example.poc.entities.Customer;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import java.util.ArrayList;
import java.util.List;

public class CustomerDao {

    private EntityManager mysqlManager;
    private EntityManager xlsManager;

    public CustomerDao(EntityManager mysql, EntityManager xls) {
        this.mysqlManager = mysql;
        this.xlsManager = xls;
    }

    public void exportAllCustomerToXls() {
        List<Customer> customers =
                mysqlManager.createQuery("SELECT c FROM Customer c", Customer.class).getResultList();

//        for(Customer c: customers){
//            System.out.println(c.getName());
//        }

        EntityTransaction tx = xlsManager.getTransaction();
        tx.begin();
        for(Customer c : customers){
            xlsManager.persist(c);
        }
        tx.commit();
    }
}

在第一种情况(请参阅注释部分)中,我从数据库中读取所有客户,并将他们的名字写入标准输出。程序突然写下了他们所有的名字。

但是,当我尝试将它们写入 xls 文件时,我会收到以下错误:

jan. 30, 2015 12:59:25 DU org.datanucleus.store.rdbms.query.ForwardQueryResult closingConnection
INFO: Reading in results for query "SELECT c FROM Customer c" since the connection used is closing/committing
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 14.187 s
[INFO] Finished at: 2015-01-30T12:59:26+01:00
[INFO] Final Memory: 37M/286M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.1:java (default-cli) on project datanucleus-poc: An exception occured while executing the Java class. null: InvocationTargetException: NullPointerException -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

但是,当我手动创建一个 Customer 对象并保留该对象时,它会成功运行。

你能给我看一些例子,在 Java 中将一些对象保存到 xls 中吗?

最佳答案

由于您要将对象附加到不同的数据存储,因此需要将持久性属性 datanucleus.attachSameDatastore 添加到 EMF for Excel,设置为“false”,以便它会检查每个对象之前是否已存在决定添加它们

关于java - 使用 DataNucleus 从 MySQL 到 XLS (java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28235974/

相关文章:

java - 如何使用 Docx4J 向现有表添加行

java - 如何使用 wireshark 解密服务以服务 SSL 流量?

java - 如何查找arraylist中落入特定范围的元素数量?

java - 在运行时检查位置设置更改

excel - 使窗口在后台打开

sql - 将 Excel 电子表格导入 SQL 数据库 (Coldfusion 9)

java - 更改android顶部栏的背景颜色

Php 使用连接优化我的请求?

php - MySql 自动完成查询 - 对于多个单词

php - 多标签搜索查询仅通过 1 个标签匹配获得结果