java - 如何使用jdbc连接将excel表中的数据存储到mysql数据库中

标签 java mysql excel jdbc

我已经编写了从 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 控制台,但我不知道如何存储在数据库中

请告诉我如何使用JDBC将数据存入mysql数据库

提前致谢

最佳答案

由于您有一个要在其中添加单元格值的 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 and Java JDBC - Tutorial

关于java - 如何使用jdbc连接将excel表中的数据存储到mysql数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18721773/

相关文章:

Java 8 API 静态方法在非空输入上运行函数,或返回 nullValue

php - Laravel Eloquent 地从 sql 中查找数组

vba - Excel VBA在图表选项卡上更改表单控件的属性

excel - 在 Excel 中将分钟和秒转换为小时、分钟和秒

java - 使用 deployJava.js 检测 java 版本,弹出 java 升级菜单

java - Spring Data JPA 通过查询从实体获取投影

java - 有效的Java。可克隆界面

mysql连接不同字段

mysql - 从表A到表B的触发器ID

html - 在Excel vba中实现HTMLBaseElement