jihane jihane
Messages postés65Date d'inscriptionsamedi 29 octobre 2011StatutMembreDernière intervention11 avril 2012
-
11 avril 2012 à 23:35
Bonjour,
voici un code que j'ai réalisé pour importer un fichier excel a ma base de donnée sql mon problème et comment je ferait appel à la fonction qui permet de remplir ma base de donnée SQL c'est à dire la fonction(saveToDatabase(Vector dataHolder) je sais pas comment faire dans le main pour affecter a cette fonction mon fichier excel pour remplir ma base de donnée . et merci pour votr future réponse
*******************************************************************
package projetfinetude;
import com.mysql.jdbc.Statement;
import java.io.File;
import java.io.IOException;
public void setInputFile(String inputFile) {
this.inputFile = inputFile;
}
public void read() throws IOException {
File inputWorkbook = new File(inputFile);
Workbook w;
try {
w = Workbook.getWorkbook(inputWorkbook);
// Get the first sheet
Sheet sheet = w.getSheet(0);
// Loop over first 10 column and lines
for (int j = 0; j < sheet.getColumns(); j++) {
for (int i = 0; i < sheet.getRows(); i++) {
Cell cell = sheet.getCell(j, i);
CellType type = cell.getType();
if (cell.getType() == CellType.LABEL) {
System.out.println("I got a label "
+ cell.getContents());
}
if (cell.getType() == CellType.NUMBER) {
System.out.println("I got a number "
+ cell.getContents());
}
private static void saveToDatabase(Vector dataHolder)*********ICI*******
{
String name="";
String address="";
for (int i=0;i<dataHolder.size(); i++){
Vector cellStoreVector=(Vector)dataHolder.elementAt(i);
for (int j=0; j < cellStoreVector.size();j++){
HSSFCell myCell = (HSSFCell)cellStoreVector.elementAt(j);
String st = myCell.toString();
name=st.substring(0);
address=st.substring(0);
}
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
connexion con = (connexion) DriverManager.getConnection("jdbc:mysql://localhost:3306/concours","root", "geek");
Statement stat=con.createStatement();
int k=stat.executeUpdate("insert into nana(N,Nom) value('"+name+"','"+address+"')");
System.out.println("Data is inserted");
stat.close();
// con.close();
}
catch(Exception e){}
}
}
public static void main(String[] args) throws IOException {
a test = new a();
test.setInputFile("C:\\Users\\jihane\\Desktop\\"
+ "jihane.xls");
test.read();