文件类中定义内容 类型为 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();
}
这样写会有一个报错:
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)
哪里有问题吗?