今天用jdbc on duplicate key update 做批量更新时候一直不成功,但是同样的sql语句在mysql的客户端工具以及命令行都能执行成功。情况大致如下// id:主键 reprints:转发数 comments:评论数
// 数据库还有一个标题字段:title varchar(255) NOT NULL// 目的id为:2245845和2245846的两天数据在数据库中已经存在,现在想要更新 reprints和comments 值分别为:(2245845,3,2),(2245846,4,1)insert into records (id,reprints,comments) values (2245845,3,2),(2245846,4,1) on duplicate key update reprints = values(reprints),comments = values(comments);该语句在客户端工具以及命令行都能执行成功,但是通过jdbc执行时报错:Field 'title' doesn't have a default valuejdbc操作如下:
Statement stat = connection.createStatement();
String sql = "insert into records (id,reprints,comments) values (2245845,3,2),(2245846,4,1) on duplicate key update reprints = values(reprints),comments = values(comments);";
stat.execute(sql);请各位大侠帮忙看看。

解决方案 »

  1.   

    你用一个带jdbc的客户端工具试试,看看是不是一样的问题,如果不是,则是代码的问题,如果是,则是jdbc的问题。
      

  2.   

    该语句在客户端工具以及命令行都能执行成功,但是通过jdbc执行时报错:Field 'title' doesn't have a default value那你指定title列的值试试
      

  3.   


    我想不明白的是客户端和命令行工具都能执行成功,而且我的title列根本不需要更新,为啥要指定值呢,我试过把所有设置为 not null的值都指定的话能够成功,但是这任然没有解决 为啥jdbc执行会报错的问题