NutzCN Logo
问答 nutzboot 插入数据报错
发布于 2219天前 作者 文涛(wentao) 1175 次浏览 复制 上一个帖子 下一个帖子
标签:

同样的语句,为什么一个成功,一个报错,很费解

[DEBUG] 11:02:57.912 org.nutz.dao.impl.sql.run.NutDaoExecutor.printSQL(NutDaoExecutor.java:388) - INSERT INTO ad_module(name,url,pre,iconClass,createTime,updateTime) VALUES(?,?,?,?,?,?) 
    |    1 |                  2 |    3 |          4 |                   5 |                   6 |
    |------|--------------------|------|------------|---------------------|---------------------|
    | 用户管理 | /manage/user/index | user | fa fa-user | 2018-10-19 11:02:57 | 2018-10-19 11:02:57 |
  For example:> "INSERT INTO ad_module(name,url,pre,iconClass,createTime,updateTime) VALUES('用户管理','/manage/user/index','user','fa fa-user','2018-10-19 11:02:57','2018-10-19 11:02:57') "
[DEBUG] 11:02:58.044 org.nutz.dao.impl.interceptor.DaoTimeInterceptor.filter(DaoTimeInterceptor.java:28) - time=131ms, sql=INSERT INTO ad_module(name,url,pre,iconClass,createTime,updateTime) VALUES('用户管理','/manage/user/index','user','fa fa-user','2018-10-19 11:02:57','2018-10-19 11:02:57') 
[DEBUG] 11:02:58.045 org.nutz.dao.impl.sql.run.NutDaoExecutor.printSQL(NutDaoExecutor.java:388) - INSERT INTO ad_module(name,url,pre,iconClass,createTime,updateTime) VALUES(?,?,?,?,?,?) 
    |    1 |                  2 |    3 |           4 |                   5 |                   6 |
    |------|--------------------|------|-------------|---------------------|---------------------|
    | 地区管理 | /manage/city/index | city | fa fa-globe | 2018-10-19 11:02:58 | 2018-10-19 11:02:58 |
  For example:> "INSERT INTO ad_module(name,url,pre,iconClass,createTime,updateTime) VALUES('地区管理','/manage/city/index','city','fa fa-globe','2018-10-19 11:02:58','2018-10-19 11:02:58') "
[DEBUG] 11:02:58.186 org.nutz.dao.impl.sql.run.NutDaoExecutor.exec(NutDaoExecutor.java:97) - SQLException
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '????' for key 'name'
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
	at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
	at com.mysql.jdbc.Util.getInstance(Util.java:408)
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:935)
	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3973)
	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3909)
	at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2527)
	at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2680)
	at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2487)
	at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1858)
	at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1197)
	at com.alibaba.druid.filter.FilterChainImpl.preparedStatement_execute(FilterChainImpl.java:3409)
	at com.alibaba.druid.filter.FilterEventAdapter.preparedStatement_execute(FilterEventAdapter.java:440)
	at com.alibaba.druid.filter.FilterChainImpl.preparedStatement_execute(FilterChainImpl.java:3407)
	at com.alibaba.druid.proxy.jdbc.PreparedStatementProxyImpl.execute(PreparedStatementProxyImpl.java:167)
	at com.alibaba.druid.pool.DruidPooledPreparedStatement.execute(DruidPooledPreparedStatement.java:498)
	at org.nutz.dao.impl.sql.run.NutDaoExecutor._runPreparedStatement(NutDaoExecutor.java:308)
	at org.nutz.dao.impl.sql.run.NutDaoExecutor.exec(NutDaoExecutor.java:90)
	at org.nutz.dao.DaoInterceptorChain.doChain(DaoInterceptorChain.java:66)
	at org.nutz.dao.impl.interceptor.DaoTimeInterceptor.filter(DaoTimeInterceptor.java:23)
	at org.nutz.dao.DaoInterceptorChain.doChain(DaoInterceptorChain.java:64)
	at org.nutz.dao.impl.interceptor.DaoLogInterceptor.filter(DaoLogInterceptor.java:22)
	at org.nutz.dao.DaoInterceptorChain.doChain(DaoInterceptorChain.java:64)
	at org.nutz.dao.DaoInterceptorChain.invoke(DaoInterceptorChain.java:139)
	at org.nutz.dao.impl.sql.run.NutDaoRunner.runCallback(NutDaoRunner.java:159)
	at org.nutz.dao.impl.NutTxDao$1._run(NutTxDao.java:68)
	at org.nutz.dao.impl.sql.run.NutDaoRunner.run(NutDaoRunner.java:82)
	at org.nutz.dao.impl.DaoSupport.run(DaoSupport.java:240)
	at org.nutz.dao.impl.DaoSupport._exec(DaoSupport.java:252)
	at org.nutz.dao.impl.EntityOperator.exec(EntityOperator.java:56)
	at org.nutz.dao.impl.NutDao.insert(NutDao.java:152)
	at com.iptv.ad_auth_manage.telecom.utils.SysUtil.initModule(SysUtil.java:133)
	at com.iptv.ad_auth_manage.telecom.utils.SysUtil.initSystem(SysUtil.java:48)
	at com.iptv.ad_auth_manage.telecom.MainLauncher.init(MainLauncher.java:25)
	at com.iptv.ad_auth_manage.telecom.MainLauncher$FM$init$cff301be46c87e2fbea60f4ef344bacb.invoke(MainLauncher.java)
	at org.nutz.ioc.impl.ObjectMakerImpl$2.trigger(ObjectMakerImpl.java:180)
	at org.nutz.ioc.weaver.DefaultWeaver.onCreate(DefaultWeaver.java:89)
	at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:141)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:241)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:271)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:161)
	at org.nutz.boot.NbApp.execute(NbApp.java:209)
	at org.nutz.boot.NbApp.run(NbApp.java:178)
	at com.iptv.ad_auth_manage.telecom.MainLauncher.main(MainLauncher.java:53)
[DEBUG] 11:02:58.189 org.nutz.dao.impl.interceptor.DaoTimeInterceptor.filter(DaoTimeInterceptor.java:28) - time=144ms, sql=INSERT INTO ad_module(name,url,pre,iconClass,createTime,updateTime) VALUES('地区管理','/manage/city/index','city','fa fa-globe','2018-10-19 11:02:58','2018-10-19 11:02:58') 
[ERROR] 11:02:58.191 com.iptv.ad_auth_manage.telecom.utils.SysUtil.initSystem(SysUtil.java:105) - 初始化数据出错:org.nutz.dao.DaoException: !Nutz SQL Error: 'INSERT INTO ad_module(name,url,pre,iconClass,createTime,updateTime) VALUES('地区管理','/manage/city/index','city','fa fa-globe','2018-10-19 11:02:58','2018-10-19 11:02:58') '
PreparedStatement: 
'INSERT INTO ad_module(name,url,pre,iconClass,createTime,updateTime) VALUES(?,?,?,?,?,?) '
CaseMessage=Duplicate entry '????' for key 'name'
	at org.nutz.dao.impl.sql.run.NutDaoExecutor.exec(NutDaoExecutor.java:104)
	at org.nutz.dao.DaoInterceptorChain.doChain(DaoInterceptorChain.java:66)
	at org.nutz.dao.impl.interceptor.DaoTimeInterceptor.filter(DaoTimeInterceptor.java:23)
	at org.nutz.dao.DaoInterceptorChain.doChain(DaoInterceptorChain.java:64)
	at org.nutz.dao.impl.interceptor.DaoLogInterceptor.filter(DaoLogInterceptor.java:22)
	at org.nutz.dao.DaoInterceptorChain.doChain(DaoInterceptorChain.java:64)
	at org.nutz.dao.DaoInterceptorChain.invoke(DaoInterceptorChain.java:139)
	at org.nutz.dao.impl.sql.run.NutDaoRunner.runCallback(NutDaoRunner.java:159)
	at org.nutz.dao.impl.NutTxDao$1._run(NutTxDao.java:68)
	at org.nutz.dao.impl.sql.run.NutDaoRunner.run(NutDaoRunner.java:82)
	at org.nutz.dao.impl.DaoSupport.run(DaoSupport.java:240)
	at org.nutz.dao.impl.DaoSupport._exec(DaoSupport.java:252)
	at org.nutz.dao.impl.EntityOperator.exec(EntityOperator.java:56)
	at org.nutz.dao.impl.NutDao.insert(NutDao.java:152)
	at com.iptv.ad_auth_manage.telecom.utils.SysUtil.initModule(SysUtil.java:133)
	at com.iptv.ad_auth_manage.telecom.utils.SysUtil.initSystem(SysUtil.java:48)
	at com.iptv.ad_auth_manage.telecom.MainLauncher.init(MainLauncher.java:25)
	at com.iptv.ad_auth_manage.telecom.MainLauncher$FM$init$cff301be46c87e2fbea60f4ef344bacb.invoke(MainLauncher.java)
	at org.nutz.ioc.impl.ObjectMakerImpl$2.trigger(ObjectMakerImpl.java:180)
	at org.nutz.ioc.weaver.DefaultWeaver.onCreate(DefaultWeaver.java:89)
	at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:141)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:241)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:271)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:161)
	at org.nutz.boot.NbApp.execute(NbApp.java:209)
	at org.nutz.boot.NbApp.run(NbApp.java:178)
	at com.iptv.ad_auth_manage.telecom.MainLauncher.main(MainLauncher.java:53)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '????' for key 'name'
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
	at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
	at com.mysql.jdbc.Util.getInstance(Util.java:408)
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:935)
	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3973)
	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3909)
	at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2527)
	at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2680)
	at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2487)
	at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1858)
	at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1197)
	at com.alibaba.druid.filter.FilterChainImpl.preparedStatement_execute(FilterChainImpl.java:3409)
	at com.alibaba.druid.filter.FilterEventAdapter.preparedStatement_execute(FilterEventAdapter.java:440)
	at com.alibaba.druid.filter.FilterChainImpl.preparedStatement_execute(FilterChainImpl.java:3407)
	at com.alibaba.druid.proxy.jdbc.PreparedStatementProxyImpl.execute(PreparedStatementProxyImpl.java:167)
	at com.alibaba.druid.pool.DruidPooledPreparedStatement.execute(DruidPooledPreparedStatement.java:498)
	at org.nutz.dao.impl.sql.run.NutDaoExecutor._runPreparedStatement(NutDaoExecutor.java:308)
	at org.nutz.dao.impl.sql.run.NutDaoExecutor.exec(NutDaoExecutor.java:90)
	... 26 more
