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

📄 contentdaoimpl.java

📁 本书由浅入深、循序渐进地介绍了MVC的体系结构和如何构建一个基于MVC的Web框架
💻 JAVA
字号:
package com.myContent.dao.impl;

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

import com.myContent.vo.Content;
import com.myContent.vo.ContentType;
import com.myContent.dao.ContentDAO;
import com.gd.jdbc.impl.GdDbCommon;

public class ContentDAOImpl extends GdDbCommon implements ContentDAO {
	/**
	 * 构造函数
	 * @param conn
	 */
	public ContentDAOImpl(Connection conn) {
		super(conn);
	}
	/**
	 * 根据Object实现新增
	 */
	public int createContent(Content content) throws SQLException{
		String sql = "insert into content (title, content, dateTime, userId, contentTypeId) values(?, ?, ?, ?, ?)";
		this.PreparedStatement(sql);
		this.setString(1, content.getTitle());
		this.setString(2, content.getContent());
		this.setString(3, content.getDateTime());
		this.setInt(4, content.getUserId());
		this.setInt(5, content.getContentTypeId());
		return this.executeUpdate();
	}
	/**
	 * 根据Object实现修改
	 */
	public int updateContent(Content content) throws SQLException {
		String sql = "update content set title = ?, content= ? , dateTime = ?, userId = ?, contentTypeId = ? where id = ? ";
		this.PreparedStatement(sql);
		this.setString(1, content.getTitle());
		this.setString(2, content.getContent());
		this.setString(3, content.getDateTime());
		this.setInt(4, content.getUserId());
		this.setInt(5, content.getContentTypeId());
		this.setInt(6, content.getId());
		return this.executeUpdate();
	}
	/**
	 * 根据Object实现删除
	 */
	public int deleteContent(Content content)  throws SQLException{
		String sql = "delete from content where id = ?";
		this.PreparedStatement(sql);
		this.setInt(1, content.getId());
		return this.executeUpdate();
	}
	/**
	 * 根据contentId查询Object
	 */
	public Content queryContent(int contentId) throws SQLException{
		String sql = "select * from  content where id = ?";
		this.PreparedStatement(sql);
		this.setInt(1, contentId);
		return (Content)this.queryObj();
	}
	/**
	 * 根据Object实现批量新增
	 */
	public int createListContent(List list) throws SQLException{
		String sql = "insert into content (title, content, dateTime, userId, contentTypeId) values(?, ?, ?, ?, ?)";
		this.PreparedStatement(sql);
		Content content = null;
		int counts = 0;
		for (int i = 0; list != null && list.size() > i; i++) {
			content = (Content)list.get(i);
			this.setString(1, content.getTitle());
			this.setString(2, content.getContent());
			this.setString(3, content.getDateTime());
			this.setInt(4, content.getUserId());
			this.setInt(5, content.getContentTypeId());
			counts += this.executeUpdate();
		}
		
		return counts;
	}
	
	/**
	 * 根据Object实现批量新增,使用batch方式
	 */
	public int[] createBatchListContent(List list) throws SQLException{
		String sql = "insert into content (title, content, dateTime, userId, contentTypeId) values(?, ?, ?, ?, ?)";
		this.PreparedStatement(sql);
		Content content = null;
		int counts = 0;
		for (int i = 0; list != null && list.size() > i; i++) {
			content = (Content)list.get(i);
			this.setString(1, content.getTitle());
			this.setString(2, content.getContent());
			this.setString(3, content.getDateTime());
			this.setInt(4, content.getUserId());
			this.setInt(5, content.getContentTypeId());
			this.addBatch();
		}
		return this.executeBatch();
	}
	
	/**该方法用来获取所有的内容
	*/
	public List queryAllContent() throws Exception {
		String sql = "select * from  content";
		this.PreparedStatement(sql);
		return this.queryAllObj();
	}
	/**该方法用来根据类别获取所有的内容
	*/
	public List queryAllContent(int typeId) throws Exception {
		String sql = "select * from  content where contentTypeId = " + typeId;
		this.PreparedStatement(sql);
		return this.queryAllObj();
	}
	
//	重载getObjFromRs方法
	protected Content getObjFromRs(ResultSet rs) throws SQLException  {
		Content content = new Content();
		content.setId(rs.getInt("id"));
		content.setTitle(rs.getString("title"));
		content.setContent(rs.getString("content"));
		content.setDateTime(rs.getString("dateTime"));
		content.setUserId(rs.getInt("userId"));
		content.setContentTypeId((rs.getInt("contentTypeId")));
		return content;
	}
}

⌨️ 快捷键说明

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