這裏是我的代碼如何將圖片上傳到數據庫
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)
可能被看到無關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
你的錯誤信息不適合你的代碼。你不在代碼中調用'setBlob()'。你所顯示的代碼應該工作得很好,除了你應該使用'prepareStatement()'而不是'prepareCall()' - 這是調用存儲過程的事實。 (順便說一下,'oracle.jdbc.driver.OracleDriver'已被棄用,請改用'oracle.jdbc.OracleDriver')。 – 2015-03-13 11:58:56
@a_horse_with_no_name它仍然顯示相同的錯誤消息,即使您所建議的更改 – 2015-03-13 12:01:35