12 回复

那数据库表里面有"地区管理"没?

// 初始化模块
List<Integer> moduleIdList = new ArrayList<>();
if(nutTxDao.count(ModuleModel.class) <= 0) {
    moduleIdList.add(initModule(nutTxDao, "用户管理", "/manage/user/index", "user", "fa fa-user"));
    moduleIdList.add(initModule(nutTxDao, "地区管理", "/manage/city/index", "city", "fa fa-globe"));
    moduleIdList.add(initModule(nutTxDao, "配置管理", "/manage/config/index", "config", "fa fa-edit"));
    moduleIdList.add(initModule(nutTxDao, "模块管理", "/manage/module/index", "module", "fa fa-th"));
    moduleIdList.add(initModule(nutTxDao, "角色管理", "/manage/role/index", "role", "fa fa-user-secret"));
    moduleIdList.add(initModule(nutTxDao, "客户管理", "/manage/account/index", "account", "fa fa-users"));
    moduleIdList.add(initModule(nutTxDao, "城市IP管理", "/manage/ipCity/index", "ipCity", "fa fa-list"));
    moduleIdList.add(initModule(nutTxDao, "投放策略管理", "/manage/strategry/index", "strategry", "fa fa-exchange"));
    moduleIdList.add(initModule(nutTxDao, "频道管理", "/manage/channel/index", "channel", "fa fa-newspaper-o"));
}
private int initModule(NutTxDao nutTxDao, String moduleName, String url, String pre, String iconClass) {
    ModuleModel moduleModel = new ModuleModel();
    moduleModel.setName(moduleName);
    moduleModel.setUrl(url);
    moduleModel.setPre(pre);
    moduleModel.setIconClass(iconClass);
    moduleModel.modiCreateTime();
    moduleModel.modiUpdateTime();
    nutTxDao.insert(moduleModel);
    return moduleModel.getId();
}

