1、内蒙古工业大学信息工程学院设计一个通用的基于浏览器/服务器的简单图书馆管理系统,实现用户的注册和登陆,以及图书的查询与借阅。1、 实验要求1) 利用JavaBean进行数据传递2) 用户的基本信息采用数据库存放3) 使用Sevlet进行数据验证八、程序清单Admin类 /管理员package library.info;public class Admin private String username;private String userkey;public Admin()username=null;userkey=null;public Admin(String username,Stri
2、ng userkey)this.username=username;this.userkey=userkey;public void setUsername(String username) this.username = username;public String getUsername() return username;public void setUserkey(String userkey) this.userkey = userkey;public String getUserkey() return userkey;public boolean equals(Object a)
3、if(a=null)return false;if(!(a instanceof Admin)return false;Admin temp=(Admin)a;if(username.equals(temp.username)&(userkey.equals(temp.userkey)return true;else return false;Book类 /图书package library.info;public class Book private String number;private String name;private String author;private String
4、chubanshe;private String jiage;private String shuliang;public Book()number=null;name=null;author=null;chubanshe=null;jiage=null;shuliang=null;public Book(String number,String name,String author,String chubanshe,String jiage,String shuliang)this.number=number;this.name=name;this.author=author;this.ch
5、ubanshe=chubanshe;this.jiage=jiage;this.shuliang=shuliang;public void setNumber(String number) this.number = number;public String getNumber() return number;public void setName(String name) this.name = name;public String getName() return name;public void setAuthor(String author) this.author = author;
6、public String getAuthor() return author;public void setChubanshe(String chubanshe) this.chubanshe = chubanshe;public String getChubanshe() return chubanshe;public void setJiage(String jiage) this.jiage = jiage;public String getJiage() return jiage;public void setShuliang(String shuliang) this.shulia
7、ng = shuliang;public String getShuliang() return shuliang;userlogin.jsp /登陆界面 登陆 !- 登录管理界面 管理员账号 管理员密码 UserLogin /servlet用户登录的处理package library.database;import java.io.IOException;import javax.servlet.RequestDispatcher;import javax.servlet.ServletException;impor
8、t javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import library.info.*;public class UserLogin extends HttpServlet /* * */private static final long serialVersionUID = 1L;public void destroy() super.destroy(); / Just puts destr
9、oy string in log/ Put your code herepublic void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException doPost(request,response);public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException String url = e
10、rror.jsp;response.setContentType(text/html);response.setCharacterEncoding(UTF-8);request.setCharacterEncoding(UTF-8);Admin user = new Admin();user.setUsername(request.getParameter(username).trim();user.setUserkey(request.getParameter(userkey).trim();if(user.getUsername().equals()|user.getUserkey().e
11、quals()request.setAttribute(message, 用户名或密码不能为空,请重新输入!);else if(AdminDatabase.CheckLogin(user)=3) url=index.jsp; request.setAttribute(username, user.getUsername();else if(AdminDatabase.CheckLogin(user)=2)request.setAttribute(message, 密码有误,请检查!);else if(AdminDatabase.CheckLogin(user)=1)request.setAtt
12、ribute(message, 用户名不存在,请重新输入!); RequestDispatcher rd=request.getRequestDispatcher(url);rd.forward(request, response);return;public void init() throws ServletException / Put your code hereAdminDatabase /处理登陆的数据库信息package library.database;import java.sql.Connection;import java.sql.DriverManager;import
13、 java.sql.SQLException;import java.sql.ResultSet;import java.sql.Statement;import library.info.*;public class AdminDatabase public static Connection getconnect()String DBDRIVER=com.microsoft.sqlserver.jdbc.SQLServerDriver;String DBURL=jdbc:sqlserver:/localhost:1433;databaseName=libraryinfo; Connecti
14、on con = null; try Class.forName(DBDRIVER); con = DriverManager.getConnection(DBURL,sa,zty); catch(ClassNotFoundException e)System.err.println(e.getMessage();System.exit(1);catch(SQLException e)System.err.println(e.getMessage();return con;public static int CheckLogin(Admin user)Admin temp = new Admi
15、n();Connection con=null; Statement stmt=null; ResultSet rs=null;boolean flag=false;int lab=1;trycon=AdminDatabase.getconnect(); stmt=con.createStatement(); rs=stmt.executeQuery(select * from admininfo where username =+user.getUsername().trim()+); while(rs.next() flag = true; temp.setUsername(rs.getS
16、tring(username).trim(); temp.setUserkey(rs.getString(userkey).trim(); if(user.equals(temp) lab=3; flag=false; break; if(flag)lab=2;catch(SQLException e)System.err.println(e.getMessage();if(rs!=null)try rs.close(); catch(SQLException e)Finally(con,stmt);return lab;public static void Finally(Connectio
17、n con,Statement stmt) if(stmt!=null) try stmt.close(); catch(SQLException e) if(con!=null) try con.close(); catch(SQLException e) error.jsp /处理错误信息 base href= 错误 !- 重新登录 success.jsp /处理正确信息 base href= 成功 !- 返回主页 index.jsp /主页面 base href= 欢迎登陆 !- 欢迎你:,使用图书管理系统 图书管理系统 书号查询: % ArrayList bookinfo
18、 = BookDatabase.showAllInfo(); if(bookinfo!=null&bookinfo.size()0) % 图书列表 书号 书名 作者 出版社 价格 数量 操作 % for(int i=0;i input type=hidden name=number value= Copyright zty 2012.6.11 BookDatabase类 /处理数据库的图书信息package library.database;import java.util.ArrayList;import java.sql.Connection;import java.sql.SQLExce
19、ption;import java.sql.ResultSet;import java.sql.PreparedStatement;import library.info.*;public class BookDatabase private static Connection con; private static PreparedStatement stmt;private static ResultSet rs;public static ArrayList showAllInfo()ArrayList bookinfo = new ArrayList();trycon=AdminDat
20、abase.getconnect();stmt=con.prepareStatement(select * from bookinfo );rs=stmt.executeQuery();while(rs.next()Book book = new Book();book.setNumber(rs.getString(number);book.setName(rs.getString(name);book.setAuthor(rs.getString(author);book.setChubanshe(rs.getString(chubanshe);book.setJiage(rs.getStr
21、ing(jiage);book.setShuliang(rs.getString(shuliang);bookinfo.add(book);catch(SQLException e)bookinfo=null;if(rs!=null)try rs.close(); catch(SQLException e)AdminDatabase.Finally(con,stmt);return bookinfo;public static boolean addInfo(Book book) boolean flag=false; try con=AdminDatabase.getconnect();st
22、mt=con.prepareStatement(insert into bookinfo +values(?,?,?,?,?,?);stmt.setString(1,book.getNumber();stmt.setString(2,book.getName();stmt.setString(3,book.getAuthor();stmt.setString(4,book.getChubanshe();stmt.setString(5,book.getJiage();stmt.setString(6,book.getShuliang(); if(stmt.executeUpdate()=1)f
23、lag=true;catch(SQLException e)System.out.println(e.getMessage();AdminDatabase.Finally(con,stmt);return flag; public static Book QueryInfo(String number)Book book = new Book(); ResultSet rs=null; trycon=AdminDatabase.getconnect();stmt=con.prepareStatement(select * from bookinfo where number= +number+
24、);rs=stmt.executeQuery();while(rs.next()book.setNumber(rs.getString(number);book.setName(rs.getString(name);book.setAuthor(rs.getString(author);book.setChubanshe(rs.getString(chubanshe);book.setJiage(rs.getString(jiage);book.setShuliang(rs.getString(shuliang);catch(SQLException e)book=null;if(rs!=nu
25、ll)try rs.close(); catch(SQLException e)AdminDatabase.Finally(con,stmt);return book;public static boolean deleteInfo(String number)boolean flag=false;try con=AdminDatabase.getconnect();stmt=con.prepareStatement(delete from bookinfo where number=+number+); if(stmt.executeUpdate()=1)flag=true;catch(SQLException e)System.out.println(e.getMessage();AdminDatabase.Finally(con,stmt);return flag;