一:主界面
以下是代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
#t1 {
margin: auto;
text-align: center;
width: 600px;
height: 300px
}
table, tr, td {
border-collapse: collapse;
}
#inp {
text-align: center;
}
#p {
text-align: center;
}
#go {
border: none;
}
#de {
border: none;
}
</style>
</head>
<body>
<div id="inp">
<a href="add.jsp">新增</a>
</div>
<table border="" id="t1">
<form action="key.do">
<tr>
<td colspan="6">教员:<select name="teacher">
<c:forEach var="i" items="${ allT }">
<option>${ i.tname }</option>
</c:forEach>
</select> 班级:<select name="sclass">
<c:forEach var="i" items="${ allC }">
<option>${ i.sclazz }</option>
</c:forEach>
</select> 学生爱好:<input type="checkbox" name="ah" value="篮球">篮球 <input
type="checkbox" name="ah" value="唱歌">唱歌 <input
type="checkbox" name="ah" value="足球">足球 <input
type="checkbox" name="ah" value="跳舞">跳舞
<button id="but">查询</button>
</td>
</tr>
</form>
<tr>
<td>学生ID</td>
<td>学生姓名</td>
<td>学生教员</td>
<td>班级</td>
<td>学生爱好</td>
<td>操作</td>
</tr>
<c:forEach var="i" items="${ all }">
<tr>
<td>${ i.sid }</td>
<td>${ i.sname }</td>
<td>${ i.tname }</td>
<td>${ i.sclass }</td>
<td>${ i.sah }</td>
<td><button onclick="return del('${ i.sid }')" id="de">删除</button>
<a href="updshow.do?sid=${ i.sid }">修改</a></td>
</tr>
</c:forEach>
</table>
<div id="p">
第${ pages }页,共${ max }页,总记录${ m }条,<br>
<a href="index.do?pid=1">首页</a>,<a
href="index.do?pid=${ pages-1<1?1:pages-1 }">上一页</a>
<form action="index.do"></form>
<a href="index.do?pid=${ pages+1>max?max:pages+1 }">下一页</a>,<a
href="index.do?pid=${ max }">尾页</a>
</div>
<script>
//return del('${ i.sid }',1)
function del(id) {
var flag = window.confirm("您确定要删除吗?")
if (flag) {
window.location.href = "del.do?sid=" + id;
} else {
window.location.href = "index.do";
}
}
</script>
</body>
</html>
二:删除(根据id删除)
三:修改
修改成功跳回主界面
下面是代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style>
#t1 {
margin: auto;
text-align: center;
width: 600px;
height: 300px
}
table, tr, td {
border-collapse: collapse;
}
#inp {
text-align: center;
}
#p {
text-align: center;
}
#but {
background-color: orange;
}
#go {
border: none;
}
</style>
</head>
<body>
<form action="upd.do">
<table id="t1" border="">
<tr>
<td>姓名:<input name="sname" value="${ s.sname }"> <input
name="sid" value="${ s.sid }" type="hidden">
</td>
</tr>
<tr>
<td>教员: <select name="tname">
<c:forEach var="i" items="${ allT }">
<option value="${ i.tname }" ${ i.tname==s.tname?"selected":"" }>${ i.tname }</option>
</c:forEach>
</select>
</td>
</tr>
<tr>
<td>班级: <select name="sclazz">
<c:forEach var="i" items="${ allC }">
<option value="${ i.sclazz }"
${ i.sclazz==s.sclass?"selected":"" }>${ i.sclazz }</option>
</c:forEach>
</select>
</td>
</tr>
<tr>
<td>爱好: <input type="checkbox" name="ah" value="篮球"
<c:forEach var="i" items="${ split }">
<c:if test="${ i=='篮球' }"> checked
</c:if>
</c:forEach>>篮球
<input type="checkbox" name="ah" value="唱歌"
<c:forEach var="i" items="${ split }">
<c:if test="${ i=='唱歌' }"> checked
</c:if>
</c:forEach>>唱歌
<input type="checkbox" name="ah" value="足球"
<c:forEach var="i" items="${ split }">
<c:if test="${ i=='足球' }"> checked
</c:if>
</c:forEach>>足球
<input type="checkbox" name="ah" value="跳舞"
<c:forEach var="i" items="${ split }">
<c:if test="${ i=='跳舞' }"> checked
</c:if>
</c:forEach>>跳舞
</td>
</tr>
<tr>
<td colspan="3"><button>确定</button>
<button type="reset">重置</button></td>
</tr>
</table>
</form>
</body>
</html>
四:增加
增加成功跳回主界面
六:下面是所有dao方法
package com.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.dao.impl.IStudentDao;
import com.entity.clazz;
import com.entity.student;
import com.entity.teacher;
import com.util.DBHelper;
public class StudentDao implements IStudentDao {
/**
* 查询所有学生
*
* @return
*/
public List<student> getAll() {
List<student> list = new ArrayList<student>();
Connection con = DBHelper.getCon();
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = con.prepareStatement("select * from student");
rs = ps.executeQuery();
while (rs.next()) {
student s = new student();
s.setSid(rs.getInt(1));
s.setSname(rs.getString(2));
s.setSah(rs.getString(3));
s.setSclass(rs.getString(4));
s.setTname(rs.getString(5));
list.add(s);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBHelper.myClose(con, ps, rs);
}
return list;
}
/**
* 查询所有教室
*/
public List<teacher> getAllT() {
List<teacher> list = new ArrayList<teacher>();
Connection con = DBHelper.getCon();
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = con.prepareStatement("select * from teacher");
rs = ps.executeQuery();
while (rs.next()) {
teacher t = new teacher();
t.setTid(rs.getInt(1));
t.setTname(rs.getString(2));
list.add(t);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBHelper.myClose(con, ps, rs);
}
return list;
}
/**
* 查询所有班级
*/
public List<clazz> getAllC() {
List<clazz> list = new ArrayList<clazz>();
Connection con = DBHelper.getCon();
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = con.prepareStatement("select * from clazz");
rs = ps.executeQuery();
while (rs.next()) {
clazz c = new clazz();
c.setCid(rs.getInt(1));
c.setSclazz(rs.getString(2));
list.add(c);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBHelper.myClose(con, ps, rs);
}
return list;
}
public List<student> getpage(int pages, int pagesize) {
List<student> list = new ArrayList<student>();
Connection con = DBHelper.getCon();
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = con.prepareStatement(
"select * from (select *,row_number() over(order by sid) as rowid from student)as b where rowid between ? and ?");
ps.setInt(1, pages);
ps.setInt(2, pagesize);
rs = ps.executeQuery();
while (rs.next()) {
student s = new student();
s.setSid(rs.getInt(1));
s.setSname(rs.getString(2));
s.setSah(rs.getString(3));
s.setSclass(rs.getString(4));
s.setTname(rs.getString(5));
list.add(s);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBHelper.myClose(con, ps, rs);
}
return list;
}
public List<student> getpage(int pages, int pagesize, String key1, String key2, String key3) {
List<student> list = new ArrayList<student>();
Connection con = DBHelper.getCon();
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = con.prepareStatement(
"\r\n" + "select * from (select *,row_number() over(order by sid) as rowid from student\r\n"
+ "where sah like ? \r\n" + "and sclass like ? and tname like ?\r\n"
+ ")as b where rowid between ? and ?");
ps.setInt(4, pages);
ps.setInt(5, pagesize);
ps.setString(1, "%" + key1 + "%");
ps.setString(2, "%" + key2 + "%");
ps.setString(3, "%" + key3 + "%");
rs = ps.executeQuery();
while (rs.next()) {
student s = new student();
s.setSid(rs.getInt(1));
s.setSname(rs.getString(2));
s.setSah(rs.getString(3));
s.setSclass(rs.getString(4));
s.setTname(rs.getString(5));
list.add(s);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBHelper.myClose(con, ps, rs);
}
return list;
}
/**
* 删除
*
* @param sid
* @return
*/
public int del(int sid) {
Connection con = DBHelper.getCon();
PreparedStatement ps = null;
try {
ps = con.prepareStatement("delete from student where sid=?");
ps.setInt(1, sid);
return ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
DBHelper.myClose(con, ps, null);
}
return 0;
}
/**
* 增加
*
* @param s
* @return
*/
public int add(student s) {
Connection con = DBHelper.getCon();
PreparedStatement ps = null;
try {
ps = con.prepareStatement("insert into student values(?,?,?,?)");
ps.setString(1, s.getSname());
ps.setString(2, s.getSah());
ps.setString(3, s.getSclass());
ps.setString(4, s.getTname());
return ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
DBHelper.myClose(con, ps, null);
}
return 0;
}
/**
* 根据id查询
*
* @param sid
* @return
*/
public student byids(int sid) {
Connection con = DBHelper.getCon();
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = con.prepareStatement("select * from student where sid=?");
ps.setInt(1, sid);
rs = ps.executeQuery();
while (rs.next()) {
student s = new student();
s.setSid(rs.getInt(1));
s.setSname(rs.getString(2));
s.setSah(rs.getString(3));
s.setSclass(rs.getString(4));
s.setTname(rs.getString(5));
return s;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBHelper.myClose(con, ps, rs);
}
return null;
}
/**
* 修改
*
* @param s
* @return
*/
public int upd(student s) {
Connection con = DBHelper.getCon();
PreparedStatement ps = null;
try {
ps = con.prepareStatement("update student set sname=?,sah=?,sclass=?,tname=? where sid=?");
ps.setString(1, s.getSname());
ps.setString(2, s.getSah());
ps.setString(3, s.getSclass());
ps.setString(4, s.getTname());
ps.setInt(5, s.getSid());
return ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
DBHelper.myClose(con, ps, null);
}
return 0;
}
/**
* 模糊查询
*
* @param key1
* @param key2
* @param key3
* @return
*/
public List<student> getkey(String key1, String key2, String key3) {
List<student> list = new ArrayList<student>();
Connection con = DBHelper.getCon();
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = con.prepareStatement("select * from student where tname like ? and sclass like ? and sah like ?");
ps.setString(1, "%" + key1 + "%");
ps.setString(2, "%" + key2 + "%");
ps.setString(3, "%" + key3 + "%");
rs = ps.executeQuery();
while (rs.next()) {
student s = new student();
s.setSid(rs.getInt(1));
s.setSname(rs.getString(2));
s.setSah(rs.getString(3));
s.setSclass(rs.getString(4));
s.setTname(rs.getString(5));
list.add(s);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBHelper.myClose(con, ps, rs);
}
return list;
}
}
到这就结束了