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

📄 booksmn.java

📁 课程设计期间开发的JSP系统,后台采用SQL.这是一个很好的系统,适合初学者研究
💻 JAVA
字号:
package model;

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

import javax.servlet.http.HttpServletRequest;

import model.strFormat;
import org.pan.web.book.books;


public class booksmn extends DataBase {
	private books abooks = new books();	//新的图书类
	private javax.servlet.http.HttpServletRequest request; //建立页面请求
	private boolean sqlflag = true ;		//对接收到的数据是否正确
	private List<books> booklist;				//显示图书列表向量数组
	private int page = 1;					//显示的页码
	private int pageSize=10;				//每页显示的图书数
	private int pageCount =0;				//页面总数
	private int recordCount =0;			//查询的记录总数

	
	public booksmn() throws Exception{
		super();
	}

	public List<books> getBooklist() {
		return booklist;
	}

	public boolean getSqlflag() {
		return sqlflag;
	}

	public String getGbk( String str) {
		try
		{
			str =  new String(str.getBytes("ISO-8859-1"),"GBK");
			return str;
		}
		catch (Exception e)
		{
			return str;
		}
	}
	
	//将页面表单传来的资料分解
	public boolean getRequest(javax.servlet.http.HttpServletRequest newrequest) {
		boolean flag = false;
		try
		{	
			request = newrequest;
			String ID = request.getParameter("id");
			int bookid = 0;
			try
			{
				bookid = Integer.parseInt(ID);
			}
			catch (Exception e)
			{
			}
			abooks.setId(bookid);
			String bookname = request.getParameter("bookname");
			if (bookname==null || bookname.equals(""))
			{
				bookname = "";
				sqlflag = false;
			}
			abooks.setBookName(getGbk(bookname)); 
			String author = request.getParameter("author");
			if (author==null || author.equals(""))
			{	
				author = "";
				sqlflag = false;
			}
			abooks.setAuthor(getGbk(author));
			String publish = request.getParameter("publish");
			if (publish==null)
			{
				publish = "";
			}
			abooks.setPublish(getGbk(publish));
			String bookclass = request.getParameter("bookclass");				
			int bc = Integer.parseInt(bookclass);
			abooks.setBookClass(bc);
			String bookno = request.getParameter("bookno");
			if (bookno == null)
			{
				bookno = "";
			}
			abooks.setBookNo(getGbk(bookno));
			float price;
			try	{
				price =new Float(request.getParameter("price")).floatValue();
			}
			catch (Exception e)
			{	
				price = 0;
				sqlflag = false;
			}
			abooks.setPrince(price);
			
			String content = request.getParameter("content");
			if (content == null)
			{	
				content = "";
			}
			abooks.setContent(getGbk(content));	
			if (sqlflag)
			{
				flag = true;
			}
			return flag;					
		}
		catch (Exception e)
		{
			return flag;
		}
	}

	public String getSql() {
		sqlStr = "select id,classname from my_Book order by id";
		return sqlStr;
	}


