0

我已经编写了从 Excel 表访问数据的代码,但我无法使用 JDBC 将数据存储到数据库中。我从 excel 表中访问数据的代码

   try{
    List list=new ArrayList();
    String fileName="d:\\menu.xls";
    File file=new File(fileName);
    InputStream input = new BufferedInputStream(new FileInputStream(file));
    POIFSFileSystem fs = new POIFSFileSystem( input );
    HSSFWorkbook wb = new HSSFWorkbook(fs);
    HSSFSheet sheet = wb.getSheetAt(0);
  //int i=0;
 Iterator rows=sheet.rowIterator();
 while(rows.hasNext()){
     HSSFRow row=(HSSFRow)rows.next();
     System.out.println("\n");
     Iterator cells=row.cellIterator();
     while( cells.hasNext() ) {
         HSSFCell cell = (HSSFCell) cells.next();
         if(HSSFCell.CELL_TYPE_NUMERIC==cell.getCellType()){
         System.out.print(cell.getNumericCellValue()+" " );
        // list.add(cell.getNumericCellValue());

         }
         else if (HSSFCell.CELL_TYPE_STRING==cell.getCellType()){
             System.out.print(cell.getStringCellValue()+" " );
             //list.add(cell.getStringCellValue());

         }
         else
             if (HSSFCell.CELL_TYPE_BOOLEAN==cell.getCellType()){
             System.out.print(cell.getBooleanCellValue()+" " );
             //list.add(cell.getBooleanCellValue());

             }
             else
                 if(HSSFCell.CELL_TYPE_BLANK==cell.getCellType()){
                     System.out.print( "BLANK     " );}
                     else
                 System.out.print("Unknown cell type");

     }

 }
System.out.println(list);
}catch(Exception e){
    e.printStackTrace();
}

我将所有数据都输入了 myeclipse 控制台 bt 我不知道如何存储在数据库中

请告诉我如何使用 JDBC 将数据存储到 mysql 数据库中

提前感谢

4

1 回答 1

3

由于您ArrayList在其中添加单元格值,因此可以按以下步骤操作:

public void insertRowInDB(List cellValues){
    Connection con = null;
    PreparedStatement preparedStatement = null;
    try{            
        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection("jdbc:mysql://localhost/databaseName?"
                                      + "user=sqlUser&password=sqlPassword");
         String sql = "INSERT INTO tableName(field1, field2, ..., fieldN) VALUES (?,?,...,?)";
         preparedStatement = con.prepareStatement(sql);
         int paramIndex = 1;
         for(Object cell : cellValues){
             preparedStatement.setObject(paramIndex, cell);
             paramIndex++;
         }
         int status = preparedStatement.executeUpdate();
         //DO something with the status if needed
    } catch(SQLException ex) { 
          /* log the exception */
    } finally {
        try{
            preparedStatemnt.close();
            con.close();
        } catch(SQLException ignored) {}
    }
}

你需要做这个小改动:

while(rows.hasNext()){
    List list = new ArrayList(); // initialize the list here

    HSSFRow row=(HSSFRow)rows.next();
    System.out.println("\n");
    Iterator cells=row.cellIterator();
    while( cells.hasNext() ) {
        /* all your code seems fine here, just uncomment list.add(something) lines */
    }

    insertRowInDB(list); // insert the cells in your database
}

我还建议您查看此MySQL 和 Java JDBC - 教程

于 2013-09-11T11:53:29.773 回答