100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > 图示管理系统(还书 续借 预约功能)jsp+JavaBean+JDBC+servlet+Dao

图示管理系统(还书 续借 预约功能)jsp+JavaBean+JDBC+servlet+Dao

时间:2024-05-06 15:53:02

相关推荐

图示管理系统(还书 续借 预约功能)jsp+JavaBean+JDBC+servlet+Dao

先在MySql中建一个数据库其中包含三个表,一个book表,一个user表,一个info表

jsp界面只输入图书号,按图书号还书,续借,预约

JavaBean中写了book类,info类,user类,数据库连接类,还有一个时间类

book类

package bean;

public class Book {

private String id;

private String name;

private String author;

private double price;

private String info;

public Book(String id, String name, String author, double price, String info) {

this.id = id;

this.name = name;

this.author = author;

this.price = price;

this.info = info;

}

public Book() {}

public String getId() {

return id;

}

public void setId(String id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getAuthor() {

return author;

}

public void setAuthor(String author) {

this.author = author;

}

public double getPrice() {

return price;

}

public void setPrice(double price) {

this.price = price;

}

public String getInfo() {

return info;

}

public void setInfo(String info) {

this.info = info;

}

}

info类

package bean;

public class Info {

private String id;

private String name;

private String uname;

private String type;

private String lendate;

private String huanqi;

private String cq;

private String info;

private String appoint;

public Info() {}

public Info(String id, String name, String uname, String type, String lendate, String huanqi, String cq,

String info, String appoint) {

this.id = id;

this.name = name;

this.uname = uname;

this.type = type;

this.lendate = lendate;

this.huanqi = huanqi;

this.cq = cq;

this.info = info;

this.appoint = appoint;

}

public String getId() {

return id;

}

public void setId(String id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getUname() {

return uname;

}

public void setUname(String uname) {

this.uname = uname;

}

public String getType() {

return type;

}

public void setType(String type) {

this.type = type;

}

public String getLendate() {

return lendate;

}

public void setLendate(String lendate) {

this.lendate = lendate;

}

public String getHuanqi() {

return huanqi;

}

public void setHuanqi(String huanqi) {

this.huanqi = huanqi;

}

public String getCq() {

return cq;

}

public void setCq(String cq) {

this.cq = cq;

}

public String getInfo() {

return info;

}

public void setInfo(String info) {

this.info = info;

}

public String getAppoint() {

return appoint;

}

public void setAppoint(String appoint) {

this.appoint = appoint;

}

}

user类

package bean;

public class User {

private String uname;

private String password;

private String info;

public User() {}

public User(String uname, String password, String info) {

this.uname = uname;

this.password = password;

this.info = info;

}

public String getUname() {

return uname;

}

public void setUname(String uname) {

this.uname = uname;

}

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

public String getInfo() {

return info;

}

public void setInfo(String info) {

this.info = info;

}

}

数据库连接类(可作为模板,任何使用JDBC都可以建此类,调用即可)

package bean;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

public class SJKLJ {

private static String driverName="com.mysql.jdbc.Driver";

private static String userName="root";

private static String userPwd="root";

private static String dbName="bookmanage";

public static Connection getDBconnection() {

String url1="jdbc:mysql://localhost:3306/"+dbName;

String url2="?user="+userName+"&password="+userPwd;

String url3="&useUnicode=true&characterEncoding=GB2312";

String url=url1+url2+url3;

try {

Class.forName(driverName);

Connection conn=DriverManager.getConnection(url);

return conn;

} catch (Exception e) {

e.printStackTrace();

}

return null;

}

public static void closeB(Connection conn,PreparedStatement pstmt,ResultSet rs) {

try {

if(rs!=null)rs.close();

if(pstmt!=null) pstmt.close();

if(conn!=null) conn.close();

} catch (Exception e) {

e.printStackTrace();

}

}

}

时间类(其中包含借书,续借,和超期的时间,我只用到了续借)

package bean;

import java.text.SimpleDateFormat;

import java.util.*;

public class Date_d {

private String now;

public String now_time() {

// SimpleDateFormat format = new SimpleDateFormat("yyyy-mm-dd");

Calendar calendar = Calendar.getInstance();

int cur_month=calendar.get(Calendar.MONTH)+1;

int cur_year=calendar.get(Calendar.YEAR);

int cur_day=calendar.get(Calendar.DAY_OF_MONTH);

now = cur_year+"-"+cur_month+"-"+cur_day;

return now;

}

public String huanq() {

Calendar calendar = Calendar.getInstance();

int cur_month=calendar.get(Calendar.MONTH)+2;

int cur_year=calendar.get(Calendar.YEAR);

int cur_day=calendar.get(Calendar.DAY_OF_MONTH);

now = cur_year+"-"+cur_month+"-"+cur_day;

return now;

}

public String Xujie() {

Calendar calendar = Calendar.getInstance();

int cur_month=calendar.get(Calendar.MONTH)+3;

int cur_year=calendar.get(Calendar.YEAR);

int cur_day=calendar.get(Calendar.DAY_OF_MONTH);

now = cur_year+"-"+cur_month+"-"+cur_day;

return now;

}

@SuppressWarnings("unused")

public long chaoq(String bl) throws Exception {

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

//开始时间

Date startDate = sdf.parse(bl);

//结束时间

Date endDate = sdf.parse(this.now_time());///

//得到相差的天数 betweenDate

long betweenDate = (endDate.getTime() - startDate.getTime())/(60*60*24*1000);

return betweenDate;

}

}

servlet类,我只写了查询id,三个功能的servlet是一样的,只是跳转到的jsp名不一样

Dao中写了,查找id,还书时(更新book的info为可借,更新info表的info为已还),续借时(只需将info表的huanqi加一个月即可),预约时(更新book表的info为不可预约,更新info表的appoint的信息为已被预约)

package Dao;

import java.sql.*;

import java.util.ArrayList;

import java.util.List;

import bean.Book;

import bean.Date_d;

import bean.Info;

import bean.SJKLJ;

public class Dao {

public List<Info> select(String id) throws Exception{

String sql="select id,name,uname,type,lendate,huanqi,cq,info,appoint from info where id="+id;

Connection conn=null;

PreparedStatement pstmt=null;

ResultSet rs=null;

List<Info> list=new ArrayList<Info>();

conn=SJKLJ.getDBconnection();

pstmt=conn.prepareStatement(sql);

rs=pstmt.executeQuery();

rs.last();

if(rs.getRow()!=0) {

rs.beforeFirst();

while(rs.next()) {

String id1=rs.getString("id");

String name=rs.getString("name");

String uname=rs.getString("uname");

String type=rs.getString("type");

String lendate=rs.getString("lendate");

String huanqi=rs.getString("huanqi");

String cq=rs.getString("cq");

String info=rs.getString("info");

String appoint=rs.getString("appoint");

Info in=new Info(id1,name,uname,type,lendate,huanqi,cq,info,appoint);

list.add(in);

}

}

SJKLJ.closeB(conn, pstmt, rs);

return list;

}

public int update_b(String id) throws Exception{

String sql="update book set info = '可借' where id = "+id;

Connection conn=null;

PreparedStatement pstmt=null;

int n=0;

@SuppressWarnings("unused")

String m=null;

conn=SJKLJ.getDBconnection();

pstmt=conn.prepareStatement(sql);

n=pstmt.executeUpdate();

SJKLJ.closeB(conn, pstmt, null);

return n;

}

public int update_i(String id) throws Exception{

String sql="update info set info ='已还' where id ="+id;

Connection conn=null;

PreparedStatement pstmt=null;

int n=0;

conn=SJKLJ.getDBconnection();

pstmt=conn.prepareStatement(sql);

n=pstmt.executeUpdate() ;

SJKLJ.closeB(conn, pstmt, null);

return n;

}

public int update_in(String id)throws Exception{

Date_d dd=new Date_d();

String now=dd.Xujie();

String sql="update info set huanqi='"+now+"' where id="+id;

Connection conn=null;

PreparedStatement pstmt=null;

int n=0;

conn=SJKLJ.getDBconnection();

pstmt=conn.prepareStatement(sql);

n=pstmt.executeUpdate() ;

SJKLJ.closeB(conn, pstmt, null);

return n;

}

public int update_bo(String id)throws Exception{

String sql="update book set info='不可预约' where id="+id;

Connection conn=null;

PreparedStatement pstmt=null;

int n=0;

conn=SJKLJ.getDBconnection();

pstmt=conn.prepareStatement(sql);

n=pstmt.executeUpdate() ;

SJKLJ.closeB(conn, pstmt, null);

return n;

}

public int update_inf(String id)throws Exception{

String sql="update info set appoint='已被预约' where id="+id;

Connection conn=null;

PreparedStatement pstmt=null;

int n=0;

conn=SJKLJ.getDBconnection();

pstmt=conn.prepareStatement(sql);

n=pstmt.executeUpdate() ;

SJKLJ.closeB(conn, pstmt, null);

return n;

}

public List<Book> select_b(String id)throws Exception{

String sql="select id,name,author,price,info from book where id="+id;

Connection conn=null;

PreparedStatement pstmt=null;

ResultSet rs=null;

conn=SJKLJ.getDBconnection();

pstmt=conn.prepareStatement(sql);

rs=pstmt.executeQuery();

rs.last();

List<Book> list=new ArrayList<Book>();

if(rs.getRow()!=0) {

rs.beforeFirst();

while(rs.next()) {

String id1=rs.getString("id");

String name=rs.getString("name");

String author=rs.getString("author");

Double price=rs.getDouble("price");

String info=rs.getString("info");

Book bb=new Book(id1,name,author,price,info);

list.add(bb);

}

}

SJKLJ.closeB(conn, pstmt, null);

return list;

}

}

最后的show.jsp中,还书和预约相似,续借更为简单

还书:

<%@ page language="java" import="bean.*" import="java.util.*" import="Dao.Dao"

import="java.sql.*" import="bean.SJKLJ" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>Insert title here</title>

</head>

<body>

<%

Dao d=new Dao();

//System.out.println("111");

@SuppressWarnings("unchecked")

List<Info> list=(List<Info>)session.getAttribute("list");

if(list.get(0).getInfo().equals("")){

//System.out.println("444");

//System.out.println(list.get(0).getId());

int nn=d.update_i(list.get(0).getId());

//System.out.println("222");

if(nn==1){

int mm=d.update_b(list.get(0).getId());

if(mm==1){

%>还书成功!<%

}else{

%>还书失败,出现异常错误!<%

}

}

}else{

%>您已经还过此书!<%

}

%>

</body>

</html>

续借:

<%@ page language="java" import="bean.*" import="Dao.Dao" import="java.util.*" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>Insert title here</title>

</head>

<body>

<%

Dao d=new Dao();

System.out.println("222");

@SuppressWarnings("unchecked")

List<Info> list=(List<Info>)session.getAttribute("list");

System.out.println("333");

int m=d.update_in(list.get(0).getId());

System.out.println("444");

if(m==1){

System.out.println("555");

//Date_d dd=new Date_d();

//String hq=dd.huanq();

%>续借成功!续借时间为30天,请按时还书!<%

}else{

%>续借失败!<%

}

%>

</body>

</html>

预约:

<%@ page language="java" import="bean.*" import="java.util.*" import="Dao.Dao"

import="java.sql.*" import="bean.SJKLJ" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>Insert title here</title>

</head>

<body>

<%

Dao d=new Dao();

@SuppressWarnings("unchecked")

List<Book> list=(List<Book>)session.getAttribute("list");

if(list.get(0).getInfo().equals("预约")){

//System.out.println("444");

//System.out.println(list.get(0).getId());

int nn=d.update_inf(list.get(0).getId());

//System.out.println("222");

if(nn==1){

%>预约成功!<%

}else{

%>出现异常错误!<%

}

}else{

%>预约失败!<%

}

%>

</body>

</html>

想要添加其他的功能只需在Dao里添加即可!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。