免费注册 查看新帖 |

Chinaunix

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

字符串如何存到数据库blob字段? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2004-03-19 09:23 |只看该作者 |倒序浏览
我将字符串存放到blob字段出现乱码,请问如何解决?是否需要将字符串转换为二进制再存储,如何转换呢?

论坛徽章:
0
2 [报告]
发表于 2004-03-19 14:26 |只看该作者

字符串如何存到数据库blob字段?

String.getBytes() 可以吗?

我们原来是做成fileinputstream然后set进去。

论坛徽章:
0
3 [报告]
发表于 2004-03-19 15:45 |只看该作者

字符串如何存到数据库blob字段?

用String.getByte()写进去的也是乱码!

论坛徽章:
0
4 [报告]
发表于 2004-03-19 20:28 |只看该作者

字符串如何存到数据库blob字段?

终于解决了,将文本字符串转换为8859-1编码就可以!

论坛徽章:
1
数据库技术版块每日发帖之星
日期:2015-12-09 06:20:00
5 [报告]
发表于 2004-03-19 22:55 |只看该作者

字符串如何存到数据库blob字段?

怎样存取图形呢?

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
6 [报告]
发表于 2004-03-20 11:32 |只看该作者

字符串如何存到数据库blob字段?

这个是mysql下存取blob字段的一个很简单的类,跟据自己的需要改改就行了

/**
* Title:         BlobPros.java
* Project:       test
* Description:  把图片存入mysql中的blob字段,并取出
* Call Module:  mtools数据库中的tmp表  
* File:         C:\downloads\luozsh.jpg
* Copyright:    Copyright (c) 2003-2003
* Company:      uniware
* Create Date:  2002.12.5
* @Author:      FeiFan
* @version 1.0 版本*      
*
*  Revision history
*  Name         Date                   Description
*  ----             ----               -----------
*        Chenqh          2003.12.5        对图片进行存取
*
* note:         要把数据库中的Blob字段设为longblob        
*
*/

//package com.uniware;

import java.io.*;
import java.util.*;
import java.sql.*;

public class BlobPros
{
    private static final String URL = "jdbc:mysql://10.144.123.63:3306/mtools?user=wind&password=123&useUnicode=true";
    private Connection conn = null;
    private PreparedStatement pstmt = null;
    private ResultSet rs = null;
    private File file = null;
   
    public BlobPros()
    {
    }

    /**
    * 向数据库中插入一个新的BLOB对象(图片)
    *
    * @param infile - 要输入的数据文件
    * @throws java.lang.Exception
    *
    */
   public void blobInsert(String infile) throws Exception
   {
       FileInputStream fis = null;   
           try
           {
               Class.forName("org.gjt.mm.mysql.Driver".newInstance();
               conn = DriverManager.getConnection(URL);
            
               file = new File(infile);
               fis = new FileInputStream(file);
               //InputStream fis = new FileInputStream(infile);
                       pstmt = conn.prepareStatement("insert into tmp(descs,pic) values(?,?)";
                       pstmt.setString(1,file.getName());    //把传过来的第一个参数设为文件名
               //pstmt.setBinaryStream(2,fis,(int)file.length());   //这种方法原理上会丢数据,因为file.length()返回的是long型
                       pstmt.setBinaryStream(2,fis,fis.available());  //第二个参数为文件的内容
                       pstmt.executeUpdate();
                    }
           catch(Exception ex)
           {
          System.out.println("[blobInsert error : ]" + ex.toString());
           }
               finally
               {
               //关闭所打开的对像//
               pstmt.close();
               fis.close();
               conn.close();
           }
    }
   

    /**
    * 从数据库中读出BLOB对象
    *
    * @param outfile - 输出的数据文件
    * @param picID - 要取的图片在数据库中的ID
    * @throws java.lang.Exception
    *
    */

    public void blobRead(String outfile,int picID) throws Exception
    {
        FileOutputStream fos = null;
        InputStream is = null;
        byte[] Buffer = new byte[4096];

            try
            {
                Class.forName("org.gjt.mm.mysql.Driver".newInstance();
                conn = DriverManager.getConnection(URL);
                pstmt = conn.prepareStatement("select pic from tmp where id=?";
                pstmt.setInt(1,picID);         //传入要取的图片的ID
                rs = pstmt.executeQuery();
                rs.next();
                     
                file = new File(outfile);
                if(!file.exists())
                {
                    file.createNewFile();     //如果文件不存在,则创建
                }
                fos = new FileOutputStream(file);
                is = rs.getBinaryStream("pic";
                int size = 0;
               /* while(size != -1)
                {
                    size = is.read(Buffer);    //从数据库中一段一段的读出数据
                    //System.out.println(size);
                    if(size != -1)            //-1表示读到了文件末
                        fos.write(Buffer,0,size);
                }  */
                while((size = is.read(Buffer)) != -1)
                                {
                    //System.out.println(size);
                    fos.write(Buffer,0,size);
                                }
                              
                }
            catch(Exception e)
            {
                System.out.println("[OutPutFile error : ]" + e.getMessage());
            }
            finally
            {
                //关闭用到的资源
                fos.close();
                rs.close();
                pstmt.close();
                conn.close();
            }
    }
     
    public static void main(String[] args)
    {
        try
        {
            
            BlobPros blob = new BlobPros();
            //blob.blobInsert("C:\\Downloads\\luozsh1.jpg";           
            blob.blobRead("c:/downloads/luozishang.jpg",47);
        }
        catch(Exception e)
        {
            System.out.println("[Main func error: ]" + e.getMessage());
        }
    }
}

论坛徽章:
0
7 [报告]
发表于 2004-03-22 21:36 |只看该作者

字符串如何存到数据库blob字段?

存取图形用输入流

论坛徽章:
1
数据库技术版块每日发帖之星
日期:2015-12-09 06:20:00
8 [报告]
发表于 2004-03-22 21:54 |只看该作者

字符串如何存到数据库blob字段?

[quote]原帖由 "twzlhm"]存取图形用输入流[/quote 发表:

能不能再据个例子?谢谢

论坛徽章:
0
9 [报告]
发表于 2004-03-23 21:48 |只看该作者

字符串如何存到数据库blob字段?

//存取二进制文件到BLOB字段的函数
import oracle.sql.BLOB;
import oracle.jdbc.driver.OracleResultSet;

public void updateBlob(Connection conn,String strFile,long newId,String table_name) {
   try {
     conn.setAutoCommit(false);
     Statement stmt = conn.createStatement();
     //stmt.execute("insert into A(A,B) values ('"+strId+"',empty_blob())";
     ResultSet rset = stmt.executeQuery("select t.content from "+table_name +" t where id=" + newId +
                                        " FOR UPDATE";
     BLOB blob = null;
     while (rset.next()) {
       blob = ( (OracleResultSet) rset).getBLOB(1);
     }
     File binaryFile = new File(strFile);
     FileInputStream instream = new FileInputStream(binaryFile); //读入二进制文件
     OutputStream outstream = blob.getBinaryOutputStream();
     byte[] buffer = new byte[32];

     int length = -1;
     while ( (length = instream.read(buffer)) != -1)
     outstream.write(buffer, 0, length);
     instream.close();
     outstream.close();

     conn.commit();
     conn.setAutoCommit(true);
     stmt.close();
     conn.close();
   }
   catch(Exception e){
     e.printStackTrace();
   }
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP