notesdao.java

来自「java带进度条上传尽量不要让站长把时间都花费在为您修正说明上」· Java 代码 · 共 417 行

JAVA
417
字号
package com.jmwl.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.jmwl.common.BlogException;
import com.jmwl.common.DateTime;
import com.jmwl.dto.NoteDTO;
import com.jmwl.dto.Note_updateDTO;
import com.jmwl.vo.NoteVO;
import com.jmwl.vo.NotelistVO;

public class NotesDAO extends BasicDAO{

	DateTime time=new DateTime();
	public boolean addNotes(String notename,int userid) throws BlogException
	{
		boolean b = false;
		String sql = "insert into note_type(note_name,user_id) values(?,?)";
		try {
			PreparedStatement ps = this.getConn().prepareStatement(sql);
			ps.setString(1,notename );
			ps.setInt(2, userid);
			int i = ps.executeUpdate();
			if(i==1)
				b = true;
			return b;
		} catch (SQLException e) {
			throw new BlogException("对不起,添加日志时的SQL语句错误");
		}
	}
	/**
	 * 添加一篇日志
	 * @param info
	 * @return
	 * @throws BlogException 
	 */
	public boolean addnote(NoteDTO info) throws BlogException
	{
		boolean b=false;
		String sql="insert into note(title,content,pub_time,user_id,see_count,note_type_id,note_level) values(?,?,?,?,?,?,?) ";
		try {
			PreparedStatement ps=this.getConn().prepareStatement(sql);
			ps.setString(1, info.getTitle());
			ps.setString(2, info.getContent());
			ps.setString(3, time.getCurTime());
			ps.setInt(4, info.getUser_id());
			ps.setInt(5, 0);
			ps.setInt(6, info.getNote_type_id());
			ps.setInt(7, 0);
			int i=ps.executeUpdate();
			if(i==1)
			{
				b=true;
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
			throw new BlogException("对不起,添加日志时的SQL语句错误");
		}
		return b;
	}
	/**
	 * 删除一篇日志
	 * @param id
	 * @return
	 * @throws BlogException 
	 */
	public boolean delete_note(int id) throws BlogException
	{
		boolean b=false;
		String sql="delete from note where id=?";
		try {
			PreparedStatement ps=this.getConn().prepareStatement(sql);
			ps.setInt(1, id);
			int i=ps.executeUpdate();
			if(i==1)
			{
				b=true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
			throw new BlogException("对不起,删除日志时的SQL语句错误");
		}
		return b;
	}
	/**
	 * 修改日志
	 * @param info
	 * @return
	 * @throws BlogException
	 */
	public boolean update_note(Note_updateDTO info) throws BlogException
	{
		boolean b=false;
		String sql="update note set title=?,content=?,note_type_id=? where id=?";
		try {
			PreparedStatement ps=this.getConn().prepareStatement(sql);
			ps.setString(1, info.getTitle());
			ps.setString(2, info.getContent());
			ps.setInt(3, info.getNtoe_type_id());
			ps.setInt(4, info.getId());
			int i=ps.executeUpdate();
			if(i==1)
			{
				b=true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
			throw new BlogException("对不起,修改日志时的SQL语句错误");
			
		}
		return b;
	}
	/**
	 * 得到一篇日志
	 * @param id
	 * @return
	 * @throws BlogException
	 */
	public NoteVO getnote(int id) throws BlogException
	{
		NoteVO info=new NoteVO();
		String sql="select * from note where id=?";
		try {
			PreparedStatement ps=this.getConn().prepareStatement(sql);
			ps.setInt(1, id);
			ResultSet rs=ps.executeQuery();
			while(rs.next())
			{
				info.setId(rs.getInt(1));
				info.setTitle(rs.getString(2));
				info.setContent(rs.getString(3));
				info.setPub_time(rs.getTimestamp(4));
				info.setUser_id(rs.getInt(5));
				info.setSee_count(rs.getInt(6));
				info.setNote_type_id(rs.getInt(7));
				info.setNote_level(rs.getInt(8));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new BlogException("对不起,显示日志时的SQL语句错误");
		}
		return info;
	}
	/**
	 * 得到一个日志列表
	 * @return
	 * @throws BlogException 
	 */
	public List<NoteVO> get_all_note(int id,int currpage) throws BlogException
	{
		List<NoteVO> list=new ArrayList();
		String sql="select * from note where user_id=? order by pub_time desc limit "+(currpage-1)*10+",10";
		PreparedStatement ps;
		try {
			ps = this.getConn().prepareStatement(sql);
			ps.setInt(1, id);
			ResultSet rs=ps.executeQuery();
			while(rs.next())
			{
				NoteVO info=new NoteVO();
				info.setId(rs.getInt(1));
				info.setTitle(rs.getString(2));
				info.setContent(rs.getString(3));
				info.setPub_time(rs.getTimestamp(4));
				info.setUser_id(rs.getInt(5));
				info.setSee_count(rs.getInt(6));
				info.setNote_type_id(rs.getInt(7));
				info.setNote_level(rs.getInt(8));
				list.add(info);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new BlogException("对不起,得到日志列表时的SQL语句错误");
		}	
		return list;
	}
	/**
	 * 得到一个指定类型的日志列表
	 * @param id
	 * @return
	 * @throws BlogException 
	 */
	public List<NoteVO> get_type_note(int id,int bid,int currpage) throws BlogException
	{
		List<NoteVO> list=new ArrayList();
		String sql="select * from note where note_type_id=? and user_id=? order by pub_time desc limit "+(currpage-1)*10+",10";
		PreparedStatement ps;
		try {
			ps = this.getConn().prepareStatement(sql);
			ps.setInt(1, id);
			ps.setInt(2, bid);
			ResultSet rs=ps.executeQuery();
			while(rs.next())
			{
				NoteVO info=new NoteVO();
				info.setId(rs.getInt(1));
				info.setTitle(rs.getString(2));
				info.setContent(rs.getString(3));
				info.setPub_time(rs.getTimestamp(4));
				info.setUser_id(rs.getInt(5));
				info.setSee_count(rs.getInt(6));
				info.setNote_type_id(rs.getInt(7));
				info.setNote_level(rs.getInt(8));
				list.add(info);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new BlogException("对不起,显示指定类型日志列表时的SQL语句错误");
		}	
		return list;
	}
	
	/**
	 * 显示所有日志的数量
	 * @return
	 * @throws BlogException
	 */
	public int getcount(int id) throws BlogException
	{
		int n=0;
		String sql="select count(*) from note where user_id=?";
		try {
			PreparedStatement ps=this.getConn().prepareStatement(sql);
			ps.setInt(1, id);
			ResultSet rs=ps.executeQuery();
			while(rs.next())
			{
				n=rs.getInt(1);
			}
		} catch (SQLException e) {
			throw new BlogException("对不起,显示所有日志数量时的SQL语句错误");
		}
		
		return n;
	}
	/**
	 * 显示所有日志的页数
	 * @param id
	 * @return
	 * @throws BlogException
	 */
	public int getcountpage(int id) throws BlogException
	{
		int page=0;
		int countinfo=getcount(id);
		System.out.println("countinfo-----"+countinfo);
		if(countinfo==0)
		{
			return 1;
		}
		if(countinfo%10==0)
		{
			page=countinfo/10;
		}
		else
		{
			page=countinfo/10+1;
		}
		return page;
	}
	/**
	 * 显示某种日志类型的日志数量
	 * @param uid
	 * @param tid
	 * @return
	 * @throws BlogException 
	 */
	public int get_note_sl(int uid,int tid) throws BlogException
	{
		int n=0;
		String sql="select count(*) from note where user_id=? and note_type_id=?";
		try {
			PreparedStatement ps=this.getConn().prepareStatement(sql);
			ps.setInt(1, uid);
			ps.setInt(2, tid);
			ResultSet rs=ps.executeQuery();
			while(rs.next())
			{
				n=rs.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
//			throw new BlogException("对不起,显示某种日志类型的日志数量时的SQL语句错误");
		}
		return n;
	}
	/**
	 * 得到指定类型的日志分页页数
	 * @param uid
	 * @param tid
	 * @return
	 * @throws BlogException
	 */
	public int getonepage(int uid,int tid) throws BlogException
	{
		int page=0;
		int countinfo=get_note_sl(uid,tid);
		if(countinfo==0)
		{
			return 1;
		}
		if(countinfo%10==0)
		{
			page=countinfo/10;
		}
		else
		{
			page=countinfo/10+1;
		}
		return page;
	}
	/**
	 * 添加一次查看次数
	 * @param id
	 * @return
	 * @throws BlogException 
	 */
	public boolean add_see_cs(int id) throws BlogException
	{
		boolean b=false;
		String sql="update note set see_count=see_count+1 where id=?";
		try {
			PreparedStatement ps=this.getConn().prepareStatement(sql);
			ps.setInt(1, id);
			int i=ps.executeUpdate();
			if(i==1)
			{
				b=true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new BlogException("对不起,添加一次查看次数时的SQL语句错误");
		}
		return b;
	}
	public List<NotelistVO> get_list() throws BlogException
	{
		List<NotelistVO> list=new ArrayList();
		String sql="select * from note_type";
		try {
			PreparedStatement ps=this.getConn().prepareStatement(sql);
			ResultSet rs=ps.executeQuery();
			while(rs.next())
			{
				NotelistVO info=new NotelistVO();
				info.setId(rs.getInt(1));
				info.setType_name(rs.getString(2));
				list.add(info);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			throw new BlogException("对不起,得到日志类型列表时的SQL语句错误");
		}
		return list;
	}
	/**
	 * 给回复的用户加分
	 * @param id
	 * @return
	 * @throws BlogException 
	 */
	public boolean add_hf_jf(int id) throws BlogException
	{
		boolean b=false;
		String sql="update userslogin set counter=counter+1 where uid=?";
		try {
			PreparedStatement ps=this.getConn().prepareStatement(sql);
			ps.setInt(1, id);
			int n=ps.executeUpdate();
			if(n==1)
			{
				b=true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
			throw new BlogException("对不起,回复用户加分时的SQL语句错误");
		}
		
		return b;
		
	}
	/**
	 * 写日志用户加分
	 * @param id
	 * @return
	 * @throws BlogException
	 */
	public boolean add_note_jf(int id) throws BlogException
	{
		boolean b=false;
		String sql="update userslogin set counter=counter+5 where uid=?";
		try {
			PreparedStatement ps=this.getConn().prepareStatement(sql);
			ps.setInt(1, id);
			int n=ps.executeUpdate();
			if(n==1)
			{
				b=true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
			throw new BlogException("对不起,写日志用户加分时的SQL语句错误");
		}

		return b;
	}
}

⌨️ 快捷键说明

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