📄 filedao.java
字号:
package com.oa.lp.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import com.oa.lp.model.File;
import com.oa.lp.model.Folder;
import com.oa.lp.model.Inform;
import com.oa.lp.util.DTOPopulator;
public class FileDAO {
private Connection conn;
public Connection getConn() {
return conn;
}
public void setConn(Connection conn) {
this.conn = conn;
}
/**
* 添加文件
* @param file
* @throws SQLException
*/
public void addFile(File file) throws SQLException{
String sql = "insert into [FILE](FOLDER_ID,FILE_NAME,FILE_PATH,UPLOAD_TIME,FILE_SIZE,FILE_STATE)" +
" values(?,?,?,getDate(),?,?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1,file.getFolderId());
pstmt.setString(2,file.getFileName());
pstmt.setString(3,file.getFilePath());
pstmt.setInt(4,file.getFileSize());
pstmt.setInt(5,file.getFileState());
pstmt.executeUpdate();
pstmt.close();
}
/**
* 删除文件通过ID
* @param fileId
* @throws SQLException
*/
public void delFile(int fileId) throws SQLException{
String sql = "delete from [FILE] where FILE_ID=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1,fileId);
pstmt.executeUpdate();
pstmt.close();
}
/**
* 通过文件夹ID 返回文件夹下所有文件集合
* @param folderId
* @return
* @throws Exception
*/
public List listAllFile(int folderId) throws Exception{
List list = null;
String sql = "";
if(folderId==0){
return list;
}else{
sql = "select * from [FILE] where FOLDER_ID="+folderId;
}
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
//将结果集中的每一行记录封装成一个对象,再放进集合返回
list = DTOPopulator.populate(rs, File.class);
rs.close();
pstmt.close();
return list;
}
/**
* 通过文件ID得到文件
* @throws Exception
*/
public File getFileById(int fileId) throws Exception{
File file = new File();
List list=null;
String sql = "select * from [FILE] where FILE_ID=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1,fileId);
ResultSet rs = pstmt.executeQuery();
list = DTOPopulator.populate(rs, File.class);
if(list.size()>0){
file = (File)list.get(0);
}
rs.close();
pstmt.close();
return file;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -