我用的是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:[email protected]">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:[email protected]">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)请各位帮小弟看下,多谢!