package Information;
import java.sql.*;
import java.util.*;
import java.util.Date;
import java.text.*;
import comm.*;
import java.io.*;
public class Information {
private int InfoId; // 信息编号
private int CateId; // 信息分类编号
private String Title; // 信息题目
private String Content; // 信息内容
private String PostTime; // 提交时间
private String Poster; // 信息提交人
private int Hits; // 阅读次数
private int Attpic; // 是否有图片,0 - 无图片;1 - 有图片
// 定义字符串处理对象
StringOper so = new StringOper();
// 设置、取得属性值
public void setInfoId(int InfoId){
this.InfoId = InfoId;
}
public int getInfoId(){
return this.InfoId;
}
public void setCateId(int CateId){
this.CateId = CateId;
}
public int getCateId(){
return this.CateId;
}
// 设置题目,替换题目中的单引号为'',和斜杠为"
public void setTitle(String sTitle){
sTitle = String_replace(sTitle,"'","''");
sTitle = String_replace(sTitle,"\"", """);
this.Title = sTitle;
}
// 获取题目,将''还原为单引号
public String getTitle(){
Title = String_replace(Title,"''","'");
return this.Title;
}
public void setContent(String Content){
this.Content = Content;
}
public String getContent(){
return this.Content;
}
public void setPostTime(String PostTime){
this.PostTime = PostTime;
}
public String getPostTime(){
return this.PostTime;
}
public void setPoster(String poster){
this.Poster = poster;
}
public String getPoster(){
return this.Poster;
}
public void setHits(int Hits){
this.Hits = Hits;
}
public int getHits(){
return this.Hits;
}
public void setAttpic(int attpic){
this.Attpic = attpic;
}
public int getAttpic(){
return this.Attpic;
}
// 判断是否存在某个信息,如果存在则返回true,并返回其详细信息
public boolean getInformation()throws Exception
{
DBOper o_DBOper = new DBOper();
ResultSet rs = null;
// 设置查询语句,其中将提交时间PostTime转为指定输出格式
String sql = "Select to_char(PostTime,'YYYY-MM-DD HH:mm:ss') as PostTime,InfoId,";
sql += "CateId,Hits,Attpic,Title,Content,Poster From Information where InfoId="+InfoId;
try
{
// 执行查询操作
rs = o_DBOper.getResultSet(sql);
// 如果存在此信息,则返回其内容
if(rs.next())
{
setInfoId(rs.getInt("InfoId"));
setCateId(rs.getInt("CateId"));
setHits(rs.getInt("Hits"));
setAttpic(rs.getInt("Attpic"));
setTitle(so.ReplaceNull(rs.getString("Title")));
// 使用ClobTools中的读Clob内容的方法读取信息内容,详细信息查看ClobTools类
Content = ClobTools.readClob("Information","Content","InfoId=" +InfoId);
setContent(Content);
setPoster(so.ReplaceNull(rs.getString("Poster")));
setPostTime(so.ReplaceNull(rs.getString("PostTime")));
return true;
}
}
catch(Exception e)
{
throw new Exception(e.getMessage());
}
finally
{
try {o_DBOper.close();} catch (Exception e) { System.out.print(e.toString());}
}
return false;
}
// 得到包含多个或者一个信息的数组对象
public Vector getMoreInformation()throws Exception
{
// 定义变量数组
Vector v_Information = new Vector();
int conditionNo = 0;
// 连接数据库
DBOper o_DBOper = new DBOper();
ResultSet rs = null;
// 设置查询语句
String sql = "Select to_char(PostTime,'YYYY-MM-DD HH:mm:ss') as PostTime,InfoId,";
sql += "CateId,Hits,Attpic,Title,Content,Poster From Information ";
String condition = " where ";
try
{
// 如果信息编号不为0,则添加查询条件
if(InfoId != 0)
{
condition += "InfoId="+InfoId;
conditionNo++;
}
// 如果信息类别编号不为0,则添加查询条件
if(CateId != 0)
{
if(conditionNo > 0)
{
condition += " and";
}
condition += " CateId="+CateId;
conditionNo++;
}
if(Title != null)
{
if(conditionNo > 0)
{
condition += " and";
}
condition += " Title='"+Title+"'";
conditionNo++;
}
if(Hits != 0)
{
if(conditionNo > 0)
{
condition += " and";
}
condition += " Hits="+Hits;
conditionNo++;
}
if(Poster != null)
{
if(conditionNo > 0)
{
condition += " and";
}
condition += " Poster='"+Poster+"'";
conditionNo++;
}
if(conditionNo > 0)
{
sql += condition;
}
sql += " order by PostTime desc,CateId desc";
rs = o_DBOper.getResultSet(sql);
// System.out.println(sql);
while(rs.next())
{
Information o_Information2 = new Information();
o_Information2.setInfoId(rs.getInt("InfoId"));
o_Information2.setCateId(rs.getInt("CateId"));
o_Information2.setHits(rs.getInt("Hits"));
o_Information2.setAttpic(rs.getInt("Attpic"));
o_Information2.setTitle(so.ReplaceNull(rs.getString("Title")));
// 使用ClobTools中的读Clob内容的方法读取信息内容,详细信息查看ClobTools类
Content = ClobTools.readClob("Information","Content","InfoId="+InfoId);
o_Information2.setContent(so.ReplaceNull(Content));
o_Information2.setPoster(so.ReplaceNull(rs.getString("Poster")));
o_Information2.setPostTime(so.ReplaceNull(rs.getString("PostTime")));
v_Information.add(o_Information2);
}
}
catch(Exception e)
{
throw new Exception(e.getMessage());
}
finally
{
try {o_DBOper.close();} catch (Exception e) { System.out.print(e.toString());}
}
return v_Information;
}
// 管理员可以删除多条信息的方法,参数为信息编号字符串
public void DeleteInformation(String InfoId)throws Exception
{
DBOper o_DBOper = new DBOper();
ResultSet rs = null;
String sql_dlt = "Delete From Information where InfoId in(" + InfoId+")";
try
{
o_DBOper.getResultSet(sql_dlt);
}
catch(Exception e)
{
throw new Exception(e.getMessage());
}
finally
{
try {o_DBOper.close();} catch (Exception e) { System.out.print(e.toString());}
}
}
// 编辑者删除信息,只能删除自己添加的信息,需要提供编辑者的用户名sUserId
public void DeleteInformation(String InfoId,String sUserId)throws Exception
{
DBOper o_DBOper = new DBOper();
ResultSet rs = null;
String sql_dlt = "Delete From Information where InfoId in(" + InfoId+") and Poster='" + sUserId + "'";
try
{
o_DBOper.getResultSet(sql_dlt);
}
catch(Exception e)
{
throw new Exception(e.getMessage());
}
finally
{
try {o_DBOper.close();} catch (Exception e) { System.out.print(e.toString());}
}
}
// 插入信息的详细内容
public void CreateInformation()throws Exception
{
// 转换信息题目中的单引号为'',"\""为"""
Title = String_replace(Title,"'","''");
Title = String_replace(Title,"\"", """);