文件類(lèi)中定義內(nèi)容 類(lèi)型為 File可以嗎
public class Files {
?? ?private String file_id;
?? ?private String file_name;
?? ?private File file_details;
}
public class FileAction {
?? ?
?? ?public static void fileUpdata(Files srcFile) throws Exception{
?? ??? ?Connection conn =DBUtil.getDBConnection();
?? ??? ?if(!srcFile.getFile_details().exists()){
?? ??? ??? ?throw new IllegalArgumentException("文件:"+srcFile.getFile_details()+"不存在");
?? ??? ?}
?? ??? ?if(!srcFile.getFile_details().isFile()){
?? ??? ??? ?throw new IllegalArgumentException(srcFile.getFile_details()+"不是文件");
?? ??? ?}
?? ??? ?FileInputStream is = new FileInputStream(srcFile.getFile_details());
?? ??? ??? ?String sql="" +"insert into files (file_name,file_details)? values(?,?) "+" ";
?? ??? ??? ?PreparedStatement ptmt=conn.prepareStatement(sql);
?? ??? ??? ?ptmt.setString(1, srcFile.getFile_name());
?? ??? ??? ??? ?
?? ??? ??? ?ptmt.setBlob(2, is);;
?? ??? ??? ?ptmt.execute();
?? ??? ??? ?is.close();
?? ?}
}
public static void main(String[] args) {
?? ??? ?
?? ??? ?UserAtion ation = new? UserAtion();
?? ??? ?FileAction? fileAtion = new FileAction();
?? ??? ?Users uu = new Users();
?? ??? ?Files ff= new Files();
?? ??? ?
?? ??? ?ff.setFile_name("ying");
?? ??? ?ff.setFile_details(new File("E:\\imooc\\test.doc"));
?? ??? ?
?? ??? ?try {
?? ??? ??? ?fileAtion.fileUpdata(ff);
?? ??? ?} catch (Exception e) {
?? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ?e.printStackTrace();
?? ??? ?}
這樣寫(xiě)會(huì)有一個(gè)報(bào)錯(cuò):
Exception in thread "main" java.lang.AbstractMethodError: Method com/mysql/jdbc/PreparedStatement.setBlob(ILjava/io/InputStream;)V is abstract
?? ?at com.mysql.jdbc.PreparedStatement.setBlob(PreparedStatement.java)
?? ?at imooc.combat.action.FileAction.fileUpdata(FileAction.java:29)
?? ?at Test.Test.main(Test.java:27)
哪里有問(wèn)題嗎?
2017-02-27
當(dāng)數(shù)據(jù)庫(kù)字段為blob類(lèi)型時(shí),必須使用PreparedStatement中的setBinaryStream(int,InputStream,int)方法;當(dāng)數(shù)據(jù)庫(kù)字段為longblob類(lèi)型時(shí),必須使用PreparedStatement中的setBinaryStream(int,InputStream,long)方法。