⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 filedaoimpl.java

📁 一个bbs论坛系统
💻 JAVA
字号:
package com.lovo.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.lovo.po.FilePO;
import com.lovo.po.PublishPO;
import com.lovo.util.DBUtil;

public class FileDAOImpl implements FileDAO {

	private Connection con = null;

	private PreparedStatement pre = null;

	private ResultSet rs = null;

	
	public void delete(String name) throws SQLException {
		String sql = "delete from file where name = ? ";
		con = DBUtil.getDBUtil().getConnection();
		
		try {
			pre = con.prepareCall(sql);
			pre.setString(1, name);
			pre.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		}  finally {
			DBUtil.getDBUtil().close(rs);
			DBUtil.getDBUtil().close(pre);
			DBUtil.getDBUtil().close(con);
		}
	}

	public void insert(FilePO file) throws SQLException {
		String sql = "insert into file(url, name, publishid) values(?, ?, ?)";
		con = DBUtil.getDBUtil().getConnection();
		
		try {
			pre = con.prepareStatement(sql);
			pre.setString(1, file.getUrl());
			pre.setString(2, file.getName());
			pre.setInt(3, file.getPublish().getId());
			pre.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		}  finally {
			DBUtil.getDBUtil().close(rs);
			DBUtil.getDBUtil().close(pre);
			DBUtil.getDBUtil().close(con);
		}
	}

	public List<FilePO> queryFileByPublishId(int publishId) throws SQLException {
		List<FilePO> fileList = new ArrayList<FilePO>();
		String sql = "select * from file where publishid = ?";
		con = DBUtil.getDBUtil().getConnection();
		
		try {
			pre = con.prepareStatement(sql);
			pre.setInt(1, publishId);
			rs = pre.executeQuery();
			while(rs.next()) {
				FilePO file = new FilePO();
				file.setId(rs.getInt("id"));
				file.setName(rs.getString("name"));
				file.setUrl(rs.getString("url"));
				PublishPO publish = new PublishPO();
				publish.setId(rs.getInt("publishid"));
				file.setPublish(publish);
				fileList.add(file);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		}  finally {
			DBUtil.getDBUtil().close(rs);
			DBUtil.getDBUtil().close(pre);
			DBUtil.getDBUtil().close(con);
		}
		return fileList;
		
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -