⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 news.java

📁 本系统实现的是一个信息发布平台
💻 JAVA
字号:
/*
 * 创建日期 2005-8-24
 *
 * TODO 要更改此生成的文件的模板,请转至
 * 窗口 - 首选项 - Java - 代码样式 - 代码模板
 */
package cn.edu.bit.Object;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Vector;

import cn.edu.bit.DBQuery.DataProcess;
import cn.edu.bit.Object.Pageable;

/**
 * @author ligang
 *
 * TODO 要更改此生成的类型注释的模板,请转至
 * 窗口 - 首选项 - Java - 代码样式 - 代码模板
 */
public class News {
	
	private int id;
	
	private String title;
	
	private String content;
	
	private String time;
	
	News()
	{
		
	}
	
	News(int id,String title,String content,String time)
	{
		this.id = id;
		
		this.title=title;
		
		this.content = content;
		
		this.time = time;
	}
	
	public void setId(int id)
	{
		this.id = id;
	}
	
	public int getId()
	{
		return this.id;
	}
	
	public void setTitle(String title)
	{
		this.title = title;
	}
	
	public String getTitle()
	{
		return this.title;
	}
	
	public void setContent(String content)
	{
		this.content = content;
	}
	
	public String getContent()
	{
		return this.content;
	}
	
	public void setTime(String time)
	{
		this.time = time;
	}
	
	public String getTime()
	{
		return this.time;
	}
	
	public static Vector search(String strSql, int page) throws Exception {

        // 容器,用于存放Bean
        Vector Items = new Vector();

        // 得到数据库连接
        Connection con = DataProcess.getConnection();

        // 预处理语句
        PreparedStatement pStmt = null;

        // 结果集
        ResultSet rs = null;

        try {
            pStmt = con.prepareStatement(strSql,
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY);
            rs = pStmt.executeQuery();

            // 得到一个分页功能类
            Pageable pgb = new Pageable(rs);

            // 设置分页的大小
            pgb.setPageSize(5);

            // 设置要转向的类
            
            pgb.gotoPage(page);
            
            // 在结果集中定位到某行,在这里,这一行是要转向的页的第一行
            rs.absolute(pgb.getRowsCount());

            int i = 0;
            // 取得要转向页的那些行的信息,将其按行存入Bean中,并将这些Bean存入容器中
            do {
                Items.add(new News(rs.getInt("id"),rs.getString("title"),rs.getString("content"),rs.getString("datetime")));
                i++;
            }while (rs.next() && i < pgb.getCurrentPageRowsCount());

            return Items;
            
        } finally {
            try {
                rs.close();
                pStmt.close();
                con.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
	
	public static News getNews(int ID)
	{
		String selectStr = "select * from news where id="+ID;
		
		Connection con = DataProcess.getConnection();
		
		News news = new News();
		
		try{
			
			Statement stmt = con.createStatement();
			
			ResultSet rs = stmt.executeQuery(selectStr);
			
			if(rs.next())
			{				
				String title = rs.getString("title");
				
				String content = rs.getString("content");
				
				String time = rs.getString("datetime");
				
				news.setId(ID);
				
				news.setTitle(title);
				
				news.setContent(content);
				
				news.setTime(time);
			}
			
			rs.close();
			
			stmt.close();
			
			con.close();
			
			return news;
		
		}
		catch(Exception e)
		{
			e.printStackTrace();
			
			return null;
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -