News.java
上传用户:jishiqi_cj
上传日期:2022-08-08
资源大小:24765k
文件大小:4k
- package StudyNews;
- import java.util.*;
- import java.sql.ResultSet;
- public class News{
-
- protected int id;
- protected String title ;
- protected String content;
- protected String author;
- protected String time;
- protected String keyword ;
- protected int type;
-
- public News(){ }
-
- public void setId(int id) {
- this.id = id;
- }
-
- public void setTitle(String title) {
- this.title = title;
- }
-
- public void setContent(String content) {
- this.content = content;
- }
-
- public void setAuthor(String author) {
- this.author = author;
- }
-
- public void setTime(String time) {
- this.time = time;
- }
-
- public void setKeyword(String keyword) {
- this.keyword = keyword;
- }
-
- public void setType(int type) {
- this.type = type;
- }
-
- public int getId(){
- return id;
- }
-
- public String getTitle(){
- return title;
- }
-
- public String getContent(){
- return content;
- }
-
- public String getAuthor(){
- return author;
- }
-
- public String getTime(){
- return time;
- }
-
- public String getKeyword(){
- return keyword;
- }
-
- public int getType(){
- return type;
- }
-
- public boolean Insert(DB db) throws Exception{
- String strSql;
- ResultSet rs;
- int iMaxId;
- strSql = "Select max(id) From news";
- rs = db.OpenSql(strSql);
- if ( rs.next()) {
- iMaxId=rs.getInt(1)+1;
- }
- else{
- iMaxId=1;
- }
-
- strSql = "insert into news values("
- + iMaxId +",'"
- + title +"','"
- + content +"','"
- + author +"',sysdate,'"
- + keyword +"',"
- + type +")";
- if ( db.ExecSql(strSql)==0) {
- return false;
- }
- else{
- return true;
- }
-
- }
-
- public boolean Edit(DB db) throws Exception{
- String strSql;
- strSql = "update news set title='"+title+"',"
- + " content='"+ content +"',"
- + " author='"+ author +"',"
- + " keyword='"+ keyword +"',"
- + " type="+ type
- + " where id="+id;
- if ( db.ExecSql(strSql)==0) {
- return false;
- }
- else{
- return true;
- }
- }
-
- public static Vector SearchNewsTitle(DB db) throws Exception{
- Vector newsList = new Vector();
- ResultSet rs,rsNest;
- String strSql=null;
-
- strSql = "select * from news order by time desc";
- rs = db.OpenSql(strSql);
-
- while (rs.next()){
- News news = new News();
-
- news.setId(rs.getInt("id")) ;
- news.setTitle(rs.getString("title")) ;
- news.setTime(rs.getString("time")) ;
- news.setType(rs.getInt("type")) ;
-
- newsList.add(news);
- }
- System.out.println("newsList: "+newsList.size());
-
- return newsList;
- }
-
- public static Vector SearchRelativeNews(DB db,int newsId,String keyword) throws Exception{
- Vector newsList = new Vector();
- ResultSet rs,rsNest;
- String strSql=null;
-
- strSql = "select * from news where id<>" + newsId +" and title like '%"
- + keyword + "%' order by time desc";
- rs = db.OpenSql(strSql);
-
- while (rs.next()){
- News news = new News();
-
- news.setId(rs.getInt("id")) ;
- news.setTitle(rs.getString("title")) ;
- news.setTime(rs.getString("time")) ;
-
- newsList.add(news);
- }
- System.out.println("newsList: "+newsList.size());
-
- return newsList;
- }
-
- public static News GetDetail(DB db,int newsId,boolean bEdit) throws Exception{
- Vector newsList = new Vector();
- ResultSet rs,rsNest;
- String strSql=null;
- String rplContent=null;
-
- strSql = "select * from news where id = " + newsId ;
- rs = db.OpenSql(strSql);
- News news = new News();
- if (rs.next()){
-
- news.setId(newsId) ;
- news.setTitle(rs.getString("title")) ;
- news.setAuthor(rs.getString("author")) ;
-
- rplContent = rs.getString("content");
-
- if(!bEdit){
- rplContent = rplContent.replaceAll("n","<br>");
- }
- news.setContent(rplContent) ;
- news.setTime(rs.getString("time")) ;
- news.setKeyword(rs.getString("keyword")) ;
- news.setType(rs.getInt("type")) ;
- }
- return news;
- }
-
- public static boolean Delete(DB db,int newsId) throws Exception{
- String strSql;
- strSql = "delete from news where id='"+newsId+"'";
- if ( db.ExecSql(strSql)==0) {
- return false;
- }
- else{
- return true;
- }
- }
-
- }