web实验四

所花时间:2小时

代码量:如下

博客量:本学期截至目前76篇

了解到的知识点:web实验

BOOK.java:

package book.bean;

import java.sql.*;
import java.util.*;

/**
 * 构造BookInfo对象,对应数据库的BookInfo表 提供了多个静态方法完成BookInfo对象与对应数据库表的所有新增、查询、修改、删除等操作
 *
 * @author Leiyu
 * @version 1.0
 *
 */
public class Book {

    private String id;
    private String num;
    private String name;
    private String sex;
    private String bir;

    public String getNum() {
        return num;
    }

    public void setNum(String num) {
        this.num = num;
    }
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getBir() {
        return bir;
    }

    public void setBir(String bir) {
        this.bir = bir;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getId() {
        return id;
    }

    /**
     * 从BookInfo表中获取所有的图书信息
     *
     * @return BookInfo的数组
     */
    public static ArrayList<Book> getBookList() {
        ArrayList<Book> list = new ArrayList<Book>();
        String sql = "select * from bookinfo";
        Dao jdbc = new Dao();
        ResultSet rs = jdbc.executeQuery(sql);
        try {
            while (rs.next()) {
                Book bi = new Book();
                bi.setId(rs.getString("id"));
                bi.setNum(rs.getString("num"));
                bi.setName(rs.getString("name"));
                bi.setSex(rs.getString("sex"));
                bi.setBir(rs.getString("bir"));
                list.add(bi);
            }
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        jdbc.close();
        return list;
    }

    /**
     * 获取指定id的图书信息
     *
     * @param id 图书id
     * @return 一个BookInfo对象
     */
    public static Book getBookById(String id) {
        String sql = "select * from bookinfo where id=" + id;
        Dao jdbc = new Dao();
        ResultSet rs = jdbc.executeQuery(sql);
        Book bi = new Book();
        try {
            if (rs.next()) {
                bi.setId(rs.getString("id"));
                bi.setNum(rs.getString("num"));
                bi.setName(rs.getString("name"));
                bi.setSex(rs.getString("sex"));
                bi.setBir(rs.getString("bir"));
            }
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        jdbc.close();
        return bi;
    }

    /**
     * 更新指定id的图书信息
     *
     * @param bi       要更新的图书的对象
     * @return 修改的结果:1代表成功,0代表没有更新
     */
    public static int updateBook(Book bi) {
        int result = 0;
        String sql = "update bookinfo set num='" + bi.getNum() + "',name='" + bi.getName() + "',sex='" + bi.getSex() + "',bir='"
                + bi.getBir() + "' where id=" + bi.getId();
        System.out.println(sql);
        Dao jdbc = new Dao();
        result = jdbc.executeUpdate(sql);
        jdbc.close();
        return result;
    }

    /**
     * 删除指定id的图书
     *
     * @param id 图书id
     * @return 删除的结果:1代表成功,0代表没有删除
     */
    public static int deleteBook(String id) {
        int result = 0;
        String sql = "delete from bookinfo where id=" + id;
        Dao jdbc = new Dao();
        result = jdbc.executeUpdate(sql);
        jdbc.close();
        return result;
    }

    /**
     * 增加一本图书
     *
     * @param bi 图书对象
     * @return 新增的结果:1代表成功,0代表没有增加
     */
    public static int addBook(Book bi) {
        int result = 0;
        String sql = "insert into bookinfo(num,name,sex,bir) values('" + bi.getNum() + "','" + bi.getName() + "','" + bi.getSex() + "','"
                + bi.getBir() + "')";
        Dao jdbc = new Dao();
        System.out.println(sql);
        result = jdbc.executeUpdate(sql);
        jdbc.close();
        return result;
    }
}

  Dao.java:

package book.bean;
import java.sql.*;
/**
 * 完成与数据库的连接和数据的访问
 * @author Leiyu
 * @version 1.0
 *
 */
public class Dao {
    private String driverStr = "com.mysql.cj.jdbc.Driver";
    private String connStr = "jdbc:mysql://127.0.0.1:3306/book?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";
    private String dbusername = "root";
    private String dbpassword = "Wjtssywds123";
    private Connection conn = null;
    private Statement stmt = null;
    public Dao() {
        try {
            Class.forName(driverStr);
            conn = DriverManager.getConnection(connStr, dbusername, dbpassword);
            stmt = conn.createStatement();
        } catch (Exception ex) {
            System.out.println("数据库连接失败!");
        }
    }

    /**
     * 执行更新操作
     * @param s
     * SQL语句
     * @return
     * 更新操作的结果
     */
    public int executeUpdate(String s) {
        int result = 0;
        try {
            result = stmt.executeUpdate(s);
        } catch (Exception ex) {
            System.out.println("更新出现异常!");
        }
        return result;
    }

    /**
     * 执行查询操作
     * @param s
     * SQL语句
     * @return
     * 查询结果
     */
    public ResultSet executeQuery(String s) {
        ResultSet rs = null;
        try {
            rs = stmt.executeQuery(s);
        } catch (Exception ex) {
            System.out.println("查询出现异常!");
        }
        return rs;
    }

    /**
     * 关闭数据库
     */
    public void close() {
        try {
            stmt.close();
            conn.close();
        } catch (Exception e) {
        }
    }
}

  index.html:

<html>
<head>
<title>学生管理系统</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="css/style.css">
<link rel="stylesheet" type="text/css" href="css/jquery-ui.css">
<script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="js/jquery-ui.js"></script>
<script type="text/javascript">
	//通过AJAX方式检索所有的图书
	function RetrieveBooks() {
		$.post("list.action", {}, function(data) {
			$("#BooksTable tr:gt(0)").remove();	
			for (var i = 0; i < data.length; i++) {
				//插入表行
				var trHtml = "<tr id = "+data[i].id +"><td>" + data[i].num + "</td><td>" + data[i].name + "</td><td>"
						+ data[i].sex + "</td><td>" + data[i].bir
						+ "</td><td><a href=\"#\" class=\"updateLink\">修改</a> <a href=\"#\" class=\"deleteLink\">删除</a></td></tr>";
				$("#BooksTable").append(trHtml);
			}
			//绑定修改链接
			$(".updateLink").click(function() {
				$.post("edit.action", {id:$(this).closest("tr").attr("id")}, function(data){
					$("#UpdateId").val(data.id);
					$("#UpdateNum").val(data.num);
					$("#UpdateName").val(data.name);
					$("#UpdateSex").val(data.sex);
					$("#UpdateBir").val(data.bir);
					$("#UpdateDiv").dialog("open");
				}, "json");
			});
			//绑定删除链接
			$(".deleteLink").click(function(){
				$.post("delete.action", {id:$(this).closest("tr").attr("id")}, function(data){
					if (data=="1") {
						RetrieveBooks();
					} else
					{
						alert("删除学生信息失败!");
					}
				}, "json");
			});
		}, "json");
	}
	$(function() {
		//设定Ajax提交编码格式为utf-8
		$.ajaxSetup({
			contentType: "application/x-www-form-urlencoded; charset=utf-8"
		});
		//对“添加图书信息”窗口进行初始化
		$("#AddDiv").dialog({
			title: "添加学生信息",
			autoOpen : false,
			height : 280,
			width : 400,
			modal : true,
			show: "blind", 
			hide: "fade",
			close : function(){
				$("#AddNum").val("");
				$("#AddName").val("");
				$("#AddSex").val("");
				$("#AddBir").val("");
			}
		});
		//对“修改图书信息”窗口进行初始化
		$("#UpdateDiv").dialog({
			title: "修改学生信息",
			autoOpen : false,
			height : 280,
			width : 400,
			modal : true,
			show: "blind", 
			hide: "fade",
			close : function(){
				 $("#UpdateId").val("");
				$("#UpdateNum").val("");
				$("#UpdateName").val("");
				$("#UpdateSex").val("");
				$("#UpdateBir").val("");
			}
		});
		//对添加图书窗口的添加键绑定事件驱动程序
		$("#AddSubmit").click(function(){
			//提交服务器
			$.post("add.action", {num:$("#AddNum").val(), name:$("#AddName").val(), sex:$("#AddSex").val(), bir:$("#AddBir").val()}, function(data){
				if (data=="1") {
					$("#AddDiv").dialog("close");
					RetrieveBooks();
				} else
				{
					$("#AddTip").html("添加学生信息失败!请重新输入数据。");
					$("#AddTip").show().delay(5000).hide(0);
				}
			}, "json");
		});
		//对添加图书窗口的添加键绑定事件驱动程序
		$("#UpdateSubmit").click(function(){
			//提交服务器
			// id:$("#UpdateId").val(),
			$.post("update.action", {id:$("#UpdateId").val(),num:$("#UpdateNum").val(),name:$("#UpdateName").val(), sex:$("#UpdateSex").val(),bir:$("#UpdateBir").val()}, function(data){
				if (data=="1") {
					$("#UpdateDiv").dialog("close");
					RetrieveBooks();
				} else
				{
					$("#UpdateTip").html("更新学生信息失败!请重新输入数据。");
					$("#UpdateTip").show().delay(5000).hide(0);
				}
			}, "json");
		});
		//对“新增图书信息”链接绑定事件驱动程序
		$("#AddButton").click(function() {
			$("#AddDiv").dialog("open");
		});	
		//第一次加载检索所有书籍
		RetrieveBooks();
	});
</script>
</head>
<body>
	<h1>学生管理系统</h1>
	<a id="AddButton" href="#">增加学生信息</a>
	<table style="width: 50%" id="BooksTable">
		<tr>
			<th>学号</th>
			<th>姓名</th>
			<th>性别</th>
			<th>生日</th>
			<th>管理</th>
		</tr>
	</table>
	<div id="AddDiv" style="display: hidden">
		<form id="AddForm">
			<table style="width: 350px;" id="AddTable">
				<tr>
					<th width="30%">学号:</th>
					<td width="70%" class="ltd"><input name="num" type="text" id="AddNum"></td>
				</tr>
				<tr>
					<th>姓名:</th>
					<td class="ltd"><input name="name" type="text" id="AddName"></td>
				</tr>
				<tr>
					<th>性别:</th>
					<td class="ltd"><input name="sex" type="text" id="AddSex"></td>
				</tr>
				<tr>
					<th>生日:</th>
					<td class="ltd"><input name="bir" type="date" id="AddBir"></td>
				</tr>

				<tr>
					<th colspan="2"><input type="button" value="添加" id ="AddSubmit"> <input
						type="reset" value="重置"></th>
				</tr>
			</table>
		</form>
		<span style="color:red;" id="AddTip"></span>
	</div>
	<div id="UpdateDiv" style="display: hidden">
		<form id="UpdateForm">
			<table style="width: 350px;" id="UpdateTable">
				<tr>
					<th width="30%">学号:</th>
					<td width="70%" class="ltd">
						<input name="id" type="hidden" id="UpdateId">
						<input name="num" type="text" id="UpdateNum"></td>
				</tr>
				<tr>
					<th>姓名:</th>
					<td class="ltd"><input name="name" type="text" id="UpdateName"></td>
				</tr>
				<tr>
					<th>性别:</th>
					<td class="ltd"><input name="sex" type="text" id="UpdateSex"></td>
				</tr>
				<tr>
					<th>生日:</th>
					<td class="ltd"><input name="bir" type="date" id="UpdateBir"></td>
				</tr>
				<tr>
					<th colspan="2"><input type="button" value="修改" id ="UpdateSubmit"> <input
						type="reset" value="重置"></th>
				</tr>
			</table>
		</form>
		<span style="color:red;" id="UpdateTip"></span>
	</div>
	<br />
	<hr />
	<div style="text-align: center; width: 100%; font-size: 12px; color: #333;">
		©版权所有:石家庄铁道大学信息科学与技术学院  <a href="Lab04-2.png"
			target="_blank">网站地图</a>
	</div>
</body>
</html>

  index.jsp:

<%@ page pageEncoding="utf-8" import="java.util.ArrayList,book.bean.Book"%>
<html>
<head>
<title>学生管理系统</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
	<h1>学生管理系统</h1>
	<a href="addview.do">增加学生信息</a>
	<p />
	<table style="width: 50%">
		<tr>
			<th>学号</th>
			<th>姓名</th>
			<th>性别</th>
			<th>生日</th>
			<th>管理</th>
		</tr>
		<%
		@SuppressWarnings("unchecked")
			ArrayList<Book> list = (ArrayList<Book>) request.getAttribute("list");
			for (Book bi : list) {
				String id = bi.getId();
		%>
		<tr>
			<td><%=bi.getNum()%></td>
			<td><%=bi.getName()%></td>
			<td><%=bi.getSex()%></td>
			<td><%=bi.getBir()%></td>
			<td><a href="edit.do?id=<%=id%>">修改</a> <a
				href="delete.do?id=<%=id%>">删除</a></td>
		</tr>
		<%
			}
		%>
	</table>
	<br />
	<hr />
	<div style="text-align: center; width: 100%; font-size: 12px; color: #333;">
		©版权所有:石家庄铁道大学信息科学与技术学院  <a href="Lab04-1.png"
			target="_blank">网站地图</a>
	</div>
</body>
</html>

  

posted @ 2023-05-19 21:24  南北啊  阅读(29)  评论(0)    收藏  举报
1 2 3
4