I have a table in MYSQL and I am using JDBC Templates to do an insert into that table.

我在MYSQL中有一个表,我使用JDBC模板在该表中插入。

One of the columns has a default value, and I am not specifying it in the Map<String, Object> parameters map.

其中一列有一个默认值,我没有在Map 参数图中指定它。

I am getting an exception Column 'colName' cannot be null.

我收到异常列'colName'不能为null。

Can anyone explain this please?

有人能解释一下吗?

Thanks

*Edit: code *

*编辑:代码*

contactDetailsInsertTemplate = new SimpleJdbcInsert( dataSource ).withTableName("contactdetails").usingGeneratedKeyColumns("contactcode");
Map<String, Object> parameters = new HashMap<String, Object>();
Number newId = contactDetailsInsertTemplate.executeAndReturnKey(parameters);

4 个解决方案

#1


12

You need to limit the columns specified in the SQL Insert.

您需要限制SQL Insert中指定的列。

See SimpleJdbcInsert.usingColumns()

If you don't do this, the SQL statement will need values for ALL columns and the default values cannot be used.

如果不这样做,SQL语句将需要所有列的值,并且不能使用默认值。

e.g. use

insert into mytable (col1, col2) values (val1, val2);

instead of

insert into mytable values (val1, val2);

更多相关文章

  1. 准备语句SQL异常“无输入参数”
  2. java7 switch语句使用字符串
  3. java通过poi模板导出excel
  4. 如何通过Java将Digital Persona的模板保存到数据库中
  5. Java日志框架——查看“完整的执行的SQL语句”

随机推荐

  1. mysql进阶(十一)外键在数据库中的作用
  2. 如何启用对MySQL服务器的外部访问?
  3. php 连接数据库 Warning: mysqli_connect
  4. XAMPP附带的mysql如何修改密码
  5. MySQL 转换函数与运算符
  6. MySQL数据库相关开发入门
  7. php&input复选框存储每个循环的值
  8. MySQL使用二进制日志来恢复数据
  9. windows下安装mysql5.6解压版,32位和64位
  10. Mysql的存储引擎之:MyISAM存储引擎