2011-01-11 5 views
1

Я хочу вставить данные Excel в базу данных, используя этот код.excel java apache poi

public class Insert { 
     public static void main(String [] args) { 
     String fileName="C:\\File.xls"; 
     Vector dataHolder=read(fileName); 
     saveToDatabase(dataHolder); 
    } 
     public static Vector read(String fileName) { 
     Vector cellVectorHolder = new Vector(); 
     try{ 
       FileInputStream myInput = new FileInputStream(fileName); 
       POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput); 
      HSSFWorkbook myWorkBook = new HSSFWorkbook(myFileSystem); 
      HSSFSheet mySheet = myWorkBook.getSheetAt(0); 
      Iterator rowIter = mySheet.rowIterator(); 
      while(rowIter.hasNext()){ 
        HSSFRow myRow = (HSSFRow) rowIter.next(); 
        Iterator cellIter = myRow.cellIterator(); 
        Vector cellStoreVector=new Vector(); 
        while(cellIter.hasNext()){ 
          HSSFCell myCell = (HSSFCell) cellIter.next(); 
          cellStoreVector.addElement(myCell); 
        } 
        cellVectorHolder.addElement(cellStoreVector); 
      } 
     }catch (Exception e){e.printStackTrace(); } 
     return cellVectorHolder; 
    } 
     private static void saveToDatabase(Vector dataHolder) { 
     String username=""; 
       String password=""; 
       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(); 
           username=st.substring(0,1); 
           password=st.substring(0); 
                 } 
         try{ 
    Class.forName("com.mysql.jdbc.Driver").newInstance(); 
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root", "root"); 
     Statement stat=con.createStatement(); 
     int k=stat.executeUpdate("insert into login(username,password) value('"+username+"','"+password+"')"); 
     System.out.println("Data is inserted"); 
     stat.close(); 
     con.close(); 
     } 
     catch(Exception e){} 
     } 
     } 
     } 

Как этот код вводить в моду?

+0

* Что * тип кнопки? Кнопка GUI на основе Java или кнопка в Excel (макрос, ...)? –

+0

Пользовательский графический интерфейс на основе Java. –

+1

Здесь так много неудач, с чего сложно начать ... –

ответ

1

обычно вы должны добавить Действие слушателю кнопки:

Button button = new Button("click me"); 
    button.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
      // call your logic here 
     } 
    });