📄 news.java
字号:
}
/**
* 删除新闻
*/
public void delNews(int newsId){
if (sdbo==null)
sdbo = SQLDBOperator.getInstance("Connection");
String sql = "delete from news where newsId=?;";
try{
sdbo.prepareStatement(sql);
sdbo.setInt(1,newsId);
sdbo.executeUpdate();
}
catch(Exception e){
System.out.print("News delNews() " +e.getMessage());
Debug.writeLog("News delNews(), Exception Occured ! Info :" + e.getLocalizedMessage());
}
finally{
sdbo.Close();
}
}
/**
* 审核新闻(显示未被审核的文章)
*/
public Iterator listShNews(){
Vector vector=new Vector();
if (sdbo==null)
sdbo = SQLDBOperator.getInstance("Connection");
ResultSet rs = null;
String strSql = "select newsId,headTitle,author,DATE_FORMAT(newsTime,'%Y-%m-%d') as time from news where state=0;";
try{
rs = sdbo.executeQuery(strSql);
try{
while(rs.next()){
NEWSTable tableNews=new NEWSTable();
tableNews.setNewsId(rs.getInt("newsId"));
tableNews.setHeadTitle(rs.getString("headTitle"));
tableNews.setAuthor(rs.getString("author"));
tableNews.setNewsTime(rs.getString("time"));
vector.add(tableNews);
}
rs.close();
}
catch(NullPointerException nullE){
System.out.print("News listShNews() " + nullE.getMessage());
Debug.writeLog("News listShNews(), Exception Occured ! Info :" + nullE.getLocalizedMessage());
}
}
catch(SQLException sqlE){
System.out.print("News listShNews() " + sqlE.getMessage());
Debug.writeLog("News listShNews(), Exception Occured ! Info :" + sqlE.getLocalizedMessage());
}
finally{
sdbo.Close();
}
return vector.iterator();
}
/**
* 未被审核的文章总数
* @param newsId
*/
public int sumShNews(){
if (sdbo==null)
sdbo = SQLDBOperator.getInstance("Connection");
int sum=0;
String sql="select count(newsId) as total from news where state=0;";
try{
ResultSet rs = sdbo.executeQuery(sql);
try{
rs.next();
sum = rs.getInt("total");
rs.close();
}
catch(NullPointerException nullE){
System.out.println("News sumNews() :" +nullE.getMessage());;
Debug.writeLog("News sumNews(), Exception Occured ! Info :" + nullE.getLocalizedMessage());
}
}
catch(SQLException sqlE){
System.out.println("News sumNews() :" +sqlE.getMessage());
Debug.writeLog("News sumNews(), Exception Occured ! Info :" + sqlE.getLocalizedMessage());
}
finally{
sdbo.Close();
}
return sum;
}
/**
* 审核文章
*/
public void shNews(int newsId){
if (sdbo==null)
sdbo = SQLDBOperator.getInstance("Connection");
String strSql="update news set state=1 where newsId='"+newsId+"'";
try{
sdbo.executeUpdate(strSql);
}
catch(Exception e){
System.out.print("News listShNews() " + e.getMessage());
Debug.writeLog("News listShNews(), Exception Occured ! Info :" + e.getLocalizedMessage());
}
finally{
sdbo.Close();
}
}
//判断有无评论
public boolean isTalk(){
ResultSet rs = null;
String strSql = "select replyId from newsreply";
boolean isTalk=false;
if (sdbo==null)
sdbo = SQLDBOperator.getInstance("Connection");
try{
rs = sdbo.executeQuery(strSql);
try{
rs.last();
if(rs.getRow()>0){
isTalk=true;
}
rs.close();
}
catch(NullPointerException nullE){
System.out.print("News isTalk() " + nullE.getMessage());
Debug.writeLog("News isTalk(), Exception Occured ! Info :" + nullE.getLocalizedMessage());
}
}
catch(SQLException sqlE){
System.out.print("Usr isAdminName() " +sqlE.getMessage());
Debug.writeLog("Usr isAdminName(), Exception Occured ! Info :" + sqlE.getLocalizedMessage());
}
finally{
sdbo.Close();
}
return isTalk;
}
/**
* 含有评论的文章题目
*/
//对文章的回复的文章总数
public int sumReplyNews(){
if (sdbo==null)
sdbo = SQLDBOperator.getInstance("Connection");
int sum=0;
String sql="select distinct news.newsId from news,newsreply where news.newsId=newsreply.newsId;";
try{
ResultSet rs = sdbo.executeQuery(sql);
try{
rs.next();
sum = rs.getInt("totalNews");
rs.close();
}
catch(NullPointerException nullE){
System.out.println("News sumReplyNews() :" +nullE.getMessage());;
Debug.writeLog("News sumReplyNews(), Exception Occured ! Info :" + nullE.getLocalizedMessage());
}
}
catch(SQLException sqlE){
System.out.println("News sumReplyNews() :" +sqlE.getMessage());
Debug.writeLog("News sumReplyNews(), Exception Occured ! Info :" + sqlE.getLocalizedMessage());
}
finally{
sdbo.Close();
}
return sum;
}
/**
* 列出文章
*/
public Iterator listReplyNews(){
ResultSet rs = null;
String strSql = "select distinct news.newsId,news.headTitle,DATE_FORMAT(news.newsTime,'%Y-%m-%d') as time from news,newsreply where news.newsId=newsreply.newsId;";
Vector vector=new Vector();
if (sdbo==null)
sdbo = SQLDBOperator.getInstance("Connection");
try{
rs = sdbo.executeQuery(strSql);
try{
while(rs.next()){
NEWSTable tableNews = new NEWSTable();
tableNews.setNewsId(rs.getInt("newsId"));
tableNews.setHeadTitle(rs.getString("headTitle"));
tableNews.setNewsTime(rs.getString("time"));
vector.add(tableNews);
rs.close();
}
}
catch(NullPointerException nullE){
System.out.print("News listReplyNews() " + nullE.getMessage());
Debug.writeLog("News listReplyNews(), Exception Occured ! Info :" + nullE.getLocalizedMessage());
}
}
catch(SQLException sqlE){
System.out.print("Usr isAdminName() " +sqlE.getMessage());
Debug.writeLog("Usr isAdminName(), Exception Occured ! Info :" + sqlE.getLocalizedMessage());
}
finally{
sdbo.Close();
}
return vector.iterator();
}
/**
* 取得对评论文章的次数
*/
public int sumReply(int newsId){
ResultSet rs = null;
int sum=0;
String strSql = "select count(replyId) as total from newsreply where newsId=?;";
if (sdbo==null)
sdbo = SQLDBOperator.getInstance("Connection");
try{
sdbo.prepareStatement(strSql);
sdbo.setInt(1,newsId);
rs = sdbo.executeQuery();
try{
rs.next();
sum = rs.getInt("total");
rs.close();
}
catch(NullPointerException nullE){
System.out.print("Usr isAdminName() " + nullE.getMessage());
Debug.writeLog("Usr isAdminName(), Exception Occured ! Info :" + nullE.getLocalizedMessage());
}
}
catch(SQLException sqlE){
System.out.print("Usr isAdminName() " +sqlE.getMessage());
Debug.writeLog("Usr isAdminName(), Exception Occured ! Info :" + sqlE.getLocalizedMessage());
}
finally{
sdbo.Close();
}
return sum;
}
/**
* 列出回复的内容
*/
public Iterator listReply(int newsId){
ResultSet rs = null;
String strSql = "select replyId,newsId,user,content from newsreply where newsId=?;";
Vector vector=new Vector();
if (sdbo==null)
sdbo = SQLDBOperator.getInstance("Connection");
try{
sdbo.prepareStatement(strSql);
sdbo.setInt(1,newsId);
rs = sdbo.executeQuery();
try{
while(rs.next()){
NEWSReply tableReply = new NEWSReply();
tableReply.setReplyId(rs.getInt("replyId"));
tableReply.setUser(rs.getString("user"));
tableReply.setContent(rs.getString("content"));
vector.add(tableReply);
}
}
catch(NullPointerException nullE){
System.out.print("News listReplyNews() " + nullE.getMessage());
Debug.writeLog("News listReplyNews(), Exception Occured ! Info :" + nullE.getLocalizedMessage());
}
finally{
rs.close();
}
}
catch(SQLException sqlE){
System.out.print("Usr isAdminName() " +sqlE.getMessage());
Debug.writeLog("Usr isAdminName(), Exception Occured ! Info :" + sqlE.getLocalizedMessage());
}
finally{
sdbo.Close();
}
return vector.iterator();
}
/**
* 删除回复
*/
public void delNewsReply(int replyId){
if (sdbo==null)
sdbo = SQLDBOperator.getInstance("Connection");
String strSql="delete from NEWSReply where replyId=?;";
try{
sdbo.prepareStatement(strSql);
sdbo.setInt(1,replyId);
sdbo.executeUpdate();
}
catch(Exception e){
System.out.print("News listShNews() " + e.getMessage());
Debug.writeLog("News listShNews(), Exception Occured ! Info :" + e.getLocalizedMessage());
}
finally{
sdbo.Close();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -