用java 链接mysql数据库 向其中插入jpg图像(blob) 总是出现异常

异常: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 '?I?? (é??v?r???WYS? 4?^Ke??j¨?h?)?h??sHi’?R?U`‰ìQ?áB?-??C??xB????=??' at line 1
源码为:package lobdemo;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 BlobDemo1 { public static final String DBDRIVER="org.gjt.mm.mysql.Driver"; public static final String DBURL="jdbc:mysql://localhost:3306/mydatabase"; public static final String DBUSER="root"; public static final String DBPASS="mysqladmin"; public static void main(String[] args) throws Exception{ Connection conn=null; PreparedStatement pstmt = null; String name ="hello"; String sql = "INSERT INTO userblob(name,photo) VALUES(?,?)"; Class.forName(DBDRIVER); conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS); pstmt = conn.prepareStatement(sql); File f = new File("E:"+File.separator+"2.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(); }}

第1个回答  2013-12-24
代码没有格式实在看起来很乱啊,不过有个建议:不需要把jpg图片存入数据库吧?一般都是把图片的名称作为字符串存入数据库然后把图片统一放在一个地方,这样存储效率更高啊
第2个回答  2013-12-24
建议把图片存放到文件夹中,然后在数据库中记录路径

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网