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á
Z?é`U?/'4?????…1ú?D,! d0???à§??!
A??? Fo???9I?Fy‰3?! e???à' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.Util.getInstance(Util.java:381)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1030)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3558)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3490)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1959)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2109)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2648)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2077)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2362)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2280)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2265)
at forum.huhai.DBAccess.executePreparedStatement(DBAccess.java:124)
at forum.test.InsertImageTest.inserImage(InsertImageTest.java:56)
at forum.test.InsertImageTest.main(InsertImageTest.java:22)
弄了半天也没找出来错哪了,后来写了个独立的测试程序:
- public static void inserImage() throws FileNotFoundException {
- DBAccess access=new DBAccess("insert into imagetable(image)values(?)",DBAccess.DBAccess_CONST.PREPARESTATEMENT);
- String filepath="d://snap2.bmp";
- filepath="D://我的文档//workspace//AIForum//WebRoot//forum//img//defaultPhoto.gif";
- System.out.println("文件路径:"+filepath);
- File file=new File(filepath);
- InputStream inputStream=new FileInputStream(file);
- //InputStream inputStream=null;
- try {
- access.setBlob(1, inputStream);
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- if(access.executePreparedStatement())
- System.out.println("插入图片成功");
- else
- System.out.println("插入图片失败");
- }
插入snap2.bmp成功,但是换别的图片就是不行,我还以为是格式问题,但同样格式的其他bmp图片也不能成功,真是觉得奇怪。于是就在百度搜“插入图片 MySQLSyntaxErrorException”,终于找到一篇相同问题并有解决方案的文章:http://topic.csdn.net/u/20090715/00/9d4a9bd7-5a14-4c94-9d88-158202c07f07.html,文中提到将数据库编码GBK改为GB2312,我以前是GB2312,后看到GBK兼容繁体中文和简体中文,就在重建数据库时将数据库编码换为GBK了,没想到会出现这么个问题。http://blog.csdn.net/yakoo5/archive/2009/07/15/4349670.aspx这篇文章中提到这么一句话:MySQL将字符集设为GBK时,使用参数更新或插入中的字符串字段参数会引起异常,将字符集改为GB2312就可以了。
最终解决办法:
1.将数据库GBK编码改为GB2312;
2.重启MySQL服务。
修改数据库的GBK编码的方法:
1,在cmd中输入 :net stop mysql ,关闭数据库服务
2,找到mysql的安装路径,并找到my.ini 文件,将default 的编码改为 gb2312
3, 重启数据库服务: net start mysql
4,删除以前的表,重新建表