BLOB 二进制数据 最大长度4G
ORACLE的话

解决方案 »

  1.   

    前一阵子有回答某同学的帖子,参考一下一个application的完整例子,楼主针对你的环境修改下。环境:
    oracle 9i 
    jdk 1.6.xx
    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;import javax.swing.JFileChooser;/*-- 测试表 
    create table TestImage(
           img_id int primary key,
           img_content blob
    )*/
    public class BlobTestForOracle {
    private static final String DB_DRIVER = "oracle.jdbc.driver.OracleDriver";
    private static final String DB_URL = "jdbc:oracle:thin:@localhost:1521:oratest";
    private static final String DB_USER = "test";
    private static final String DB_PASSWORD = "password";

    public BlobTestForOracle(){
    try {
    Class.forName(DB_DRIVER);
    } catch (ClassNotFoundException e) {
    e.printStackTrace();
    }
    }
    /**
     * 另村为...
     * 
     * */
    public String chooseFileSave(String title,String fileName){
    JFileChooser chooser = new JFileChooser();
    chooser.setSelectedFile(new File(fileName));
    chooser.setDialogTitle(title);
    int rnt = chooser.showSaveDialog(null);
    if(rnt == JFileChooser.APPROVE_OPTION){
    return chooser.getSelectedFile().getAbsolutePath();
    }else{
    return "";
    }
    } /**
     * 打开...
     * 
     * */
    public String chooseFileOpen(String title,String fileName){
    JFileChooser chooser = new JFileChooser();
    chooser.setSelectedFile(new File(fileName));
    chooser.setDialogTitle(title);
    int rnt = chooser.showOpenDialog(null);
    if(rnt == JFileChooser.APPROVE_OPTION){
    return chooser.getSelectedFile().getAbsolutePath();
    }else{
    return "";
    }
    }

    /**
     * 写入blob
     * */
    public void writeFileToDatabaseByRid(String fileName,int rid){
    BufferedInputStream bis = null;
    Connection conn  = null;
    PreparedStatement pstmt = null;
    String strSQL = null;
    File file = null;
    int size = 0;
    try{
    strSQL = "INSERT INTO TestImage(img_id,img_content) VALUES(?,?)";
    conn = DriverManager.getConnection(DB_URL,DB_USER,DB_PASSWORD);
    pstmt = conn.prepareStatement(strSQL);
    fileName = this.chooseFileOpen("请选择一个文件",fileName);
    file = new File(fileName);
    if(! file.exists()){
    System.out.println(file.getAbsolutePath());
    System.out.println("文件貌似不存在!");
    return;
    }
    else if(file.length() > 50 * 1024 * 1024){ // 限制一下文件大小
    System.out.println("文件是不是太大鸟? " + file.length());
    return;
    }
    size = (int)file.length();
    bis = new BufferedInputStream(new FileInputStream(file));
    pstmt.setInt(1, rid);
    pstmt.setBinaryStream(2,bis,size); // 类似于逐一读取,bis流也被耗尽需要reset
    int effect = pstmt.executeUpdate();
    if(effect == 1){
    System.out.println("写入ok!" + file.getAbsolutePath());
    }
    else{
    System.out.println("写入failed!");
    }
    bis.close();
    }catch(IOException ioe){
    ioe.printStackTrace();
    }catch(SQLException sqle){
    sqle.printStackTrace();
    }finally{

    if(pstmt != null){
    try {
    pstmt.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }

    if(conn != null){
    try{
    conn.close();
    }catch(SQLException e){
    e.printStackTrace();
    }
    }
    }
    }

    /**
     * 从blob读取
     * */
    public void getFileFromDatabaseByRid(String fileName,int rid){
    String strSQL = null;
    Connection conn = null;
    PreparedStatement pstmt = null;
    ResultSet rst = null;
    int size = 0;
    File file = null;
    try{
    strSQL = "SELECT * FROM TestImage WHERE img_id = ?";
    conn = DriverManager.getConnection(DB_URL,DB_USER,DB_PASSWORD);
    pstmt = conn.prepareStatement(strSQL);
    pstmt.setInt(1, rid);
    rst = pstmt.executeQuery();
    if(rst.next()){
    fileName = this.chooseFileSave("存储为...", fileName);
    file = new File(fileName);
    if("".equals(file.getName().trim())){
    System.out.println("failed!");
    return;
    }
    OutputStream os = new BufferedOutputStream(new FileOutputStream(file));
    BufferedInputStream bis = new BufferedInputStream(rst.getBinaryStream("img_content"));

    byte[] b = new byte[10 * 1024]; // 这里是设置了缓存 byte[] b 设置为 10K
    int len = 0; // 返回read一次实际读取的字节数
    while((len = bis.read(b)) != -1){
    os.write(b,0,len); // 将b数组的内容写(按照len个字节输出)到输出流中,注意这里不要混淆了b.length
    size += len;
    }
    os.flush();
    os.close();
    bis.close();
    System.out.println("保存文件完毕! " + size + " Byte(s)");
    }
    else{
    System.out.println("id : " + rid + " 文件没有找到");
    }
    }catch(IOException ioe){
    ioe.printStackTrace();
    }
    catch(SQLException sqle){
    sqle.printStackTrace();
    }finally{
    if(rst != null){
    try {
    rst.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }

    if(pstmt != null){
    try {
    pstmt.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }

    if(conn != null){
    try{
    conn.close();
    }catch(SQLException e){
    e.printStackTrace();
    }
    }
    }
    }

    public static void main(String[] args){
    // test
    BlobTestForOracle test = new BlobTestForOracle();
    test.writeFileToDatabaseByRid("C:/a.jpg",123);
    test.getFileFromDatabaseByRid("C:/a.jpg", 123); }
    }
      

  2.   

    某大大的blog上也有讲述:
    http://my.so-net.net.tw/idealist/Java/JDBC/streamDataType.html
      

  3.   

    哦,对不起,我忘了说我的开发环境了,我用的是jdk1.6+JBuilder2005+SQL Server2000
      

  4.   

    SQL Server2000里没有数据类型blob,只有image