java hibernate将数据从SQL Server导出到.dbf(dbase)

时间:2017-11-28 13:12:55

标签: java hibernate dbase

我的应用程序应该将数据从sql server导出到 .dbf -file。我的java代码我已经通过hibernate将数据转换为 .csv -file。但我无法将其写入dbase而不是csv。你能救我吗?

主要课程:

public class CreateODV {

public static void main(String[] args) {
    try (Database con = new Database(Database.PERSISTENCE_UNIT_03)) {
        EntityManager em = con.getEntityManager();

        List<ODV> odv = ODV.get_ODV(em);

        Output.writeOdv(odv);
    }
}
}

ODV.java

@Entity
@Table(name = "qry_main_odv")
@NamedQueries({ @NamedQuery(name = ODV.GET_ALL, query = "SELECT a FROM ODV a"),
@NamedQuery(name = ODV.GET_ODV, query = "SELECT a FROM ODV a")})
public class ODV {

public final static String GET_ALL = "ODV.GET_ALL";
public final static String GET_ODV = "ODV.GET_ODV";

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID")
private int id;

@Column(name = "PROJEKT")
private String projekt;

@Column(name = "KNOTEN_NAM")
private String knotenNam;

@Column(name = "LAUF_INDEX")
private Integer laufIndex;

@Column(name = "KREUZ_NR")
private Integer kreuzNr;

@Column(name = "MELDEPUNKT")
private String meldepunkt;

@Column(name = "AWGNUMMER")
private Integer awgNummer;

@Column(name = "TABLELLE")
private Integer tabelle;

@Column(name = "LINIE_MP")
private Integer linieMp;

@Column(name = "ROUTE")
private Integer route;

@Column(name = "AUFSTELLDA")
private Integer aufstellda;


public ODV() {
}

public ODV(int id, String projekt, String knotenNam, int laufIndex, int kreuzNr, String meldepunkt, int awgNummer,
        int tabelle, int linieMp, int route, int aufstellda) {
    this.id = id;
    this.projekt = projekt;
    this.knotenNam = knotenNam;
    this.laufIndex = laufIndex;
    this.kreuzNr = kreuzNr;
    this.meldepunkt = meldepunkt;
    this.awgNummer = awgNummer;
    this.tabelle = tabelle;
    this.linieMp = linieMp;
    this.route = route;
    this.aufstellda = aufstellda;
}

public static List<ODV> get_ODV(EntityManager em) {
    List<ODV> odv = new ArrayList<>();
    try {
        odv = em.createNamedQuery(GET_ODV, ODV.class).getResultList();

    } catch (NoResultException e) {
        System.out.println("No Result");
    }
    return odv;
}

public int getId() {
    return id;
}

public String getProjekt() {
    return projekt;
}

public String getKnotenNam() {
    return knotenNam;
}

public Integer getLaufIndex() {
    return laufIndex;
}

public Integer getKreuzNr() {
    return kreuzNr;
}

public String getMeldepunkt() {
    return meldepunkt;
}

public Integer getAwgNummer() {
    return awgNummer;
}

public Integer getTabelle() {
    return tabelle;
}

public Integer getLinieMp() {
    return linieMp;
}

public Integer getRoute() {
    return route;
}

public Integer getAufstellda() {
    return aufstellda;
}

@Override
public String toString() {
    String output = "Projekt: " + projekt;
    output += " Knoten Name: " + knotenNam;
    output += " Laufindex: " + laufIndex;
    output += " Kreuzungsnummer: " + kreuzNr;
    output += " Meldepunkt: " + meldepunkt;
    output += " AWG Nummer: " + awgNummer;
    output += " Tabelle: " + tabelle;
    output += " LinieMp: " + linieMp;
    output += " Route: " + route;
    output += " Aufstellda: " + aufstellda;
    return output;
}
}

output.java:

public class Output {

public static void writeOdv(List<ODV> odv) {

    String content = "";
    FileOutputStream fos = null;
    Writer writer = null; 

    try {

        fos = new FileOutputStream("C:\\Users\\Mike\\Desktop\\folder\\output.dbf");
        writer = new BufferedWriter(new OutputStreamWriter(fos, StandardCharsets.UTF_8));

        content += "PROJEKT";
        content += ";";
        content += "KNOTEN_NAM";
        content += ";";
        content += "LAUF_INDEX";
        content += ";";
        content += "KREUZ_NR";
        content += ";";
        content += "MELDEPUNKT";
        content += ";";
        content += "AWGNUMMER";
        content += ";";
        content += "TABELLE";
        content += ";";
        content += "LINIE_MP";
        content += ";";
        content += "ROUTE";
        content += ";";
        content += "AUFSTELLDA";
        content += "\n";

        for (ODV row : odv) {

            content += row.getProjekt() == null ? "" : row.getProjekt();
            content += ";";
            content += row.getKnotenNam() == null ? "" : row.getKnotenNam();
            content += ";";
            content += row.getLaufIndex() == null ? "" : row.getLaufIndex();
            content += ";";
            content += row.getKreuzNr() == null ? "" : row.getKreuzNr();
            content += ";";
            content += row.getMeldepunkt() == null ? "" : row.getMeldepunkt();
            content += ";";
            content += row.getAwgNummer() == null ? "" : row.getAwgNummer();
            content += ";";
            content += row.getTabelle() == null ? "" : row.getTabelle();
            content += ";";
            content += row.getLinieMp() == null ? "" : row.getLinieMp();
            content += ";";
            content += row.getRoute() == null ? "" : row.getRoute();
            content += ";";
            content += row.getAufstellda() == null ? "" : row.getAufstellda();
            content += "\n";

        }

        writer.write(content);              

    } catch (IOException e) {

        // TODO Auto-generated catch block
        e.printStackTrace();

    } finally {
        try {
            writer.flush();
            writer.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }       
}
}

在output.java中我想导出到 .dbf 。是否有捷径可寻?或者有没有办法像我试过的那样?

0 个答案:

没有答案