就是报错后,再到数据库看看, 有没有这个记录

Duplicate entry '????'

感觉是乱码了

启动的时候, 有一段MySQL的信息输出, 找找看

表是空的,报错我rollback了

应该是编码问题

[INFO ] 11:02:54.819 com.alibaba.druid.pool.DruidDataSource.init(DruidDataSource.java:930) - {dataSource-1} inited
[DEBUG] 11:02:55.861 org.nutz.dao.impl.DaoSupport.setDataSource(DaoSupport.java:188) - select expert : org.nutz.dao.impl.jdbc.mysql.MysqlJdbcExpert
[DEBUG] 11:02:55.868 org.nutz.dao.impl.DaoSupport$1.invoke(DaoSupport.java:199) - JDBC Driver --> mysql-connector-java-5.1.44 ( Revision: b3cda4f864902ffdde495b9df93937c3e20009be )
[DEBUG] 11:02:55.868 org.nutz.dao.impl.DaoSupport$1.invoke(DaoSupport.java:200) - JDBC Name   --> MySQL Connector Java
[DEBUG] 11:02:55.868 org.nutz.dao.impl.DaoSupport$1.invoke(DaoSupport.java:202) - JDBC URL    --> jdbc:mysql://127.0.0.1/ad_auth
[DEBUG] 11:02:55.957 org.nutz.dao.impl.jdbc.mysql.MysqlJdbcExpert.checkDataSource(MysqlJdbcExpert.java:212) - Mysql : character_set_client=latin1
[DEBUG] 11:02:55.957 org.nutz.dao.impl.jdbc.mysql.MysqlJdbcExpert.checkDataSource(MysqlJdbcExpert.java:212) - Mysql : character_set_connection=latin1
[DEBUG] 11:02:55.957 org.nutz.dao.impl.jdbc.mysql.MysqlJdbcExpert.checkDataSource(MysqlJdbcExpert.java:212) - Mysql : character_set_database=latin1
[DEBUG] 11:02:55.957 org.nutz.dao.impl.jdbc.mysql.MysqlJdbcExpert.checkDataSource(MysqlJdbcExpert.java:212) - Mysql : character_set_filesystem=binary
[DEBUG] 11:02:55.957 org.nutz.dao.impl.jdbc.mysql.MysqlJdbcExpert.checkDataSource(MysqlJdbcExpert.java:212) - Mysql : character_set_results=
[DEBUG] 11:02:55.958 org.nutz.dao.impl.jdbc.mysql.MysqlJdbcExpert.checkDataSource(MysqlJdbcExpert.java:212) - Mysql : character_set_server=latin1
[DEBUG] 11:02:55.958 org.nutz.dao.impl.jdbc.mysql.MysqlJdbcExpert.checkDataSource(MysqlJdbcExpert.java:212) - Mysql : character_set_system=utf8
[DEBUG] 11:02:55.958 org.nutz.dao.impl.jdbc.mysql.MysqlJdbcExpert.checkDataSource(MysqlJdbcExpert.java:212) - Mysql : character_sets_dir=/usr/share/mysql/charsets/
[DEBUG] 11:02:56.023 org.nutz.dao.impl.jdbc.mysql.MysqlJdbcExpert.checkDataSource(MysqlJdbcExpert.java:219) - Mysql : binlog_format=MIXED
[DEBUG] 11:02:56.089 org.nutz.dao.impl.jdbc.mysql.MysqlJdbcExpert.checkDataSource(MysqlJdbcExpert.java:226) - Mysql : database=ad_auth
[DEBUG] 11:02:56.154 org.nutz.dao.impl.jdbc.mysql.MysqlJdbcExpert.checkDataSource(MysqlJdbcExpert.java:232) - Mysql : user=ad_auth@192.168.8.90
[DEBUG] 11:02:56.241 org.nutz.dao.impl.DaoSupport.setDataSource(DaoSupport.java:221) - Database info --> MYSQL:[MySQL - 5.6.32-log]

确实编码问题,我加了 ?useUnicode=true&characterEncoding=UTF-8 可以用了

添加回复
请先登陆
回到顶部