📄 news.java
字号:
/**
* 最新信息模块java bean
* News.java
* @author usr
*/
package building;
import java.util.Vector;
import java.sql.*;
import javax.sql.*;
public class News
{
//基本属性
private int id = 0; //ID
private int adderId = 0; //添加者ID
private String title = null; //标题
private String contents = null; //内容
private String addTime = null; //添加时间
//构造方法
public News(){}
/**
* 基本属性的set及get方法
*/
//ID属性的set及get方法
public void setId(int id)
{
this.id = id;
}//End of setId
public int getId()
{
return(id);
}//End of getId
//添加者属性的set及get方法
public void setAdderId(int adderId)
{
this.adderId = adderId;
}//End of setAdderId
public int getAdderId()
{
return(adderId);
}//End of getAdderId
//标题属性的set及get方法
public void setTitle(String title)
{
this.title = title;
}//End of setTitle
public String getTitle()
{
return(title);
}//End of getTitle
//内容属性的set及get方法
public void setContents(String contents)
{
this.contents = contents;
}//End of setContents
public String getContents()
{
return(contents);
}//End of getContents
//添加时间属性的set及get方法
public void setAddTime(String addTime)
{
this.addTime = addTime;
}//End of aetAddTime
public String getAddTime()
{
return(addTime);
}//End of getAddTime
/**
* 业务逻辑操作
*/
/**
* 读出最新的信息(5条)
*/
public static Vector getLatestNews(DB db)
throws SQLException
{
String strSql = null;
ResultSet rs = null;
Vector newsList = new Vector();
strSql = "select * from News order by id desc";
rs = db.OpenSql(strSql);
if (rs.next())
{
//查询到结果
rs.beforeFirst();
while (rs.next())
{
News news = new News();
news.setId(rs.getInt("id"));
news.setAdderId(rs.getInt("adderid"));
news.setTitle(rs.getString("title"));
news.setContents(rs.getString("contents"));
news.setAddTime(rs.getString("addtime"));
newsList.add(news);
}//End of while
return(newsList);
}//End of if
else
{
//无查询结果
return(null);
}//End of else
}//End of getLatestNews
/**
*查找指定编号的消息的具体信息
*/
public static News getNews(DB db,int id)
throws Exception
{
String strSql = null;
ResultSet rs = null;
String strContents = null;
strSql = "select * from News where id=" + id;
rs = db.OpenSql(strSql);
if (rs.next())
{
//得到信息
News news = new News();
news.setId(rs.getInt("id"));
news.setAdderId(rs.getInt("adderid"));
news.setTitle(rs.getString("title"));
strContents = rs.getString("contents");
news.setContents(strContents.replaceAll("\n","<br>"));
news.setAddTime(rs.getString("addtime"));
return(news);
}//End of if
else
{
//没有得到信息
return(null);
}//End of else
}//End of getNews
/**
* 查询所有的News信息
*/
public static Vector getAllNews(DB db)
throws SQLException
{
String strSql = null;
ResultSet rs = null;
Vector newsList = new Vector();
//String strContents = null;
strSql = "select * from News";
rs = db.OpenSql(strSql);
while (rs.next())
{
News news = new News();
news.setId(rs.getInt("id"));
news.setAdderId(rs.getInt("adderid"));
news.setTitle(rs.getString("title"));
//strContents = rs.getString("contents");
news.setContents(rs.getString("contents"));
news.setAddTime(rs.getString("addtime"));
newsList.add(news);
}//End of while
return(newsList);
}//End of getAllNews
/**
* 添加新的News信息
*/
public boolean addNews(DB db)
throws SQLException
{
String strSql = null;
ResultSet rs = null;
int iMax = 0;
//查找最大的ID号
strSql = "select Max(id) from News";
rs = db.OpenSql(strSql);
if (rs.next())
{
iMax = rs.getInt(1);
iMax++;
}//End of if
else
{
iMax = 1;
}//End of else
strSql = "insert into News values("
+ iMax + ","
+ adderId + ",'"
+ title + "','"
+ contents + "','"
+ addTime + "')";
if ( db.ExecSql(strSql) == 0 )
{
//添加失败
return(false);
}//End of if
else
{
//添加成功
return(true);
}//End of else
}//End of addNews
}//End of class News
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -