📄 information.java
字号:
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,"\"", """);
DBOper o_DBOper = new DBOper();
ResultSet rs = null;
try
{
// 插入除了信息内容字段的其他字段,提交时间为系统当前时间,使用Oracle中sysdate函数
String sql = "Insert into Information(InfoId,CateId,Hits,Title,PostTime,Attpic,Poster) " ;
sql = sql + "Values("+InfoId+","+CateId+","+Hits+",'"+Title+"',";
sql = sql + "sysdate,"+Attpic+",'"+Poster+"')";
//System.out.println("INSERT SQL:"+sql);
o_DBOper.getResultSet(sql);
// 将信息内容插入Clob字段中
ClobTools ClobTools1 = new ClobTools();
ClobTools1.writeClob("Information", "Content", Content, "InfoId="+InfoId);
}
catch(Exception e)
{
throw new Exception(e.getMessage());
}
finally
{
try {o_DBOper.close();} catch (Exception e) { System.out.print(e.toString());}
}
}
// 更新信息
public void UpdateInformation()throws Exception
{
// 题目中字符串转换
Title = String_replace(Title,"'","''");
Title = String_replace(Title,"\"", """);
DBOper o_DBOper = new DBOper();
ResultSet rs = null;
// 更新除了内容字段外的其他字段
String sql = "update Information set PostTime=sysdate,";
sql = sql +"CateId="+CateId+",Title='"+Title+"',Attpic="+Attpic;
sql = sql + " where InfoId=" + InfoId;
//System.out.println("UPDATE SQL :"+sql);
try
{
o_DBOper.getResultSet(sql);
// 将信息内容插入Clob字段
ClobTools ClobTools1 = new ClobTools();
ClobTools1.writeClob("Information", "Content", Content, "InfoId="+InfoId);
}
catch(Exception e)
{
throw new Exception(e.getMessage());
}
finally
{
try {o_DBOper.close();} catch (Exception e) { System.out.print(e.toString());}
}
}
// 更新阅读次数,发生在有人阅读此信息时
public void UpdateHits(int Hits)throws Exception
{
DBOper o_DBOper = new DBOper();
ResultSet rs = null;
String sql = "Update Information Set Hits=" + Hits + " Where InfoId=" + InfoId;
try
{
o_DBOper.getResultSet(sql);
}
catch(Exception e)
{
throw new Exception(e.getMessage());
}
finally
{
try {o_DBOper.close();} catch (Exception e) { System.out.print(e.toString());}
}
}
// 取得当前表中最大的信息编号
public int getMaxId()throws Exception
{
DBOper o_DBOper = new DBOper();
ResultSet rs = null;
// 设定查询语句,Decode为Oracle自带函数,当Max(InfoId)为null时,返回0
String sql = "Select Decode(Max(InfoId),null,0,Max(InfoId)) as mks From Information";
try
{
rs = o_DBOper.getResultSet(sql);
if(rs.next())
return rs.getInt("mks");
}
catch(Exception e)
{
throw new Exception(e.getMessage());
}
finally
{
try {o_DBOper.close();} catch (Exception e) { System.out.print(e.toString());}
}
return 0;
}
// 按照指定条件查询信息
public ArrayList getSearchResult(String strSQL)throws Exception
{
//定义ArrayList()数组对象BdList
ArrayList BdList = new ArrayList();
//定义数据库对象
DBOper o_DBOper = new DBOper();
ResultSet rs = null;
//设置查询条件
String sql = "Select to_char(PostTime,'YYYY-MM-DD') As PostTime,InfoId,";
sql += "CateId, Hits, Attpic, Title, Content, Poster From Information" + strSQL;
try
{
//执行SQL语句
rs = o_DBOper.getResultSet(sql);
//使用循环语句,将结果集中的数据保存到数组中
while(rs.next()) {
String arrayReturn[] = new String[8];
arrayReturn[0]=String.valueOf(rs.getInt("InfoId"));
arrayReturn[1]=String.valueOf(rs.getInt("CateId"));
arrayReturn[2]=String.valueOf(rs.getInt("Hits"));
arrayReturn[3]=String.valueOf(rs.getInt("Attpic"));
arrayReturn[4]=so.ReplaceNull(rs.getString("Title"));
//使用ClobTools中的读Clob内容的方法读取信息内容
Content = ClobTools.readClob("Information","Content","InfoId=" + rs.getInt("InfoId"));
arrayReturn[5]=so.ReplaceNull(Content);
arrayReturn[6]=so.ReplaceNull(rs.getString("Poster"));
arrayReturn[7]=so.ReplaceNull(rs.getString("PostTime"));
//将数组添加到BdList对象中
BdList.add(arrayReturn);
}
}
catch(Exception e)
{
throw new Exception(e.getMessage());
}
finally
{
try {o_DBOper.close();} catch (Exception e) { System.out.print(e.toString());}
}
//返回BdList对象
return BdList;
}
// 替换字符串中的子字符串
public static String String_replace(String line, String oldString, String newString)
{
//如果line等于空,则返回
if (line == null) {
return null;
}
int i = 0;
//如果oldString在line中存在(位置大于0)
if ( (i = line.indexOf(oldString, i)) >= 0) {
//将line和newString字符拆分到数组line2和newString2中
char[] line2 = line.toCharArray();
char[] newString2 = newString.toCharArray();
//计算oldString的长度
int oLength = oldString.length();
//分配新字符串的存储空间
StringBuffer buf = new StringBuffer(line2.length);
// 将line2中oldString前字符串添加到buf中,然后再将newString2添加到buf中
buf.append(line2, 0, i).append(newString2);
//将指针移动到已经替换的oldString
i += oLength;
//使用上面的替换方法,用循环语句依次替换line2中的oldString字符串
int j = i;
while ( (i = line.indexOf(oldString, i)) > 0) {
buf.append(line2, j, i - j).append(newString2);
i += oLength;
j = i;
}
//将line2中余下的字符串添加到buf对象中
buf.append(line2, j, line2.length - j);
//将buf转换成String对象返回
return buf.toString();
}
//如果line中不包含oldString,则直接返回line
return line;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -