[i]Galera to com uma duvida aqui relacionada a upload e inserção no banco.
Vamos la.
meu sistema deve fazer o seguinte.
Receber um arquivo em excel por upload, fazera leitura do msm e inserir no banco as informações.
a parte de leitura e inserção no banco ja esta praticamente feita, segue :[/i]
String filename = caminho;
FileInputStream fis = null;
try {
fis = new FileInputStream(filename);
HSSFWorkbook workbook = new HSSFWorkbook(fis);
HSSFSheet sheet = workbook.getSheetAt(0);
Iterator<?> rows = sheet.rowIterator();
while (rows.hasNext()) {
sql = new StringBuffer();
sql.append("INSERT INTO "
+ Patios.class.getSimpleName()
+ " ( CAMPOS A SEREM INSERIOS );
sql.append("VALUES (");
HSSFRow row = (HSSFRow) rows.next();
Iterator<?> cells = row.cellIterator();
while (cells.hasNext()) {
HSSFCell cell = (HSSFCell) cells.next();
int type = cell.getCellType();
if (type == HSSFCell.CELL_TYPE_STRING) {
System.out.println(cell.getRichStringCellValue()
.toString());
sql.append(!cells.hasNext() ? "'"
+ cell.getRichStringCellValue().toString()
+ "'" : "'"
+ cell.getRichStringCellValue().toString()
+ "',");
} else if (type == HSSFCell.CELL_TYPE_NUMERIC) {
System.out.println(cell.getNumericCellValue());
sql.append(!cells.hasNext() ? ""
+ cell.getNumericCellValue() + "'" : "'"
+ cell.getNumericCellValue() + "',");
} else if (type == HSSFCell.CELL_TYPE_BLANK) {
System.out.println("''");
sql.append(!cells.hasNext() ? "''" : "'',");
}
}
sql.append(")");
System.out.println("SQL A SER EXECUTADO : " + sql.toString());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (fis != null) {
fis.close();
}
}
Criei uma classe de teste :
public static void main(String[] args) throws Exception {
new PatioDao().adiciona("tbPatio2.xls");
}
[i]Porém este arquivo ja esta no diretorio do meu projeto.
Minha duvida eh a seguinte…
Eh possivel eu fazer o upload de um arquivo excel e le-lo em tempo de execução?
Estes arquivos de upload seram colocados em um diretorio do meu projeto…
E outra coisa, como o vraptor possui injeção de depencia, eu consigo fazer isso?[/i]
Query query = session.createSQLQuery(sql.toString());
query.executeUpdate();
transaction.commit();
Alguem sabe??