	public boolean execute(HttpServletRequest res) throws Exception {
		request = res;
		String PAGE = request.getParameter("page");   //页码
		String classid = request.getParameter("classid");	//分类ID号
		String keyword = request.getParameter("keyword");	//查询关键词
		if (classid==null) classid="";		
		if (keyword==null) keyword = "";
		keyword = getGbk(keyword).toUpperCase();
		try
		{
			page = Integer.parseInt(PAGE);
		}
		catch (NumberFormatException e)
		{
			page = 1;
		}
		 
		//取出记录数
		if (!classid.equals("") && keyword.equals("") ) {		
			sqlStr = "select count(*) from my_book where bookclass='" + classid + "'";
		} else if (!keyword.equals("")) {
			if (classid.equals(""))
			{
				sqlStr = "select count(*) from my_book where upper(bookname) like '%" +keyword+ "%' or upper(content) like '%" + keyword + "%'";
			} else {
				sqlStr = "select count(*) from my_book where bookclass='" + classid + "' and  (upper(bookname) like '%" +keyword+ "%' or upper(content) like '%" + keyword + "%')";
			}
		} else {
			sqlStr = "select count(*) from my_book";   
		}

		int rscount = pageSize;
		try
		{
			ResultSet rs1 = stmt.executeQuery(sqlStr);
			if (rs1.next()) recordCount = rs1.getInt(1);				
			rs1.close();
		}
		catch (SQLException e)
		{
			return false;
		}
		//设定有多少pageCount
		if (recordCount < 1)
            pageCount = 0;
        else
            pageCount = (int)(recordCount - 1) / pageSize + 1;
		//检查查看的页面数是否在范围内
		if (page < 1)  
            page = 1;
        else if (page > pageCount)
            page = pageCount;
		
		rscount = (int) recordCount % pageSize;	 // 最后一页记录数        

		//sql为倒序取值
		sqlStr = "select  a.id,a.bookname,a.bookclass,b.classname,a.author,a.publish,a.bookno,a.content,a.prince from My_book a,My_bookclass b where a.Bookclass = b.Id ";
		if (!classid.equals("") && keyword.equals("") ){  //如果类别不为空,非查询
			if (page == 1)
			{
				sqlStr = sqlStr + " and a.bookclass='" + classid + "' order by a.Id desc";
			} else {
				sqlStr = sqlStr + " and a.bookclass='" + classid + "' and a.Id not in ( select Id from My_book order by Id ) and a.Id in " +
				"(select Id from My_book ORDER BY Id )  order by a.Id desc";
			}		
		} else if (!keyword.equals("")) {  //如果是查询资料
			if (page == 1)
			{
				if (!classid.equals(""))  //查询某一类
				{
					sqlStr = sqlStr + "and a.Bookclass='" + classid + "' and (upper(a.bookname) like '%" +keyword+ "%' or upper(a.content) like '%" + keyword + "%')  order by a.Id desc";
				} else {		//查询所有类
					sqlStr = sqlStr + " and (upper(a.bookname) like '%" +keyword+ "%' or upper(a.content) like '%" + keyword + "%') order by a.Id desc";
				}
			} else { 
				if (!classid.equals(""))
				{
					sqlStr = sqlStr + " and a.Bookclass='" + classid + "' and (upper(a.bookname) like '%" +keyword+ "%' or upper(a.content) like '%" + keyword + "%') and a.Id not in ( select Id from My_book ORDER BY Id ) and a.Id in " +
					"(select Id from My_book ORDER BY Id ) " + " order by a.Id desc";
				} else {
					sqlStr = sqlStr + " and (upper(a.bookname) like '%" +keyword+ "%' or upper(a.content) like '%" + keyword + "%') and a.Id not in ( select Id from My_book ORDER BY Id ) and a.Id in " +
					"(select Id from My_book ORDER BY Id ) " + " order by a.Id desc";
				}
			}	

		} else {		//非查询,也非分类浏览
			if (page == 1)
			{
				sqlStr = sqlStr + "  order by a.Id desc";
			} else {
				sqlStr = sqlStr + " and a.Id not in ( select Id from My_book ORDER BY Id ) and a.Id in " +
				"(select Id from My_book ORDER BY Id) order by a.Id desc";
			}		
		}

		try
		{
			rs = stmt.executeQuery(sqlStr);
//			booklist = new Vector(rscount);
			booklist = new ArrayList<books>();
			while (rs.next())
			{
				books book = new books();
				book.setId(rs.getInt("id"));
				book.setBookName(rs.getString("bookname"));
				book.setBookClass(rs.getInt("bookclass"));
				book.setClassname(rs.getString("classname"));
				book.setAuthor(rs.getString("author"));
				book.setPublish(rs.getString("publish"));
				book.setBookNo(rs.getString("bookno"));
				book.setContent(rs.getString("content"));
				book.setPrince(rs.getFloat("prince"));
				
				
				booklist.add(book);
			}
			rs.close();
			return true;
		}
		catch (SQLException e)
		{
			
			System.out.println(e);
			return false;
		}
		

	}

