一、错误描述
向mysql数据库中插入中文,结果报错:com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '20170511-14390-???' for key 'PRIMARY'
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '20170511-14390-???' for key 'PRIMARY'
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:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1041)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4237)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4169)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2617)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2778)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2825)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2156)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2459)
at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:2007)
... 14 more
19/02/04 13:00:40 INFO Executor: Finished task 175.0 in stage 6.0 (TID 642). 3864 bytes result sent to driver
19/02/04 13:00:40 INFO TaskSetManager: Finished task 175.0 in stage 6.0 (TID 642) in 933 ms on localhost (executor driver) (200/200)
19/02/04 13:00:40 INFO TaskSchedulerImpl: Removed TaskSet 6.0, whose tasks have all completed, from pool
19/02/04 13:00:40 INFO DAGScheduler: ResultStage 6 (foreachPartition at TopNStatJob.scala:117) finished in 62.489 s
19/02/04 13:00:40 INFO DAGScheduler: Job 2 finished: foreachPartition at TopNStatJob.scala:117, took 67.643300 s
19/02/04 13:00:40 INFO SparkUI: Stopped Spark web UI at http://169.254.197.54:4040
19/02/04 13:00:40 INFO MapOutputTrackerMasterEndpoint: MapOutputTrackerMasterEndpoint stopped!
19/02/04 13:00:41 INFO MemoryStore: MemoryStore cleared
19/02/04 13:00:41 INFO BlockManager: BlockManager stopped
19/02/04 13:00:41 INFO BlockManagerMaster: BlockManagerMaster stopped
19/02/04 13:00:41 INFO OutputCommitCoordinator$OutputCommitCoordinatorEndpoint: OutputCommitCoordinator stopped!
19/02/04 13:00:41 INFO SparkContext: Successfully stopped SparkContext
19/02/04 13:00:41 INFO ShutdownHookManager: Shutdown hook called
19/02/04 13:00:41 INFO ShutdownHookManager: Deleting directory C:\Users\Administrator\AppData\Local\Temp\spark-5dc5e5ff-5ff6-4ece-b21a-892b8d0e744b
二、问题原因
mysql数据库默认编码格式是不支持中文的,需要修改编码格式为utf-8
三、解决办法
1.修改/etc/my.cnf文件
vi /etc/my.cnf
2.在[mysqld]下面添加下面的即可!
character_set_server=utf8
init_connect='SET NAMES utf8'
3.重启mysql
systemctl restart mysqld.service
4.删除之前数据库,重新建表即可
测试成功~
参考blog:https://blog.csdn.net/u010886217/article/details/84163944