使用Blob类型将图片插入MySQL数据库,出现MySQLSyntaxErrorException异常。请问如何解决?谢谢!

yakoo5 2009-07-15 12:33:36
我用的是MySQL Server5.0
使用的驱动是mysql-connect-java-5.0.8-bin.jar.
开发工具:eclipse j2ee 3.4

图片是index.jpg在jdbc项目根目录下,即
jdbc/index.jpg (其中index.jpg < 64KB)
jdbc/src/com/yakoo5/jdbc/JdbcUtils.java
jdbc/src/com/yakoo5/jdbc/BlobTest.java

数据库jdbc的blob_test表创建语句如下:
CREATE TABLE `jdbc`.`blob_test` (
`id` INTEGER NOT NULL AUTO_INCREMENT,
`big_bit` BLOB NOT NULL,
PRIMARY KEY (`id`)
)
ENGINE = InnoDB;

BlobTest.java源代码如下:
package com.yakoo5.jdbc;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
* 2009-7-14
*
* @author <a href="mailto:yakoo5@163.com">yakoo5</a>
*
*/
public class BlogTest {

/**
* @param args
* @throws IOException
* @throws SQLException
*/
public static void main(String[] args) throws SQLException, IOException {
create();

}

static void create() throws SQLException, IOException{
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;

try {
//2.建立连接
conn = JdbcUtils.getConnection();
//3.创建语句
String sql = "insert into blob_test(big_bit) values(?)";
ps = conn.prepareStatement(sql);
File file = new File("index.jpg");
InputStream in = new BufferedInputStream(new FileInputStream(file));
ps.setBinaryStream(1, in, (int)file.length());

//4.执行语句
int i = ps.executeUpdate();

in.close();

System.out.println("i="+i);
} finally {
JdbcUtils.free(rs, ps, conn);
}
}

}


其中使用到的JdbcUtils.java类源代码如下:
package com.yakoo5.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
* 2009-7-12
*
* @author <a href="mailto:yakoo5@163.com">yakoo5</a>
*
*/
public final class JdbcUtils {
private static String url = "jdbc:mysql://localhost:3306/jdbc";
private static String user ="root";
private static String password = "mysql";

private JdbcUtils(){
}

/*
* 转载类时,加载数据库驱动
*/
static{
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
throw new ExceptionInInitializerError(e);
}
}

/*
* 获得数据库连接
*/
public static Connection getConnection() throws SQLException{
return DriverManager.getConnection(url, user, password);
}

/*
* 释放数据库连接
*/
public static void free(ResultSet rs,Statement st,Connection conn){
try{
if(rs!=null)
rs.close();
}catch (SQLException e) {
e.printStackTrace();
}finally{
try{
if(st!=null)
st.close();
}catch (SQLException e) {
e.printStackTrace();
}finally{
if(conn!=null)
try {
conn.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
}
}


异常信息如下:
Exception in thread "main" com.mysql.jdbc.exceptions.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 '??<G?o?Y?ú?\'x????f?t±?ü{?G>S·,?êcê?4???÷?–?=O?è?D???\0e?~?^\'????_??ú???;' at line 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2985)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3283)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1332)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1604)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1519)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1504)
at com.yakoo5.jdbc.BlogTest.create(BlogTest.java:47)
at com.yakoo5.jdbc.BlogTest.main(BlogTest.java:27)

请各位帮小弟看下,多谢!

...全文
1070 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
haoyafei2009 2010-11-07
  • 打赏
  • 举报
回复
还有要不就是你的数据库名是汉语
把数据库名该成英语就行啦
greedsluck 2009-12-25
  • 打赏
  • 举报
回复
我怎么不能成功啊?
yakoo5 2009-07-15
  • 打赏
  • 举报
回复
注:我使用的是JDK1.6版本
yakoo5 2009-07-15
  • 打赏
  • 举报
回复
看来大家对这个都不感兴趣啊!

不过我已经解决了!

只需将MYSQL Server的字符集设置成gb2312就行了。(原先设置的是gbk,可能支持的不是很好)

MySQL将字符集设为GBK时,使用参数更或插入中的字符串字段参数会引起异常,将字符集改为GB2312就可以了。

更多细节,请访问http://blog.csdn.net/yakoo5/archive/2009/07/15/4349670.aspx

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