2015-10-04 25 views
3

我目前正在嘗試自己學習一些JDBC以及如何將對象保存到數據庫。現在我試圖將文檔上傳到數據庫。 我收到以下錯誤:有關Blob和Clob的Java JDBC,需要將doc文件上傳到MySQL數據庫

Exception in thread "main" java.lang.AbstractMethodError: Method com/mysql/jdbc/PreparedStatement.setClob(ILjava/io/Reader;)V is abstract 
    at com.mysql.jdbc.PreparedStatement.setClob(PreparedStatement.java) 
    at dao.StudentDAO.uploadResume(StudentDAO.java:156) 
    at controller.Test.main(Test.java:30) 

不知道爲什麼發生這種情況,有人可以幫我看看這個錯誤嗎? 下面是我的一些代碼:

// this is in my studentDAO class: 

private static final String SQL_UPDATE_RESUME = 
     "UPDATE students 
     SET resume = ? 
     WHERE socialSecNumber = ?"; 

public boolean uploadResume(Reader r) { 
     PreparedStatement pst; 
     //Reader file; 

     try{ 
      pst = con.getConnection().prepareStatement(SQL_UPDATE_RESUME); 
      //file = r; 
      pst.setClob(1, r); 
     } 
     catch(SQLException e){ 
      System.out.println("Error when uploading the resume: " + e); 
     } 
     finally{ 
      con.closeConnection(); 
     } 
     return true; 
    } 

public class Test { 
public static void main(String[] args) { 


    File file = new File("C:/Users/Carlos L/Desktop/Resume.docx"); 
    Reader r = null; 
    try { 
     r = new FileReader(file); 
    } catch (FileNotFoundException e) { 
     System.out.println("Error when locating the file: "+ e); 
    } 
    sdao.uploadResume(r); 
} 

}

+0

很可能您的JDBC驅動程序不支持CLOB。查看[將大型對象類型對象添加到數據庫](https://docs.oracle.com/javase/tutorial/jdbc/basics/blob.html),它創建了一個「Clob」對象而不是「Reader」 – MadProgrammer

+0

是的,我替換了司機,它的工作。該文件現在上傳到數據庫,但它不清晰,我正在通過php我的管理員,我正在使用XAMPP看它。 –

回答

2

PreparedStatement.setClob(int parameterIndex, Reader reader)加入了Java 6中,並且正在使用JDBC驅動程序從之前。

升級到Java 6兼容的驅動程序,您的代碼將起作用。

+0

是的你是對的,我下載了最新的驅動程序,它的工作。現在,當我上傳到數據庫(我正在使用XAMPP與PHP我的管理員)我上傳文檔的列的內容是不可讀的,它都充滿了無意義的數據。 –

+0

這聽起來像是一個不同的問題。 ;-) – Andreas

+0

大聲笑我不認爲我今天可以問更多的問題。 –

相關問題