📄 article.java
字号:
/***********************************************
/*
/*文章查询、更新、删除等等操作
/*
/******************************************* */
package com.index;
import java.sql.*;
import java.util.ArrayList;
import com.util.*;
public class Article {
public int id;
public String title = "";
public String content="";
public String author="";
public String createtime=null;
public int sortid=0;
public int hit=0;
public int flag;
public String filename="";
public Article() {
}
public ArrayList View(int sort_id) throws Exception{
Connection conn=null;
PreparedStatement stmt =null;
ResultSet rs =null;
String sql ="select id,title,content,author,createtime," +
"hit from ts_article where sortid=? and flag=1 order by id desc";
ArrayList<Article> result=new ArrayList<Article>();
try{
conn=DBConn.getConn();
stmt= conn.prepareStatement(sql);
stmt.setInt(1,sort_id);
rs = stmt.executeQuery();
while (rs.next()){
Article article=new Article();
article.id=rs.getInt(1);
article.title=rs.getString(2).trim();
article.content=rs.getString(3);
article.author=rs.getString(4);
article.createtime=rs.getString(5);
article.hit=rs.getInt(6);
result.add(article);
}
}
catch(Exception e){
e.printStackTrace();
}
finally{
try{
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(conn!=null)
conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
return result;
}
public Boolean ViewArticle(int ArticleId) throws Exception{
Connection conn=null;
PreparedStatement stmt =null;
ResultSet rs =null;
String sql ="select id,title,content,author,createtime," +
"hit,filename,sortid,flag from ts_article where id=? ";
boolean result=false;
try{
conn=DBConn.getConn();
stmt= conn.prepareStatement(sql);
stmt.setInt(1,ArticleId);
rs = stmt.executeQuery();
if (rs.next()){
id=rs.getInt(1);
title=rs.getString(2).trim();
content=rs.getString(3);
author=rs.getString(4);
createtime=rs.getString(5);
hit=rs.getInt(6);
filename=rs.getString(7);
sortid=rs.getInt(8);
flag=rs.getInt(9);
result=true;
}
}
catch(Exception e){
e.printStackTrace();
}
finally{
try{
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(conn!=null)
conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
return result;
}
public Boolean UpHit(int ArticleId) throws Exception{
Connection conn=null;
PreparedStatement stmt =null;
ResultSet rs =null;
String sql ="update ts_article set hit=hit+1 where id=?";
boolean result=false;
try{
conn=DBConn.getConn();
stmt= conn.prepareStatement(sql);
stmt.setInt(1,ArticleId);
stmt.executeUpdate();
result=true;
}
catch(Exception e){
e.printStackTrace();
}
finally{
try{
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(conn!=null)
conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
return result;
}
public Boolean AddArticle() throws Exception{
Connection conn=null;
PreparedStatement stmt =null;
ResultSet rs =null;
String sql ="insert into ts_article (title,content,author,sortid,createtime,filename) values (?,?,?,?,?,?)";
boolean result=false;
try{
java.util.Date date=new java.util.Date();
Timestamp tt=new Timestamp(date.getTime());
conn=DBConn.getConn();
stmt= conn.prepareStatement(sql);
stmt.setString(1,this.title);
stmt.setString(2,this.content);
stmt.setString(3,this.author);
stmt.setInt(4,this.sortid);
stmt.setTimestamp(5,tt);
stmt.setString(6, this.filename);
stmt.executeUpdate();
result=true;
}
catch(Exception e){
e.printStackTrace();
}
finally{
try{
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(conn!=null)
conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
return result;
}
//修改文章
public Boolean EditArticle() throws Exception{
Connection conn=null;
PreparedStatement stmt =null;
ResultSet rs =null;
String sql ="update ts_article set title=?,content=?,author=?,sortid=?,filename=?,flag=? where id=?";
boolean result=false;
try{
conn=DBConn.getConn();
stmt= conn.prepareStatement(sql);
stmt.setString(1,this.title);
stmt.setString(2,this.content);
stmt.setString(3,this.author);
stmt.setInt(4,this.sortid);
stmt.setString(5,filename);
stmt.setInt(6,this.flag);
stmt.setInt(7, this.id);
stmt.executeUpdate();
result=true;
}
catch(Exception e){
e.printStackTrace();
}
finally{
try{
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(conn!=null)
conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
return result;
}
public Boolean DelArticle(int a_id) throws Exception{
Connection conn=null;
PreparedStatement stmt =null;
ResultSet rs =null;
String sql ="delete from ts_article where id=?";
boolean result=false;
try{
conn=DBConn.getConn();
stmt= conn.prepareStatement(sql);
stmt.setInt(1,a_id);
stmt.executeUpdate();
result=true;
}
catch(Exception e){
e.printStackTrace();
}
finally{
try{
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(conn!=null)
conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
return result;
}
//审核列表
public ArrayList Audit() throws Exception{
Connection conn=null;
PreparedStatement stmt =null;
ResultSet rs =null;
String sql ="select id,title,content,author,createtime," +
"sortid,hit,flag from ts_article where flag!=1 order by id desc";
ArrayList<Article> result=new ArrayList<Article>();
try{
conn=DBConn.getConn();
stmt= conn.prepareStatement(sql);
rs = stmt.executeQuery();
while (rs.next()){
Article article=new Article();
article.id=rs.getInt(1);
article.title=rs.getString(2).trim();
article.content=rs.getString(3);
article.author=rs.getString(4);
article.createtime=rs.getString(5);
article.sortid=rs.getInt(6);
article.hit=rs.getInt(7);
article.flag=rs.getInt(8);
result.add(article);
}
}
catch(Exception e){
e.printStackTrace();
}
finally{
try{
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(conn!=null)
conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
return result;
}
//按点击率排列
public ArrayList ArticleByHit(int sort_id) throws Exception{
Connection conn=null;
PreparedStatement stmt =null;
ResultSet rs =null;
String sql ="select id,title from ts_article where flag=1 order by hit desc";
ArrayList<Article> result=new ArrayList<Article>();
try{
if(sort_id!=-1){
sql ="select id,title from ts_article where flag=1 and sortid="+sort_id+" order by hit desc";
}
conn=DBConn.getConn();
stmt= conn.prepareStatement(sql);
rs = stmt.executeQuery();
while (rs.next()){
Article article=new Article();
article.id=rs.getInt(1);
article.title=rs.getString(2).trim();
result.add(article);
}
}
catch(Exception e){
e.printStackTrace();
}
finally{
try{
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(conn!=null)
conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
return result;
}
//搜索
public ArrayList Search(int sort_id,int flag,String key) throws Exception{
Connection conn=null;
PreparedStatement stmt =null;
ResultSet rs =null;
String sql="";
if(sort_id!=-1){
sql ="select id,title,content,author,createtime," +
"sortid from ts_article where sortid="+sort_id+" and flag=1 ";
}else{
sql ="select id,title,content,author,createtime," +
"sortid from ts_article where flag=1 ";
}
if(flag==1){
sql=sql+"and title like ? order by id desc";
}else{
sql=sql+"and content like ? order by id desc";
}
ArrayList<Article> result=new ArrayList<Article>();
try{
conn=DBConn.getConn();
stmt= conn.prepareStatement(sql);
stmt.setString(1, "%"+ key + "%");
rs = stmt.executeQuery();
while (rs.next()){
Article article=new Article();
article.id=rs.getInt(1);
article.title=rs.getString(2).trim();
article.content=rs.getString(3);
article.author=rs.getString(4);
article.createtime=rs.getString(5);
article.sortid=rs.getInt(6);
result.add(article);
}
}
catch(Exception e){
e.printStackTrace();
}
finally{
try{
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(conn!=null)
conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
return result;
}
//审核
public Boolean Audit_article() throws Exception{
Connection conn=null;
PreparedStatement stmt =null;
ResultSet rs =null;
String sql ="insert into ts_article (title,content,author,sortid,createtime,filename,flag) values (?,?,?,?,?,?,?)";
boolean result=false;
try{
java.util.Date date=new java.util.Date();
Timestamp tt=new Timestamp(date.getTime());
conn=DBConn.getConn();
stmt= conn.prepareStatement(sql);
stmt.setString(1,this.title);
stmt.setString(2,this.content);
stmt.setString(3,this.author);
stmt.setInt(4,this.sortid);
stmt.setTimestamp(5,tt);
stmt.setString(6, this.filename);
stmt.setInt(7,2);
stmt.executeUpdate();
result=true;
}
catch(Exception e){
e.printStackTrace();
}
finally{
try{
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(conn!=null)
conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
return result;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -