com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException
使用MySQL保存图片时出现如下错误信息:
Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ?(捥z無髪险uvI贖便?~BO惟荜H?矍=>焃脰F欶w?{Wv:溢炄平X覾避 at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
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:1054)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4190)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4122)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2570)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2731)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2818)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2157)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2460)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2377)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2361)
at cn.com.songjy.test.db.MySQLText.save_binary_stream(MySQLText.java:144)
at cn.com.songjy.test.db.MySQLText.main(MySQLText.java:207)
原因是我的代码是UTF-8,而我的MySQL是GBK,所以出现此错误,解决方案是加上
useUnicode=true&characterEncoding=UTF-8
即
url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8
如下图:
[img]http://dl2.iteye.com/upload/attachment/0090/3776/5f374fd4-9cc1-313e-a67f-81acc931f135.png[/img]
详见:[url=http://www.cnblogs.com/xuefuwu/archive/2012/05/06/2486577.html]mysql 连接url中useUnicode=true&characterEncoding=UTF-8 的作用[/url]
Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ?(捥z無髪险uvI贖便?~BO惟荜H?矍=>焃脰F欶w?{Wv:溢炄平X覾避 at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
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:1054)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4190)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4122)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2570)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2731)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2818)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2157)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2460)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2377)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2361)
at cn.com.songjy.test.db.MySQLText.save_binary_stream(MySQLText.java:144)
at cn.com.songjy.test.db.MySQLText.main(MySQLText.java:207)
原因是我的代码是UTF-8,而我的MySQL是GBK,所以出现此错误,解决方案是加上
useUnicode=true&characterEncoding=UTF-8
即
url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8
如下图:
[img]http://dl2.iteye.com/upload/attachment/0090/3776/5f374fd4-9cc1-313e-a67f-81acc931f135.png[/img]
详见:[url=http://www.cnblogs.com/xuefuwu/archive/2012/05/06/2486577.html]mysql 连接url中useUnicode=true&characterEncoding=UTF-8 的作用[/url]