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

📄 contenteo.java

📁 征服 Web2.0 快速入门(Java) 光盘源码
💻 JAVA
字号:
package book.portal.table;

import java.sql.ResultSet;
import java.sql.SQLException;

import book.portal.DbManager;

public class ContentEO {
	protected int id;//代表数据库中content_id列
	protected int userId;//代表数据库中user_id列
	protected String contentType;//代表数据库中content_type列
	protected String contentMain;//代表数据库中content_main列
	protected String activeStatus = "Y";//代表数据库中active_status列
	public ContentEO() {//无参的构造方法 
		this.id = -1;
	}
	public ContentEO(int id) {//有参的构造方法,参数为content_id
		this.id = id;
		if (!FromDb())//如果有找到该id的content
			this.id = -1;
	}
	public boolean FromDb() {//从数据库中读出,并更新bean
		int row = -1;
        //读记录的sql语句
		String sql = "select * from ajax_content where content_id=" + this.id
				+ " and active_status='Y'";
		ResultSet rs = DbManager.getResultSet(sql);//执行sql语句并返回ResultSet
		try {
			rs.last();//移动到最后一行
			row = rs.getRow();//得到总记录数
			if (row == 1) {//如果只查询到一条记录,则代表该记录存在并更新该类的属性
				this.userId = rs.getInt("CONTENT_ID");
				this.contentType = rs.getString("CONTENT_TYPE");
				this.contentMain = rs.getString("CONTENT_MAIN");
				this.activeStatus = rs.getString("ACTIVE_STATUS");
				return true;
			} else
				return false;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		} finally {
			try {//最后关闭ResutltSet,Statement.并释放连接
				if (rs != null)
					rs.close();
				if (rs.getStatement() != null)
					rs.getStatement().close();
				DbManager.releaseConnection();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	public boolean ToDb() {//更新数据库,并重新设置bean
		if (getId() == -1)//如果此时id为-1
		{
			return false;
		} else {
            //更新该记录的sql语句
			String sql = "update ajax_content set user_id=" + getUserId()
					+ ",content_type='" + getContentType()
					+ "',content_main='" + getContentMain()+"',active_status='"
					+ getActiveStatus() + "' where content_id=" + getId();
			DbManager.excute(sql);//执行sql语句
			return FromDb();//重新读出bean的属性
		}
	}
	public String getActiveStatus() {
		return activeStatus;
	}
	public void setActiveStatus(String activeStatus) {
		this.activeStatus = activeStatus;
	}
	public String getContentMain() {
		return contentMain;
	}
	public void setContentMain(String contentMain) {
		this.contentMain = contentMain;
	}
	public String getContentType() {
		return contentType;
	}
	public void setContentType(String contentType) {
		this.contentType = contentType;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getUserId() {
		return userId;
	}
	public void setUserId(int userId) {
		this.userId = userId;
	}
}

⌨️ 快捷键说明

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