	public boolean insert() throws Exception {
		int newid = 1;
		sqlStr = "select max(id)+1 as newid from my_book";
		rs = stmt.executeQuery(sqlStr);
		if (rs.next())
		{
			newid = rs.getInt("newid");
		}

		sqlStr = "insert into my_book (id,Bookname,Bookclass,Author,Publish,Bookno,Content,Prince ) values ("+newid+",'";
		sqlStr = sqlStr + strFormat.toSql(abooks.getBookName()) + "','";
		sqlStr = sqlStr + abooks.getBookClass() + "','";
		sqlStr = sqlStr + strFormat.toSql(abooks.getAuthor()) + "','";
		sqlStr = sqlStr + strFormat.toSql(abooks.getPublish()) + "','";
		sqlStr = sqlStr + strFormat.toSql(abooks.getBookNo()) + "','";
		sqlStr = sqlStr + strFormat.toSql(abooks.getContent()) + "',";
		sqlStr = sqlStr + abooks.getPrince()+")";
		
		try
		{
			stmt.execute(sqlStr);
			return true;
		}
		catch (SQLException sqle)
		{
//			System.out.println(sqle);
			sqle.printStackTrace();
			return false;
		}
	}

	public boolean update() throws Exception {
		sqlStr = "update my_book set ";
		sqlStr = sqlStr + "bookname = '" + strFormat.toSql(abooks.getBookName()) + "',";
		sqlStr = sqlStr + "bookclass = '" + abooks.getBookClass() + "',";
		sqlStr = sqlStr + "Author = '" + strFormat.toSql(abooks.getAuthor()) + "',";
		sqlStr = sqlStr + "publish = '" + strFormat.toSql(abooks.getPublish()) + "',";
		sqlStr = sqlStr + "bookno = '" + strFormat.toSql(abooks.getBookNo()) + "',";
		sqlStr = sqlStr + "content = '" + strFormat.toSql(abooks.getContent()) + "',";
		sqlStr = sqlStr + "prince = '" + abooks.getPrince() + "' ";
		
		sqlStr = sqlStr + "where id = '" + abooks.getId() + "'";
		try
		{		
			stmt.execute(sqlStr);
			return true;
		}
		catch (SQLException e)
		{
			return false;
		}
	
	}

	public boolean delete( int aid ) throws Exception {

		sqlStr = "delete from My_book where id = "  + aid ;
		try
		{
			stmt.execute(sqlStr);
			return true;
		}
		catch (SQLException e)
		{
			System.out.println(e);
			return false;
		}
	}

	public boolean getOnebook(int newid ) throws Exception {
		try
		{
			sqlStr="select  a.id,a.bookname,a.bookclass,b.classname,a.author,a.publish,a.bookno,a.content,a.prince from My_book a,My_bookclass b where a.Bookclass=b.Id and a.Id = " + newid ;
			rs = stmt.executeQuery(sqlStr);
			
			if (rs.next())
			{	booklist = new ArrayList<books>();
				books book = new books();
				book.setId(rs.getInt("id"));
				book.setBookName(rs.getString("bookname"));
				book.setBookClass(rs.getInt("bookclass"));
				book.setClassname(rs.getString("classname"));
				book.setAuthor(rs.getString("author"));
				book.setPublish(rs.getString("publish"));
				book.setBookNo(rs.getString("bookno"));
				book.setContent(rs.getString("content"));
				book.setPrince(rs.getFloat("prince"));
				
				
				booklist.add(book);
			} else {
				rs.close();
				return false;
			}
			rs.close();
			return true;
		}
		catch (SQLException e)
		{
			System.out.println(e);
			return false;
		}
		
	}

	public int getPage() {				//显示的页码
		return page;
	}
	public void setPage(int newpage) {
		page = newpage;
	}

	public int getPageSize(){			//每页显示的图书数
		return pageSize;
	}
	public void setPageSize(int newpsize) {
		pageSize = newpsize;
	}

	public int getPageCount() {				//页面总数
		return pageCount;
	}
	public void setPageCount(int newpcount) {
		pageCount = newpcount;
	}

	public int getRecordCount() {
		return recordCount;
	}
	public void setRecordCount(int newrcount) {
		recordCount= newrcount;
	}


};

⌨️ 快捷键说明

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