2011-09-19 33 views
0

我需要插入一行Blob列,這是從本地硬盤中的URL獲取的圖像。我的嘗試是:使用Java在數據庫中插入圖像

File file = new File(imageURL); 
FileInputStream input; 
byte[] data = null; 
try { 
    input = new FileInputStream(file); 
    data = new byte[input.available()]; 
    input.close(); 
} catch (Exception e) { 
    e.printStackTrace(); 
} 

然後,我建我的圖片對象:imagen畫質IMA =新的imagen畫質(0,數據,新的Date());我將它發送給另一位經理。我得到轉換的字節[]爲BLOB對象是這樣的:

byte[] datos = image.getDatos(); 
    Blob blob = null; 
    try { 
     blob = conection.createBlob(); 
     blob.setBytes(1,datos); 
    } catch (SQLException e) {   
     e.printStackTrace(); 
    } 

    java.sql.Date sqlDate = new java.sql.Date(image.getFecha().getTime()); 

    String query = " INSERT INTO imagen VALUES('" + image.getId() + "','" 
      + blob + "','" + sqlDate + "') "; 

    int lastID = BBDD.add(query, conection); 

我可以運行沒有問題,但我只得到這樣的「[email protected]」和我的MySQL doesn' t顯示我的其他東西。

任何人都可以幫助我嗎?我使用SQLyog來查看它。

謝謝!

+0

你還有另外一個問題:'InputStream.available()'確實沒有**告訴你輸入流包含多少個字節。而且你甚至沒有從代碼中的輸入流中讀取數據(這樣做你必須調用'read()'方法) –

回答

1

SQL工具只知道文件中使用的類型。 SQL工具無法理解blob或類似的二進制字段,只是不加修改地通過。您需要將這個二進制數據讀入另一個能夠理解數據含義的工具。

+0

實際上,我有一個名爲Image的Java Bean,帶有一個id,數據和日期。我以上述方式構建對象,然後通過此對象獲取信息以將其插入到我的BBDD中。所以你的意思是說,在進行查詢之前,我應該使用其他一些API來保存我的圖像,不是嗎? – Sierra

+0

不,我的意思是你需要將查詢中的數據傳遞給另一個API – Mark

2

好了,你可以用事先準備好的聲明:

java.sql.PreparedStatement ps = 
         connection.prepareStatement("UPDATE table SET file = ?"); 
ps.setBinaryStream(1, new FileInputStream(file), (int)file.length()); 

UPDATE

Blob的默認toString()將被調用這就是爲什麼你看到這樣奇怪的輸出,唯一的辦法是使用PreparedStatements,請糾正我,如果我擰任何人。

public void insert() throws Exception { 
    Connection conn = null; 
    PreparedStatement ps = null; 

    InputStream is = null; 
    try { 
     conn = this.connection.getConnection(); 
     String sql = "INSERT INTO Table (image) VALUES (?)"; 

     ps = conn.prepareStatement(sql); 
     if (this.file != null && this.file.canRead()) { 
      is = new BufferedInputStream(new FileInputStream(this.file)); 
      ps.setBinaryStream(1, is, (int) this.file.length()); 
     } else { 
      ps.setNull(1, Types.BLOB); 
     } 
    } catch (Exception e) { 
     LOG.error("", e); 
     throw e; 
    } finally { 
     FileUtil.close(is); 
     DAOUtil.close(conn); 
    } 
} 

而且你有一個連接對象,你可以跳過BBDD這個時候我的建議。

+0

你能告訴我們你用來插入數據庫的代碼嗎? –

+0

因爲我做查詢的方式,我不能使用prepareresstment。有沒有什麼辦法只用Statement ?.非常感謝你!! – Sierra

+0

說真的,爲了未來的用戶,這個答案會有一點點。如果你不這樣做,它很可能會被壓倒投票或被標記。 – cwallenpoole

0
package com.technicalkeeda; 

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

public class InsertImageTest { 

    /** 
    * This is used to get the Connection 
    * 
    * @return 
    */ 
    public Connection getConnection() { 
     Connection connection = null; 

     try { 
      Class.forName("com.mysql.jdbc.Driver"); 
      connection = DriverManager.getConnection(
        "jdbc:mysql://localhost:3306/technicalkeeda", "root", ""); 
     } catch (Exception e) { 
      System.out.println("Error Occured While Getting the Connection: - " 
        + e); 
     } 
     return connection; 
    } 

    /** 
    * Insert Image 
    */ 
    public void insertImage() { 
     Connection connection = null; 
     PreparedStatement statement = null; 
     FileInputStream inputStream = null; 

     try { 
      File image = new File("C:/honda.jpg"); 
      inputStream = new FileInputStream(image); 
      connection = getConnection(); 
      statement = connection 
        .prepareStatement("insert into trn_imgs(img_title, img_data) " 
          + "values(?,?)"); 
      statement.setString(1, "Honda Car"); 
      statement.setBinaryStream(2, (InputStream) inputStream, 
        (int) (image.length())); 

      statement.executeUpdate(); 
     } catch (FileNotFoundException e) { 
      System.out.println("FileNotFoundException: - " + e); 
     } catch (SQLException e) { 
      System.out.println("SQLException: - " + e); 
     } finally { 
      try { 
       connection.close(); 
       statement.close(); 
      } catch (SQLException e) { 
       System.out.println("SQLException Finally: - " + e); 
      } 
     } 

    } 

    /*** 
    * Execute Program 
    * 
    * @param args 
    * @throws SQLException 
    */ 
    public static void main(String[] args) throws SQLException { 
     InsertImageTest imageTest = new InsertImageTest(); 
     imageTest.insertImage(); 
    } 

}