2015-03-13 85 views
0

這裏是我的代碼如何將圖片上傳到數據庫

Connection con = null; 
    PreparedStatement ps = null; 
    InputStream is = null; 
    try { 
     Class.forName("oracle.jdbc.driver.OracleDriver"); 
     con = DriverManager. 
       getConnection("jdbc:oracle:thin:@<hostname>:<port num>:<DB name>" 
        ,"user","password"); 
     ps = con.prepareCall("insert into student_profile values (?,?)"); 
     ps.setInt(1, 101); 
     is = new FileInputStream(new File("Student_img.jpg")); 
     ps.setBinaryStream(2, is); 
     int count = ps.executeUpdate(); 
     System.out.println("Count: "+count); 
    } catch (ClassNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (SQLException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (FileNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } finally{ 
     try{ 
      if(is != null) is.close(); 
      if(ps != null) ps.close(); 
      if(con != null) con.close(); 
     } catch(Exception ex){} 
    } 
} 

}

我不知道在哪裏我錯了,但在執行代碼 異常的錯誤,我得到

  • 線程「main」中的異常java.lang.AbstractMethodError:方法oracle/jdbc/driver/OraclePreparedStatementWrapper.setBlob(ILjava/io/InputStream;)V在oracle.jdbc.driver.OraclePreparedStatementWrapp er.setBlob(OraclePreparedStatementWrapper.java)
+2

可能被看到無關http://stackoverflow.com/questions/1194990/why-do-i -get-java-lang-abstractmethoderror-when-trying-load-a-blob-in-the-db – Moob 2015-03-13 11:47:09

+0

你的錯誤信息不適合你的代碼。你不在代碼中調用'setBlob()'。你所顯示的代碼應該工作得很好,除了你應該使用'prepareStatement()'而不是'prepareCall()' - 這是調用存儲過程的事實。 (順便說一下,'oracle.jdbc.driver.OracleDriver'已被棄用,請改用'oracle.jdbc.OracleDriver')。 – 2015-03-13 11:58:56

+0

@a_horse_with_no_name它仍然顯示相同的錯誤消息,即使您所建議的更改 – 2015-03-13 12:01:35

回答

0

請不要低估,我沒有50的聲望添加評論。只需將我的答案指向我的問題,我將最終刪除它。

我不知道這是否是正確的答案,但在我的情況下,我不得不切換到oracle驅動程序版本6.以前的驅動程序有問題與斑點。在maven中:

<dependency> 
    <groupId>com.oracle</groupId> 
    <artifactId>oracle-jdbc</artifactId> 
    <version>6</version> 
</dependency> 
0

使用get getBlob()方法使用ResultSet從數據庫中獲取圖像。

ResultSet rs = null; 
    PreparedStatement pstmt = null; 
    String query = "SELECT photo FROM MyPictures WHERE id = ?"; 
    try { 
     pstmt = conn.prepareStatement(query); 
     pstmt.setInt(1, id); 
     rs = pstmt.executeQuery(); 
     rs.next(); 
     Blob blob = rs.getBlob("photo"); 
     // materialize BLOB onto client 
     return blob.getBytes(1, (int) blob.length()); 
    } finally { 
     rs.close(); 
     pstmt.close(); 
     conn.close(); 
    } 

Code Source

Insert BLOG(Picture or Photo) Data Type Into Oracle Database

+0

_That是好嗎_但是如何將圖像插入到數據庫中 – 2015-03-13 11:54:41

+0

我在我的答案中添加了一個鏈接,它將幫助您將BLOG圖像上傳到數據庫 – 2015-03-13 12:06:35

0

使用的setBinaryStream()方法

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

public class InsertPictureToMySql { 
public static void main(String[] args) throws Exception, IOException, SQLException { 
Class.forName("org.gjt.mm.mysql.Driver"); 
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/databaseName", "root", "root"); 
String INSERT_PICTURE = "insert into MyPictures(id, name, photo) values (?, ?, ?)"; 

FileInputStream fis = null; 
PreparedStatement ps = null; 
try { 
    conn.setAutoCommit(false); 
    File file = new File("myPhoto.png"); 
    fis = new FileInputStream(file); 
    ps = conn.prepareStatement(INSERT_PICTURE); 
    ps.setString(1, "001"); 
    ps.setString(2, "name"); 
    ps.setBinaryStream(3, fis, (int) file.length()); 
    ps.executeUpdate(); 
    conn.commit(); 
} finally { 
    ps.close(); 
    fis.close(); 
} 
} 
}