免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1734 | 回复: 4
打印 上一主题 下一主题

BLOB求助 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-04-16 10:54 |只看该作者 |倒序浏览
RT,直接上源代码:

package compress;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;


public class TestBlob {
   
    String Url = "jdbc:mysql://localhost:3306/test?user=root&password=123456&useUnicode=true&characterEncoding=gbk";
    String driverName = "org.gjt.mm.mysql.Driver";
    Connection conn;
    File file;
   
    public Connection getConnection() {
        Connection connection = null;
        try {
            Class.forName(driverName).newInstance();
            connection = DriverManager.getConnection(Url);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        
        return connection;
    }
   
    public boolean putImage(){
        conn = getConnection();
        file = new File("c:\\1.gif");
        String sql = "insert into TestImage(id,image) values(?,?)";
        PreparedStatement psmtStatement = null;
        FileInputStream inputStream = null;
        try {
            inputStream = new FileInputStream(file);
            psmtStatement = conn.prepareStatement(sql);
            psmtStatement.setInt(1, 1);
            psmtStatement.setBinaryStream(2, inputStream, (int)file.length());
            
            psmtStatement.executeUpdate();
            
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return false;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } finally{
            if(psmtStatement!=null){
                try {
                    psmtStatement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                psmtStatement = null;
            }
            
        }
        return true;
    }
   
    public static void main(String[] args) {
        TestBlob tb = new TestBlob();
        System.out.print(tb.putImage());
    }

}




报异常:
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 '?????í??*?·?…s?¨§??!3è??é???z?@?“ó#?? ??M\0°???????=????
??7??CA?I??$XE' 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 compress.TestBlob.putImage(TestBlob.java:49)
at compress.TestBlob.main(TestBlob.java:62)
false
请问怎么办啊?

论坛徽章:
1
白银圣斗士
日期:2015-11-23 08:33:04
2 [报告]
发表于 2009-04-16 11:08 |只看该作者
开发工具里调试一下。
看看sqL是什么,再发上来。

论坛徽章:
0
3 [报告]
发表于 2009-04-16 18:16 |只看该作者
原帖由 枫影谁用了 于 2009-4-16 11:08 发表
开发工具里调试一下。
看看sqL是什么,再发上来。

MyEclipse 控制台信息: insert into testImage(id,image) values(1,** STREAM DATA **)

论坛徽章:
0
4 [报告]
发表于 2009-04-16 21:00 |只看该作者
建议先base64 encode之后再存进去,尽量不要直接存二进制格式

论坛徽章:
0
5 [报告]
发表于 2009-04-17 00:16 |只看该作者
原帖由 yejr 于 2009-4-16 21:00 发表
建议先base64 encode之后再存进去,尽量不要直接存二进制格式

我是初学者,请问具体怎么操作?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP