【问题标题】:Java Date Formatter not working while saving Excel date data into MySQLJava Date Formatter 在将 Excel 日期数据保存到 MySQL 时不起作用
【发布时间】:2017-12-03 21:20:56
【问题描述】:

我的目标是将excel中的每一行数据保存到MySQL数据库中。在我的 excel 文件中,日期格式是 dd.MM.yyyy,而 yyyy-MM-dd 是 MySQL 中的日期格式。

这是我的代码:

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelReader {

    public static void main(String[] args) throws Exception {

        try {

            Class.forName("com.mysql.jdbc.Driver");
            Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/activity", "root", "password");
            con.setAutoCommit(false);
            PreparedStatement pstm = null ;
            FileInputStream input = new FileInputStream("testing.xlsx");
            //POIFSFileSystem fs = new POIFSFileSystem( input );
            XSSFWorkbook wb = new XSSFWorkbook(input);
            XSSFSheet sheet = wb.getSheetAt(0);
            Row row;
            for(int i=1; i<=sheet.getLastRowNum(); i++){
                row = sheet.getRow(i);
                String activityName= row.getCell(5).getStringCellValue();
                Integer activityAllocation = (int) row.getCell(8).getNumericCellValue();
                //DataFormatter dataFormatter = new DataFormatter();
                //String activityDate = dataFormatter.formatCellValue(row.getCell(10));
                Date activityDate = row.getCell(10).getDateCellValue();                
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                String Date = sdf.format(activityDate);
                String activityPersonInCharge = row.getCell(11).getStringCellValue();

                String sql = "INSERT INTO activity(ACTIVITY_NAME,ACTIVITY_ALLOCATION,ACTIVITY_DATE,ACTIVITY_MADE_BY) VALUES('"+activityName+"','"+activityAllocation +"','"+activityDate +"','"+activityPersonInCharge +"')";
                pstm = (PreparedStatement) con.prepareStatement(sql);
                pstm.execute();
                System.out.println("Import rows "+i);
            }
            con.commit();
            pstm.close();
            con.close();
            input.close();
            System.out.println("Success import excel to mysql table");
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }

}

我尝试格式化 Excel 单元格的日期,但无法正常工作。例外:

Exception in thread "main" java.lang.IllegalStateException: Cannot get a NUMERIC value from a STRING cell
    at org.apache.poi.xssf.usermodel.XSSFCell.typeMismatch(XSSFCell.java:1062)
    at org.apache.poi.xssf.usermodel.XSSFCell.getNumericCellValue(XSSFCell.java:307)
    at org.apache.poi.xssf.usermodel.XSSFCell.getDateCellValue(XSSFCell.java:776)
    at ExcelReader.main(ExcelReader.java:35)

我做错了代码的哪一部分?此外,如果我使用日期格式化程序的注释代码行,则不会显示异常,但它会保存错误的日期。请指导我。提前致谢

【问题讨论】:

  • 除了读取异常之外,您似乎正在尝试从基于文本的单元格中读取数值 - 您是否尝试将其读取为 String (并查看它返回的内容) ?
  • 你应该在你的数据库中插入一个实际的日期对象,而不是一个格式化的字符串......但是你没有正确使用PreparedStatement:P
  • @MadProgrammer 我尝试使用字符串,但它将错误的日期保存到 MySQL 中。例如,在 excel 中,日期是 19.06.2014,但在 MySQL 中是 2019-06-20。
  • @ScaryWombat 第 35 行是日期 activityDate = row.getCell(10).getDateCellValue();
  • @atomSmasher 它显示“无法从字符串转换为整数”

标签: java mysql excel date


【解决方案1】:

获取字符串形式的值

String activityDate = row.getCell(10).getStringCellValue();                

根据你的格式是19.06.2014

所以

SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");

然后将其解析为日期

Date dt = sdf.parse(activityDate);

使用 PreparedStatment 插入

设置值(第 3 列?)

ps.setDate (3, dt);

【讨论】:

  • 感谢您的指导。最后一步出现错误:“PreparedStatement 类型中的方法 setDate(int, Date) 不适用于参数 (int, String)”。 activityDate 是一个字符串数据类型。我应该将其更改为日期吗?
  • 再次阅读 Brian ps.setDate (3, dt); dt 是一个日期,但是您需要转换为 java.sql.Date new java.sql.Date (dt.getTime())
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-25
  • 1970-01-01
相关资源
最近更新 更多