图书管理系统可行性研究
2011级软件工程一班
李 玲
登录代码:
package com.li.servlet;
import java.io.IOException; import java.io.PrintWriter; import java.sql.ResultSet; import java.sql.Statement;
import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession;
import com.han.beans.Person;
import com.han.databases.Connect;
public class Login extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); }
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try{ String name=request.getParameter(\"name\"); String psw=request.getParameter(\"password\"); Connect db=new Connect();//创建数据库连接对象 Statement sql=db.getSql(); String str=\"Select * from np where name='\"+ name+\"' and password='\" +psw+\"'\";//SQL语句 ResultSet rs=sql.executeQuery(str); HttpSession session=request.getSession(); if(rs.next()) { Person p=(Person)session.getAttribute(\"p\"); if(p==null){
p=new Person(); session.setAttribute(\"p\ } p.setId(rs.getInt(\"id\")); p.setName(rs.getString(\"name\")); p.setLevel(rs.getInt(\"level\")); p.setPsw(rs.getString(\"password\")); p.setFlag(true); p.setBn(rs.getInt(\"borrownum\")); //RequestDispatcher
rd=request.getRequestDispatcher(\"admin/index.jsp\"); //rd.forward(request,response); response.sendRedirect(\"index.jsp\"); } else response.sendRedirect(\"register.jsp\"); }catch(Exception e) { } } }
注册代码:
package com.li.servlet;
import java.io.IOException; import java.io.PrintWriter;
import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement;
import javax.servlet.ServletException; import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession;
import com.han.databases.Connect;
public class Register extends HttpServlet {
/**
* The doGet method of the servlet.
*
* This method is called when a form has its tag value method equals to get. *
* @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); }
/**
* The doPost method of the servlet.
*
* This method is called when a form has its tag value method equals to post. *
* @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String name=request.getParameter(\"name\"); String psw=request.getParameter(\"password\"); try{ Connect db=new Connect();//创建数据库连接对象 Statement st=db.getSql(); String sql=\"insert into np (name,password,level) values('\"+name+\"','\"+psw+\"',2)\"; if(!(name.equals(\"\")||(psw.equals(\"\")))){ st.executeUpdate(sql); response.sendRedirect(\"registerResult.jsp\");} // String str2=\"Select * from np where name='\"+ // name+\"' and password='\" +psw+\"'\"; // rs=sql.executeQuery(str2); // if(rs.next()){
// response.sendRedirect(\"registerResult.jsp\"); // }else{ // response.sendRedirect(\"register.jsp\"); // } // else{ response.sendRedirect(\"error.jsp\"); } }catch(Exception e){ System.out.print(e); response.sendRedirect(\"register.jsp\"); } } }
图书查询代码:
package com.han.servlet;
import java.io.IOException; import java.io.PrintWriter; import java.sql.ResultSet; import java.sql.Statement;
import javax.servlet.ServletException; import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession;
import com.han.beans.BorrowBook; import com.han.beans.Person;
import com.han.databases.Connect;
public class MyQuery extends HttpServlet {
/**
* The doGet method of the servlet.
*
* This method is called when a form has its tag value method equals to get. *
* @param request the request send by the client to the server * @param response the response send by the server to the client
* @throws ServletException if an error occurred * @throws IOException if an error occurred */
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); }
/**
* The doPost method of the servlet.
*
* This method is called when a form has its tag value method equals to post. *
* @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { BorrowBook borrowMessage=new BorrowBook(); HttpSession session=request.getSession(); Person p=(Person) session.getAttribute(\"p\"); int pid=p.getId();//人的id号 String pname=p.getName();//人的名字 borrowMessage.setpId(pid); borrowMessage.setMyBorrowQuery(\"
书的id号 | 借书日期 | 截止日期 | 是否还书 | 超过截止日期 |
---|---|---|---|---|
\"+Integer.toString(rs.getInt(\"bid\"))+\" | \"+rs.getString(\"borrowdate\")+\" | \"+\"\"+rs.getString(\"enddate\")+\" | \"); borrowMessage.setMyBorrowQuery(\"\"); borrowMessage.setMyBorrowQuery(\" | \"); borrowMessage.setMyBorrowQuery(\" |
用户借书代码:
package com.li.servlet;
import java.io.IOException; import java.io.PrintWriter; import java.sql.Date; import java.sql.ResultSet; import java.sql.Statement; import java.text.DateFormat;
import java.text.SimpleDateFormat; import java.util.Calendar;
import javax.servlet.ServletException; import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import javax.xml.ws.Response;
import sun.java2d.pipe.SpanShapeRenderer.Simple;
import com.han.beans.Book; import com.han.beans.Person;
import com.han.databases.Connect;
public class Borrow extends HttpServlet {
/**
* The doGet method of the servlet.
*
* This method is called when a form has its tag value method equals to get. *
* @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); }
/**
* The doPost method of the servlet.
*
* This method is called when a form has its tag value method equals to post. *
* @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String bookid1=request.getParameter(\"bookid\");//想要借的书的id号 int bookid=Integer.parseInt(bookid1); Book book=new Book();
book.setBookid(bookid); HttpSession session=request.getSession(); Person p=(Person)session.getAttribute(\"p\"); int pid=p.getId();//借书人的id号 //String pname=borrowP.getName();//借书人的姓名 try{ Connect db=new Connect();//创建数据库连接对象 Statement sql=db.getSql(); String str=\"update np set borrownum=borrownum+1 where id=\"+pid;//更改np中借书数 sql.execute(str); p.setBn(p.getBn()+1); session.setAttribute(\"p\ String str1=\"update book set remain=remain-1 where bookid=\"+bookid;//更改book中书的剩余数量 sql.execute(str1); String str3=\"select * from book where bookid=\"+bookid; ResultSet rs=sql.executeQuery(str3); if(rs.next()){ book.setBookName(rs.getString(\"bookname\")); book.setBookPrice(rs.getDouble(\"bookprice\")); book.setBookType(rs.getString(\"booktype\")); book.setBookWriter(rs.getString(\"bookwriter\")); book.setRemain(rs.getInt(\"remain\")); book.setUrl(rs.getString(\"url\")); book.setValue(rs.getInt(\"value\")); session.setAttribute(\"book\ } //添加该用户借书信息到borrowbook表中??为甚么calendar获取的当前时间不对呢??、 Date borrowdate=new Date(System.currentTimeMillis());//当前时间 String bdate=borrowdate.toString(); String enddate; SimpleDateFormat sdf=new SimpleDateFormat(\"yyyy-MM-ss HH:mm:ss\"); Calendar nowtime=Calendar.getInstance(); nowtime.setTime(borrowdate); nowtime.add(Calendar.MONTH,+1);//月份加一 enddate=sdf.format(nowtime.getTime()); String str4=\"insert into borrowbook (pid,bid,borrowdate,enddate) values(\"+pid+\ sql.execute(str4);
response.sendRedirect(\"borrowBook.jsp\"); }catch(Exception e) { response.sendRedirect(\"index.jsp\"); } } }
用户还书代码:package com.li.servlet;
import java.io.IOException; import java.io.PrintWriter; import java.sql.ResultSet; import java.sql.Statement;
import javax.servlet.ServletException; import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession;
import com.han.beans.Person;
import com.han.databases.Connect;
public class Return extends HttpServlet {
/**
* The doGet method of the servlet.
*
* This method is called when a form has its tag value method equals to get. *
* @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); }
/**
* The doPost method of the servlet.
*
* This method is called when a form has its tag value method equals to post. *
* @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int bookid=Integer.parseInt(request.getParameter(\"bookid\"));//要还的书的id号 int pid=Integer.parseInt(request.getParameter(\"pid\"));//还书人的id号 HttpSession session=request.getSession(); Person p=(Person) session.getAttribute(\"p\"); try{ Connect db=new Connect();//创建数据库连接对象 Statement sql=db.getSql(); String str=\"delete from borrowbook where pid=\"+pid+\" and bid=\"+bookid;//从借书表中删除此条借书记录 sql.execute(str); String str1=\"update np set borrownum=borrownum-1 where id=\"+pid;//将人的借书数目减一 sql.execute(str1); p.setBn(p.getBn()-1); String str2=\"update book set remain=remain+1 where bookid=\"+bookid;//将book表中此书的剩余数量加一 sql.execute(str2); response.sendRedirect(\"returnsuccess.jsp\"); //ResultSet rs=sql.executeQuery(str); }catch(Exception e){ response.sendRedirect(\"error.jsp\"); } } }
用户管理代码:
package com.li.control;
import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException;
import javax.servlet.RequestDispatcher; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
import com.li.data.Register;
public class addUser extends HttpServlet {
private static final String String = null;
public String handleString(String s){ try{ byte bb[]=s.getBytes(\"ISO-8859-1\"); s=new String(bb,\"gbk\"); } catch(Exception ee){ } return s; }
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); }
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Connection con; PreparedStatement sql; Register reg=new Register(); request.setAttribute(\"register\ String username=request.getParameter(\"username\").trim(); String password =request.getParameter(\"password\").trim();
String querenps =request.getParameter(\"querenps\").trim(); String sex=request.getParameter(\"sex\").trim(); String phone=request.getParameter(\"phone\").trim(); String email=request.getParameter(\"email\").trim(); String address=request.getParameter(\"address\").trim(); if(username==null) username=\"\"; if(password==null) password=\"\"; boolean isLD=true; boolean boo=username.length()>0&&password.length()>0&&isLD; String backnews=\"\"; try{
con=DriverManager.getConnection(\"jdbc:mysql://127.0.0.1:3306/blog\oot\"); String insertCondition = \"INSERT INTO user VALUES(?,?,?,?,?,?,?)\"; sql=con.prepareStatement(insertCondition); if(boo){ sql.setString(1, handleString(username)); sql.setString(2, handleString(password)); sql.setString(3, handleString(querenps)); sql.setString(4, handleString(sex)); sql.setString(5, handleString(phone)); sql.setString(6, handleString(email)); sql.setString(7, handleString(address)); int m=sql.executeUpdate(); if(m!=0){ backnews=\"注册成功\"; reg.setBacknews(backnews); reg.setUsername(username); reg.setPassword(handleString(password)); reg.setQuerenps(handleString(querenps)); reg.setSex(handleString(sex)); reg.setPhone(phone); reg.setEmail(handleString(email)); reg.setAddress(handleString(address)); } } else{ backnews=\"信息填写不完整或名字中有非法字符\"; reg.setBacknews(backnews); }
con.close(); } catch(SQLException exp){ backnews=\"该会员名已被使用,请您更换名字\"+exp; reg.setBacknews(backnews); } RequestDispatcher
dispatcher=request.getRequestDispatcher(\"showadduser.jsp\"); dispatcher.forward(request,response); }
public void init(ServletConfig config) throws ServletException { super.init(config); try{ Class.forName(\"com.mysql.jdbc.Driver\"); } catch(Exception e){ } } }
因篇幅问题不能全部显示,请点此查看更多更全内容