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

📄 areadaoimpl.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.AreaPO;
import com.lovo.util.DBUtil;

public class AreaDAOImpl implements AreaDAO {
	
	private Connection con = null;

	private PreparedStatement pre = null;

	private ResultSet rs = null;
	
	public void insert(AreaPO po) throws SQLException {
		String sql = "insert into area (imgid,name,descontent) values(?,?,?)";
		con = DBUtil.getDBUtil().getConnection();
		try {
			pre = con.prepareStatement(sql);
			pre.setInt(1, po.getImgId());
			pre.setString(2, po.getName());
			pre.setString(3, po.getDescribe());
			pre.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		}
		finally {
			DBUtil.getDBUtil().close(pre);
			DBUtil.getDBUtil().close(con);
		}
	}
	
	public void delete(int id) throws SQLException {
		String sql = "delete from area where id = ?";
		con = DBUtil.getDBUtil().getConnection();
		try {
			pre = con.prepareStatement(sql);
			pre.setInt(1, id);
			pre.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		}
		finally {
			DBUtil.getDBUtil().close(pre);
			DBUtil.getDBUtil().close(con);
		}
	}
	
	public void update(AreaPO po) throws SQLException {
		String sql = "update area set imgid = ?, name = ?, descontent = ? where id = ?";
		con = DBUtil.getDBUtil().getConnection();
		try {
			pre = con.prepareStatement(sql);
			pre.setInt(1, po.getImgId());
			pre.setString(2, po.getName());
			pre.setString(3, po.getDescribe());
			pre.setInt(4, po.getId());
			pre.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		}
		finally {
			DBUtil.getDBUtil().close(pre);
			DBUtil.getDBUtil().close(con);
		}
	}
	
	public AreaPO queryById(int id) throws SQLException {
		String sql = "select * from area where id = ?";
		AreaPO po = null;
		con = DBUtil.getDBUtil().getConnection();
		try {
			pre = con.prepareStatement(sql);
			pre.setInt(1, id);
			rs = pre.executeQuery();
			
			while(rs.next()) {
				po = new AreaPO();
				
				po.setId(rs.getInt("id"));
				po.setImgId(rs.getInt("imgid"));
				po.setDescribe(rs.getString("descontent"));
				po.setName(rs.getString("name"));
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		}
		finally {
			DBUtil.getDBUtil().close(rs);
			DBUtil.getDBUtil().close(pre);
			DBUtil.getDBUtil().close(con);
		}
		return po;
	}
	
	public List<AreaPO> queryArea() throws SQLException {
		String sql = "select * from area";
		con = DBUtil.getDBUtil().getConnection();
		List<AreaPO> list = new ArrayList<AreaPO>();
		try {
			pre = con.prepareStatement(sql);
			rs = pre.executeQuery();
			
			while(rs.next()) {
				AreaPO po = new AreaPO();
				
				po.setId(rs.getInt("id"));
				po.setImgId(rs.getInt("imgid"));
				po.setDescribe(rs.getString("descontent"));
				po.setName(rs.getString("name"));
				list.add(po);
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		}
		finally {
			DBUtil.getDBUtil().close(rs);
			DBUtil.getDBUtil().close(pre);
			DBUtil.getDBUtil().close(con);
		}
		return list;
	}
}

⌨️ 快捷键说明

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