前言:给大家讲解增删改查
?码字不易,点个关注
?转载请说明!
开发工具:eclipse? ? 数据库:SQL Server
?
?
1.数据库建表
三张表:学生(id,名字,班级,教员,爱好)
? ? ? ? ? ? ? ?班级(id,名字)
、? ? ? ? ? ?教员?(id,名字)
2.项目构造
?创建Web项目,导入架包,数据库帮助包,实体类,Servlet类,接口类,实现接口类(dao方法类)
?3.成果展示
1.主界面
主界面代码:
<%@ 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>主界面</title>
<style type="text/css">
table, tr {
border-collapse: collapse;
}
td {
text-align: center;
}
</style>
</head>
<body>
<table border="" width="1200" height="400px" align="center">
<tr>
<td colspan="6">
<form action="key.do">
教员: <select name="teacher" class="select">
<option value="null">--选择教员--</option>
<c:forEach var="t" items="${ allt }">
<option value="${ t.tname }" ${ t.tname==teacher?"selected":"" }>
${ t.tname }</option>
</c:forEach>
</select> 班级: <select name="class1" class="select">
<option value="null">--选择班级--</option>
<c:forEach var="c" items="${ allc }">
<option value="${ c.cname }" ${ c.cname==class1?"selected":""}>
${ c.cname }</option>
</c:forEach>
</select> 学生爱好: <input type="checkbox" name="shobby" value="篮球"
<c:forEach var="i" items="${ alls }">
<c:if test="${ i.shobby=='篮球' }"> checked</c:if></c:forEach>>篮球
<input type="checkbox" name="shobby" value="足球"
<c:forEach var="i" items="${ alls }">
<c:if test="${ i.shobby=='足球' }"> checked</c:if></c:forEach>>足球
<input type="checkbox" name="shobby" value="唱歌"
<c:forEach var="i" items="${ alls }">
<c:if test="${ i.shobby=='唱歌' }"> checked</c:if></c:forEach>>唱歌
<input type="checkbox" name="shobby" value="跳舞"
<c:forEach var="i" items="${ alls }">
<c:if test="${ i.shobby=='跳舞' }"> checked</c:if></c:forEach>>跳舞
<button>查询</button>
</form>
<button onclick="location.href='Add.jsp'">增加学生</button>
</td>
</tr>
<tr>
<td>学生id</td>
<td>学生名字</td>
<td>学生班级</td>
<td>学生教员</td>
<td>学生爱好</td>
<td>操作</td>
</tr>
<c:forEach var="i" items="${alls }">
<tr>
<td>${ i.sid }</td>
<td>${ i.sname }</td>
<td>${ i.sclass }</td>
<td>${ i.steacher }</td>
<td>${ i.shobby }</td>
<td><a href="get.do?ssid=${ i.sid }">修改</a> <a
href="del.do?ssid=${i.sid }">删除</a></td>
</tr>
</c:forEach>
</table>
</body>
</html>
?
2.模糊查询
?
?
?3.增加界面
?
增加成功后,界面弹出增加成功?
?
?
?
3 修改界面(要有原有数据)
?
修改完成弹出修改成功?
?
?
?
?dao方法代码:
package com.sdao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.entity.Student;
import com.student.impl.IStudentDao;
import com.util.DBHerpre;
/**
* 学生数据库操作类
* @author
*
*/
public class StudentDao implements IStudentDao {
private Connection con;
private PreparedStatement ps;
private ResultSet rs;
/**
* 增加
*/
public int add(Student s) {
try {
con=DBHerpre.getCon();
ps=con.prepareStatement("insert into tb_Student values(?,?,?,?)");
ps.setString(1, s.getSname());
ps.setString(2, s.getSclass());
ps.setString(3, s.getSteacher());
ps.setString(4, s.getShobby());
return ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHerpre.myClose(con, ps, rs);
}
return 0;
}
/**
* 删除
*/
public int del(int sid) {
try {
con=DBHerpre.getCon();
ps=con.prepareStatement("delete tb_Student where sid=?");
ps.setInt(1, sid);
return ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHerpre.myClose(con, ps, rs);
}
return 0;
}
/**
* 修改
*/
public int upd(Student s) {
try {
con=DBHerpre.getCon();
ps=con.prepareStatement("update tb_Student set sname=?,sclass=?,steacher=?,shobby=? where sid=?");
ps.setString(1, s.getSname());
ps.setString(2, s.getSclass());
ps.setString(3, s.getSteacher());
ps.setString(4, s.getShobby());
ps.setInt(5, s.getSid());
return ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHerpre.myClose(con, ps, rs);
}
return 0;
}
/**
* 查询全部
*/
public List<Student> getAll() {
List<Student> list = new ArrayList<Student>();
try {
con=DBHerpre.getCon();
ps=con.prepareStatement("select * from tb_Student");
rs=ps.executeQuery();
while(rs.next()) {
Student s = new Student();
s.setSid(rs.getInt(1));
s.setSname(rs.getString(2));
s.setSclass(rs.getString(3));
s.setSteacher(rs.getString(4));
s.setShobby(rs.getString(5));
list.add(s);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHerpre.myClose(con, ps, rs);
}
return list;
}
/**
* 根据id查询单个
*/
public Student getId(int sid) {
try {
con=DBHerpre.getCon();
ps=con.prepareStatement("select * from tb_Student where sid=?");
ps.setInt(1, sid);
rs=ps.executeQuery();
if(rs.next()) {
Student s = new Student();
s.setSid(rs.getInt(1));
s.setSname(rs.getString(2));
s.setSclass(rs.getString(3));
s.setSteacher(rs.getString(4));
s.setShobby(rs.getString(5));
return s;
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHerpre.myClose(con, ps, rs);
}
return null;
}
/**
* 模糊查询
*/
public List<Student> getMo(String sclass, String steacher, String shobby) {
List<Student> list = new ArrayList<Student>();
try {
con=DBHerpre.getCon();
ps=con.prepareStatement("select * from tb_Student where sclass like ? and steacher like ? and shobby like ?");
ps.setString(1, "%"+sclass+"%");
ps.setString(2, "%"+steacher+"%");
ps.setString(3, "%"+shobby+"%");
rs=ps.executeQuery();
while(rs.next()) {
Student s = new Student();
s.setSid(rs.getInt(1));
s.setSname(rs.getString(2));
s.setSclass(rs.getString(3));
s.setSteacher(rs.getString(4));
s.setShobby(rs.getString(5));
list.add(s);
}
return list;
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHerpre.myClose(con, ps, rs);
}
return null;
}
}
package com.cdao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.cclass.impl.IClassDao;
import com.entity.Class;
import com.util.DBHerpre;
/**
* 班级数据库操作包
* @author zjjt
*
*/
public class Clasa1Dao implements IClassDao{
private Connection con;
private PreparedStatement ps;
private ResultSet rs;
/**
* 查询所有班级
*/
public List<Class> getCname() {
List<Class> list = new ArrayList<Class>();
try {
con = DBHerpre.getCon();
ps = con.prepareStatement("select * from tb_Class");
rs = ps.executeQuery();
while(rs.next()) {
Class c = new Class();
c.setCid(rs.getInt(1));
c.setCname(rs.getString(2));
list.add(c);
}
return list;
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHerpre.myClose(con, ps, rs);
}
return null;
}
}
?
package com.tdao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.entity.Teacher;
import com.teacher.impl.ITeacherDao;
import com.util.DBHerpre;
/**
* 教员数据库操作包
* @author zjjt
*
*/
public class TeacherDao implements ITeacherDao{
private Connection con;
private PreparedStatement ps;
private ResultSet rs;
/**
* 查寻所有老师的名字
*/
public List<Teacher> getTname() {
List<Teacher> list = new ArrayList<Teacher>();
try {
con = DBHerpre.getCon();
ps = con.prepareStatement("select * from tb_Teacher");
rs = ps.executeQuery();
while(rs.next()) {
Teacher t = new Teacher();
t.setTid(rs.getInt(1));
t.setTname(rs.getString(2));
list.add(t);
}
return list;
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHerpre.myClose(con, ps, rs);
}
return null;
}
}
?
到这里就结束了,我依旧是那个学IT的小学生?
欢迎大佬指点?
|