Bonjour,
Je débute en JPA et j'ai un projet de stage c'est de parser un fichier Excel avec POI et de rentrer les données dans une base MySQL.
J'ai créé une base glnbu avec les champs que je souhaite remplir qui correspondent aux champs du fichier Excel (noms des champs de la table : nom des colonnes à la première ligne du fichier).
Ensuite, sous netbeans, j'ai fait : "create entity class from a database" que j'ai mappé avec ma base glnbu.
Puis, j'ai fait : "create jpa controler from a entity class" a partir de ma classe entité générée.
J'ai donc un package jpa.entities et un package jpa.controllers plus mon package utils avec ma classe ExcleReader (qui lit les datas des cellules du fichier Excel.
Ma question est : Que dois-je utiliser pour persister les données d'Excel dans ma base glnbu ? Comment et quand utiliser l'entity manager et où, dans ma classe ExcelReader ?
Voici ma Classe ExcelReader,
c'est au niveau des sitch, case que je récupère les values de mon fichier Excel :
J'ai décris ma classe en @Managedbean. (je sais pas si c'est bon, ...)
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105 /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package com.atosorigin.utils; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.Iterator; import javax.annotation.ManagedBean; import javax.enterprise.context.SessionScoped; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRichTextString; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; /** * * @author tar */ @ManagedBean(value = "excelreader") @SessionScoped public class ExcelReader { public ExcelReader() { } public void persitExcelDataFrom(String xlsPath) throws IOException { InputStream inputStream = null; try { inputStream = new FileInputStream(xlsPath); } catch (FileNotFoundException e) { System.out.println("File not found in the specified path." + e); } POIFSFileSystem fileSystem = null; try { fileSystem = new POIFSFileSystem(inputStream); HSSFWorkbook workBook = new HSSFWorkbook(fileSystem); HSSFSheet sheet = workBook.getSheetAt(0); Iterator<Row> rows = sheet.rowIterator(); HSSFRow row = (HSSFRow) rows.next(); while (rows.hasNext()) { row = (HSSFRow) rows.next(); Iterator<Cell> cells = row.cellIterator(); while (cells.hasNext()) { HSSFCell cell = (HSSFCell) cells.next(); switch (cell.getCellType()) { case HSSFCell.CELL_TYPE_NUMERIC: { // cellule numérique System.out.println("ligne : " + row.getRowNum() + " ; cellule : " + cell.getCellNum() + " " + " " + cell.getNumericCellValue()); break; } case HSSFCell.CELL_TYPE_STRING: { // cellule string HSSFRichTextString richTextString = cell.getRichStringCellValue(); System.out.println("ligne : " + row.getRowNum() + " ; cellule : " + cell.getCellNum() + " " + " " + richTextString.getString()); break; } case HSSFCell.CELL_TYPE_BLANK: { // Cellule vide System.out.println("ligne : " + row.getRowNum() + " ; cellule : " + cell.getCellNum() + " " + " Cellule vide"); break; } default: { // Type non géré System.out.println("Type non supporté"); break; } } } } } catch (IOException e) { System.out.println(e); } } public static void main(String[] args) throws IOException { ExcelReader poiExample = new ExcelReader(); String xlsPath = "W:\\stages\\2011_stage_TAR\\13_REFERENCES\\13_11_EXTRACTION_26E\\extraction_26E_S07\\Listage des partitions et PLI associees pour OLNC-IBNF-ITE.xls"; poiExample.persitExcelDataFrom(xlsPath); } }
C'est la que je bloque, je fais quoi avec mes classes entité et controller générés à partir de ma base ?
Partager