【问题标题】:com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect date valuecom.mysql.jdbc.MysqlDataTruncation:数据截断:日期值不正确
【发布时间】:2019-12-26 18:57:53
【问题描述】:

谁能指导我在 MySQL 数据库中执行查询代码,我在 JSP 项目中工作,当我尝试执行查询时出现以下异常:

com.mysql.jdbc.MysqlDataTruncation:数据截断:不正确的日期值:第 1 行的列“dft”的“%-12-31”

代码:

try {
            connexion = ServletCalendrier.getConnexion();

            if(connexion == null) {
                System.err.println("Une demande de connexion a ete faite au serveur mais n'a pas abouti");
            }

            initFeries();

            String anneePlus = Integer.parseInt(annee)+1+"%";
            query ="DELETE FROM TgCalFac WHERE annee LIKE '"+anneePlus+"' AND versionFac LIKE '"+version+"' AND dft LIKE '%-12-31'";
            result = Queries.executeQuery(query);

            connexion = ServletCalendrier.getConnexion();
            pstmt = connexion.prepareStatement("SELECT count(*) as nombre FROM TgDft");     
            rset = pstmt.executeQuery();



            if(rset.next())
                nbJoursDft = rset.getInt("nombre");

            pstmt.close();
            pstmt = null;

            // Insertion du premier lot

            dft = new Dft(OperationsSurDates.stringToDate(String.valueOf((Integer.valueOf(annee)-1)) + "-12-31","yyyy-MM-dd"));
            drmf = new Drmf(dft.getDft());

            extraction = new Extraction(drmf);
            ps2 = new PS2(extraction,ps2Prec);
            dateFacture = new DateFacture(ps2);

            delai1 = OperationsSurDates.differenceDates(dateFacture.getDateFacture1(), dft.getDft());
            delai2 = OperationsSurDates.differenceDates(dateFacture.getDateFacture2(), dft.getDft());


            drmf1Type = 0;
            if(drmf.isDrmf1R()){
                drmf1Type = 1;
            }
            if(drmf.isDrmf1L()){
                drmf1Type = 2;
            }

            drmf2Type = 0;
            if(drmf.isDrmf2R()){
                drmf2Type = 1;
            }
            if(drmf.isDrmf2L()){
                drmf2Type = 2;
            }

【问题讨论】:

    标签: java maven jsp mysql-workbench mysql-connector


    【解决方案1】:

    而不是% 使用DAYOFMONTH 即:代表日和MONTH 即:代表月,因为您的year 值可以是任何值。因此您的查询将如下所示:

     query ="DELETE FROM TgCalFac 
            WHERE 
            annee LIKE '"+anneePlus+"' 
            AND 
            versionFac LIKE '"+version+"'
            AND DAYOFMONTH(dft) = 31 AND MONTH(dft) = 12";
    

    检查示例here

    【讨论】:

      猜你喜欢
      • 2016-03-23
      • 1970-01-01
      • 1970-01-01
      • 2011-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-25
      相关资源
      最近更新 更多