java, mysql 将图片写入数据库中,可是老是出现问题,求高手帮忙看看呐

create table user02(
id int auto_increment primary key,
name varchar(30) Not NULL,
photo longblob
);//以上是数据库的

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class 写入图片 {
//定义MySQL的数据库驱动程序
public static final String DBDRIVER="org.gjt.mm.mysql.Driver";
//定义MySQL数据库的连接地址
public static final String DBURL ="jdbc:mysql://localhost:3306/test";
//MySQL数据可的连接用户名
public static final String DBUSER="root";
//MySQL数据库的连接密码
public static final String DBPASS="123456";

public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
Connection conn = null ; // 数据库连接
PreparedStatement pstmt = null ;
String name = "啊啊啊" ;
String sql = "INSERT INTO user02(name,photo) VALUES (?,?) " ;
Class.forName(DBDRIVER) ; // 加载驱动程序
conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS) ;
pstmt = conn.prepareStatement(sql) ;
File f = new File("d:" + File.separator + "my.gif") ; // 图片文件
InputStream input = null ;
input = new FileInputStream(f) ;
pstmt.setString(1,name) ; // 设置第一个“?”的内容
pstmt.setBinaryStream(2,input,(int)f.length()) ; // 设置输入流
pstmt.executeUpdate() ; // 更新数据库
pstmt.close() ;
conn.close() ; // 数据库关闭
}
}

//以下是错误
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 '?è?????á????????????h???????????§??÷?g?????????·????????????à????????¨??à?????' 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:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1052)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3597)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3529)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1990)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2151)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2625)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2119)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2415)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2333)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2318)
at su.java.写入图片.main(写入图片.java:34)

第1个回答  2011-06-01
建议你不要直接把图片写进数据库,这样做效率会很低,你可以数据库里存路径图片放到服务器端一个文件夹里,我们做商业开发一般都这样做
另外你的mysql应该编码格式有问题,重新弄成utf-8编码格式试试
第2个回答  2011-05-31
Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax;
说明你的sql语句语法有问题
建议在数据库的命令行 把sql写好确定无误之后 再拿来用追问

这语法我写过N次了。

第3个回答  2011-06-01
你可以将图片的图片名存在数据库中,然后去取
第4个回答  2011-05-31
你mysql安装的时候是否选择的是utf-8编码.
相似回答