所用的jdk版本是1.8,Tomcat是8.5
com.qf.control包下的java文件
1)AddScore.java
public class AddScore extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ScoreDao dao = new ScoreDao();
//1.设置请求和响应的结果集
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
//2.获取前端数据
String sid = req.getParameter("sid");
String sname = req.getParameter("sname");
String cname = req.getParameter("cname");
String score = req.getParameter("score");
try {
int status = 0;
//更新操作
List list = dao.selectByCname(sname);
System.out.println(list);
for(int i = 0;i < list.size() ;i++) {
String Cname = (String)list.get(i);
if(Cname.equals(cname)) {
status = 1;
}
}
if(status == 1) {
int line = dao.update(Integer.parseInt(sid), Double.parseDouble(score),cname);
if(line>0){
//页面跳转
resp.getWriter().write("分数更新成功");
req.getRequestDispatcher("showAllStudents").forward(req, resp);
}else{
System.out.println("成绩更新失败");
}
}
else {
//3.封装对象
Stuscores ss = new Stuscores();
ss.setSid(Integer.parseInt(sid));
ss.setSname(sname);
ss.setCname(cname);
ss.setScore(Double.parseDouble(score));
//4.连接数据库 执行 insert
int line = dao.addS(ss);
if(line>0){
//5.页面跳转
resp.getWriter().write("分数添加成功");
req.getRequestDispatcher("showAllStudents").forward(req, resp);
}else{
System.out.println("成绩添加失败");
}
}
} catch (NumberFormatException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
2)AddStudent.java
public class AddStudent extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//设置请求和响应的编码集
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
//接收前端jsp传入的数据
String sname = req.getParameter("sname");
String ssex = req.getParameter("ssex");
String sage = req.getParameter("sage");
String stel = req.getParameter("stel");
String sdept = req.getParameter("sdept");
String saddress = req.getParameter("saddress");
//创建对象 封装数据
Student stu = new Student();
stu.setSname(sname);
stu.setSsex(ssex);
stu.setSage(Integer.parseInt(sage));
stu.setStel(stel);
stu.setSdept(sdept);
stu.setSaddress(saddress);
//连接数据库 完成 学生信息存储 insert
StudentDao dao = new StudentDao();
try {
int line = dao.addStudent(stu);
if(line>0){
//跳转到指定servlet或jsp页面 两种方式
//第一种
// RequestDispatcher dispatch=req.getRequestDispatcher("showAllStudents");
// dispatch.forward(req, resp);
// //第二种
resp.sendRedirect("showAllStudents");
}else{
resp.getWriter().write("学生信息添加失败");
resp.setHeader("refresh", "2;url=main.jsp");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
3)DeleteScore.java
public class DeleteScore extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1.设置请求和响应的结果集
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
//2.获取前端数据
String sid = req.getParameter("sid");
String cname = req.getParameter("cname");
//3.连接数据库 完成删除操作
ScoreDao dao = new ScoreDao();
try {
int line = dao.deleteScoreById(Integer.parseInt(sid), cname);
if(line>0){
//4.跳转回查询学生信息的Sevlet
req.getRequestDispatcher("showAllStudents").forward(req, resp);
}else{
System.out.println("删除失败");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
}
4)DeleteStudent.java
public class DeleteStudent extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1.设置请求和响应的结果集
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
//2.获取前端数据
String stuId = req.getParameter("stuid");
//3.连接数据库 完成删除操作
StudentDao dao = new StudentDao();
try {
int line = dao.deleteById(stuId);
if(line>0){
//4.跳转回查询学生信息的Sevlet
req.getRequestDispatcher("showAllStudents").forward(req, resp);
}else{
System.out.println("删除失败");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
}
5)LoginServlet.java
public class LoginServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//设置请求和响应的编码集
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
//获取前端传入的表单数据
String username = req.getParameter("username");
String password = req.getParameter("ps");
//创建对象 封装数据
User u = new User();
u.setUsername(username);
u.setPassword(password);
try {
if(username !=null && password!=null){
//连接数据库
UserDao dao = new UserDao();
User returnU = dao.findByUser(u);
//分发转向 如果成功 去什么地方 失败 去什么地方
if(returnU !=null){
//session 作用范围 域对象 服务器端 存入到该域中的值 在整个项目中的servlet jsp都能使用
HttpSession session = req.getSession();//获取session域对象 存储空间 用来存储user登录信息
session.setAttribute("uname", username);//将用户名的值存入此空间 并且给个名字
req.getRequestDispatcher("main.jsp").forward(req, resp);
}else{
resp.getWriter().write("用户名或者密码错误 请重新输入");
resp.setHeader("refresh", "2;url=login.jsp");
}
}else{
resp.getWriter().write("用户名或者密码不能为空");
resp.setHeader("refresh", "2;url=login.jsp");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
6)RegisterServlet.java
public class RegisterServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//设置请求和响应的编码集
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
String username = req.getParameter("username");
String pwd = req.getParameter("pwd");
String repassword = req.getParameter("repassword");
UserDao userDao = new UserDao();
if(username != null) {
if(pwd.equals(repassword)) {
User user = new User();
user.setPassword(pwd);
user.setUsername(username);
try {
int a = userDao.registerUser(user);
if(a != 0) {
resp.getWriter().write("注册成功");
req.getRequestDispatcher("login.jsp").forward(req, resp);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else {
resp.getWriter().write("密码不一致 请重新输入");
resp.setHeader("refresh", "2;url=login.jsp");
}
}
else{
resp.getWriter().write("用户名错误 请重新输入");
resp.setHeader("refresh", "2;url=login.jsp");
}
}
}
7)SearchBySname.java
public class SearchBySname extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1.设置请求和响应的编码集
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
//2.获取前端数据
String searchsname = req.getParameter("sname");
String searchssex = req.getParameter("ssex");
String searchminsage = req.getParameter("minsage");
String searchmaxsage = req.getParameter("maxsage");
//3.连接数据库 查询功能
StudentDao dao = new StudentDao();
try {
ArrayList<Student> list = dao.searchBySname(searchsname,searchssex,searchminsage,searchmaxsage);
//将list 集合 发送到前端页面去
//为了让集合中的数据 能够在jsp页面内显示 必须 将集合存入 请求域中
req.setAttribute("students", list);
req.getRequestDispatcher("show.jsp").forward(req, resp);//将数据发送到前端jsp页面
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
8)SelectStudentById.java
```java
public class SelectStudentById extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1.设置请求和响应的结果集
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
//2.获取前端数据
String stuId = req.getParameter("stuid");
//3.连接数据库 进行 查询
StudentDao dao = new StudentDao();
try {
Student stu = dao.selectById(stuId);
//为了让stu对象的数据 能够在jsp页面显示 必须将他存入request域中
//也就是发送到前端
req.setAttribute("student", stu);
//4.跳转到指定页面
req.getRequestDispatcher("updateStudent.jsp").forward(req, resp);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
}
9)ShowAllStudents.java
public class ShowAllStudents extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
//连接数据库 select
StudentDao dao = new StudentDao();
try {
ArrayList<Student> list = dao.showAllStudents();
//为了让集合中的数据 能够在jsp页面内显示 必须 将集合存入 请求域中
req.setAttribute("students", list);
//resp.sendRedirect("show.jsp");
req.getRequestDispatcher("show.jsp").forward(req, resp);//将数据发送到前端jsp页面
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req, resp);
//AddStudent servlet中 数据处理是放在doPost方法中 所以转发也是到本servlet的doPost
}
}
10)ShowScore.java
//普通a标签连接 到doGet方法中 操作数据
//form表单 没有明确提交方式method 到doGet方法中操作数据
//查看指定学生的成绩列表
public class ShowScore extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1.设置请求和响应的结果集
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
//2.获取前端数据
String stuid = req.getParameter("stuid");
String sname = req.getParameter("sname");
//3.连接数据库 执行select
ScoreDao dao = new ScoreDao();
try {
ArrayList<Stuscores> list = dao.selectS(Integer.parseInt(stuid));
//将学生姓名单独发送到页面 将sname发送到前端 起个别名 叫sn
req.setAttribute("sn", sname);
//将集合发送到页面 将list集合发送到前端页面 并且给起了个名字 叫scores
req.setAttribute("scores", list);
//4.跳转页面
req.getRequestDispatcher("showScores.jsp").forward(req, resp);
} catch (NumberFormatException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
}
11)UpdatepwServlet.java
public class UpdatepwServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取session对象 再从session 对象中取出值
HttpSession session = req.getSession();
String username = (String)session.getAttribute("uname");
//获取前端传入的密码
String password = req.getParameter("newpassword");
//连接数据库 执行update语句 根据username 修改password
User u = new User();
u.setUsername(username);
u.setPassword(password);
UserDao dao = new UserDao();
try {
int line = dao.updatePw(u);
if(line>0){
//修改密码成功 login.jsp重新登录
resp.getWriter().write("密码修改成功 请重新登录");
resp.setHeader("refresh", "2;url=login.jsp");
}else{
//提示信息 作业 失败 跳转回 updatePw.jsp
System.out.println("失败");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
}
12)UpdateStudent.java
public class UpdateStudent extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1.设置请求和响应的结果集
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
//2.获取前端数据
//接收前端jsp传入的数据
String sid = req.getParameter("sid");
String sname = req.getParameter("sname");
String ssex = req.getParameter("ssex");
String sage = req.getParameter("sage");
String stel = req.getParameter("stel");
String sdept = req.getParameter("sdept");
String saddress = req.getParameter("saddress");
//创建对象 封装数据
Student stu = new Student();
stu.setSid(Integer.parseInt(sid));
stu.setSname(sname);
stu.setSsex(ssex);
stu.setSage(Integer.parseInt(sage));
stu.setStel(stel);
stu.setSdept(sdept);
stu.setSaddress(saddress);
//4.连接数据 执行update操作
StudentDao dao = new StudentDao();
try {
int line = dao.updateById(stu);
//5.页面跳转
if(line>0){
req.getRequestDispatcher("showAllStudents").forward(req, resp);
}else{
System.out.println("修改失败");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
com.qf.dao包下的代码
1)ScoreDao.java
public class ScoreDao {
public int update(int sid,double score ,String cname) throws SQLException{
//1.创建连接
Connection conn = DBUtils.getConnection();
//2.创建执行sql语句的对象
Statement stmt = conn.createStatement();
//3.sql语句
String sql = "update stuscores set score = " + score + " where sid = "+sid+" and cname = '"+cname+"'";
System.out.println(sql);
int line = stmt.executeUpdate(sql);
return line;
}
/*一.给指定的学生添加成绩
* 1.方法名 addS
* 2.参数列表 Stuscores
* 3.返回值类型 int
* */
public int addS(Stuscores ss) throws SQLException{
//1.创建连接
Connection conn = DBUtils.getConnection();
//2.创建执行sql语句的对象
Statement stmt = conn.createStatement();
//3.sql语句
String sql = "insert into stuscores(sid,sname,cname,score) values("+ss.getSid()+",'"+ss.getSname()+"','"+ss.getCname()+"',"+ss.getScore()+")";
//4.执行 返回结果
int line = stmt.executeUpdate(sql);
return line;
}
/*二.查询指定学号的学员成绩列表
* 1.方法名 selectS
* 2.参数列表 stuid
* 3.返回值类型 ArrayList
* */
public ArrayList<Stuscores> selectS(int stuid) throws SQLException{
//1.创建连接
Connection conn = DBUtils.getConnection();
//2.创建执行sql的对象
Statement stmt = conn.createStatement();
//3.sql语句
String sql = "select * from stuscores where sid="+stuid;
//4.返回结果集 executeQuery
ResultSet rs = stmt.executeQuery(sql);
//5.创建集合 来 存储结果
ArrayList<Stuscores> list = new ArrayList();
while(rs.next()){
int id = rs.getInt("id");
int sid = rs.getInt("sid");
String sname = rs.getString("sname");
String cname = rs.getString("cname");
double score = rs.getDouble("score");
Stuscores ss = new Stuscores();
ss.setId(id);
ss.setSid(sid);
ss.setSname(sname);
ss.setCname(cname);
ss.setScore(score);
//将对象填入集合中
list.add(ss);
}
return list;
}
public boolean selectById(int sid) throws SQLException {
//1.创建连接
Connection conn = DBUtils.getConnection();
//2.创建执行sql的对象
Statement stmt = conn.createStatement();
//3.sql语句
String sql = "select * from stuscores where sid = " + sid;
ResultSet line = stmt.executeQuery(sql);
if(line.next()) {
return true;
}
else {
return false;
}
}
public int deleteScoreById(int sid,String cname) throws SQLException{
//1.创建连接
Connection conn = DBUtils.getConnection();
//2.创建执行sql的对象
Statement stmt = conn.createStatement();
//3.sql语句
String sql = "delete from stuscores where sid = "+sid+" and cname = '"+cname+"'";
System.out.println(sql);
int line = stmt.executeUpdate(sql);
return line;
}
public List selectByCname(String sname) throws SQLException{
String cname = "";
ArrayList list = new ArrayList();
//1.创建连接
Connection conn = DBUtils.getConnection();
//2.创建执行sql的对象
Statement stmt = conn.createStatement();
//3.sql语句
String sql = "select cname from stuscores where sname='"+sname+"'";
System.out.println(sql);
ResultSet line = stmt.executeQuery(sql);
while(line.next()) {
cname = line.getString("cname");
list.add(cname);
}
return list;
}
}
2)StudentDao.java
//完成对学生信息的 增删改查操作
public class StudentDao {
/*一.添加
* 1.方法名 addStudent
* 2.参数列表 Student
* 3.返回值类型 int
* */
public int addStudent(Student stu) throws SQLException{
//创建连接
Connection conn = DBUtils.getConnection();
//创建执行sql语句的对象
Statement stmt = conn.createStatement();
//sql语句
String sql="INSERT INTO student(sname,sage,ssex,sdept,stel,saddress) VALUES('"+stu.getSname()+"',"+stu.getSage()+",'"+stu.getSsex()+"','"+stu.getSdept()+"',"+stu.getStel()+",'"+stu.getSaddress()+"')";
//执行sql语句 executeUpdate
int line=stmt.executeUpdate(sql);
return line;
}
/*二.查询所有学生信息
* 1.方法名 showAllStudents
* 2.参数列表
* 3.返回值类型 ArrayList
* */
public ArrayList<Student> showAllStudents() throws SQLException{
//连接数据库
Connection conn = DBUtils.getConnection();
//创建执行sql的对象
Statement stmt = conn.createStatement();
//sql语句
String sql = "select * from student";
//查询要返回结果集 executeQuery
ResultSet rs = stmt.executeQuery(sql);
//循环获取表中每一行数据 获取一行 就封装对象 将对象存入集合中
//创建集合
ArrayList<Student> list = new ArrayList<>();
while(rs.next()){
int sid = rs.getInt("sid");
String sname = rs.getString("sname");
int sage = rs.getInt("sage");
String ssex = rs.getString("ssex");
String stel = rs.getString("stel");
String sdept = rs.getString("sdept");
String saddress = rs.getString("saddress");
//创建对象 封装数据
Student stu = new Student();
stu.setSid(sid);
stu.setSname(sname);
stu.setSage(sage);
stu.setSsex(ssex);
stu.setStel(stel);
stu.setSdept(sdept);
stu.setSaddress(saddress);
//将对象存入集合中
list.add(stu);
}
return list;
}
/*三.根据学生姓名 进行模糊查询
* 1.方法名 searchBySname
* 2.参数列表 姓名 性别 年龄区间
* 3.返回值类型 ArrayList
* */
public ArrayList<Student> searchBySname(String searchsname,String searchssex,String searchminsage,String searchmaxsage) throws SQLException{
//连接数据库
Connection conn = DBUtils.getConnection();
//创建执行sql 语句的对象
Statement stmt = conn.createStatement();
//sql语句
String sql = "select * from student where 1=1";
//判断 四个参数 是否不等于空 如果不等于空 就添加到当前sql语句后面
if(!"".equals(searchsname)){
sql += " and sname like '%"+searchsname+"%'";
}
if(!"".equals(searchssex) && searchssex!=null){
sql += " and ssex='"+searchssex+"'";
}
if(!"".equals(searchminsage)){
sql += " and sage>"+searchminsage;
}
if(!"".equals(searchmaxsage)){
sql +=" and sage<"+searchmaxsage;
}
System.out.println(sql);
//执行sql语句 executeQuery
ResultSet rs = stmt.executeQuery(sql);
ArrayList<Student> list = new ArrayList<>();
while(rs.next()){
int sid = rs.getInt("sid");
String sname = rs.getString("sname");
int sage = rs.getInt("sage");
String ssex = rs.getString("ssex");
String stel = rs.getString("stel");
String sdept = rs.getString("sdept");
String saddress = rs.getString("saddress");
//创建对象 封装数据
Student stu = new Student();
stu.setSid(sid);
stu.setSname(sname);
stu.setSage(sage);
stu.setSsex(ssex);
stu.setStel(stel);
stu.setSdept(sdept);
stu.setSaddress(saddress);
//将对象存入集合中
list.add(stu);
}
return list;
}
/*四.根据学生id删除指定学生信息
* 1.方法名 deleteById
* 2.参数列表 String stuId
* 3.返回值类型 int
* */
public int deleteById(String stuId) throws SQLException{
Connection conn = DBUtils.getConnection();
Statement stmt = conn.createStatement();
String sql = "DELETE FROM student WHERE sid="+stuId;
int line = stmt.executeUpdate(sql);
return line;
}
/*五.根据主键查询学生信息
* 1.方法名 selectById
* 2.参数列表 stuId
* 3.返回值类型 Student
* */
public Student selectById(String stuId) throws SQLException{
Connection conn = DBUtils.getConnection();
Statement stmt = conn.createStatement();
String sql = "select * from student where sid="+stuId;
ResultSet rs = stmt.executeQuery(sql);
Student stu = null;
while(rs.next()){
int sid = rs.getInt("sid");
String sname = rs.getString("sname");
int sage = rs.getInt("sage");
String ssex = rs.getString("ssex");
String stel = rs.getString("stel");
String sdept = rs.getString("sdept");
String saddress = rs.getString("saddress");
//创建对象 封装数据
stu = new Student();
stu.setSid(sid);
stu.setSname(sname);
stu.setSage(sage);
stu.setSsex(ssex);
stu.setStel(stel);
stu.setSdept(sdept);
stu.setSaddress(saddress);
}
return stu;
}
/*六 根据主键 sid 修改指定学生的信息
* 1.方法名 updateById
* 2.参数列表Student
* 3.返回值类型 int
* */
public int updateById(Student stu) throws SQLException{
Connection conn = DBUtils.getConnection();
Statement stmt = conn.createStatement();
String sql = "UPDATE student SET sname='"+stu.getSname()+"',sage="+stu.getSage()+",ssex='"+stu.getSsex()+"',sdept='"+stu.getSdept()+"',stel="+stu.getStel()+",saddress='"+stu.getSaddress()+"' where sid="+stu.getSid();
int line = stmt.executeUpdate(sql);
return line;
}
}
3)UserDao.java
public class UserDao {
//登录 查询 根据用户名 和 密码
/*1.方法名 findByUser
*2.参数列表 User对象
*3.返回值类型 User
* */
public User findByUser(User user) throws SQLException{
Connection conn = DBUtils.getConnection();
//创建执行sql语句的对象
Statement stmt = conn.createStatement();
//编写sql语句
String sql = "select * from user where username='"+user.getUsername()+"' and password='"+user.getPassword()+"'";
//executeQuery方法
ResultSet rs = stmt.executeQuery(sql);
//如果查询到 就封装对象
User returnU = null;
if(rs.next()){
returnU = new User();
returnU.setUsername(rs.getString("username"));
returnU.setPassword(rs.getString("password"));
}
return returnU;
}
//修改密码 1.方法名 updatePw 2.参数列表 User 3.返回值类型 int
public int updatePw(User u) throws SQLException{
//连接数据库
Connection conn = DBUtils.getConnection();
//创建执行sql 的语句对象
Statement stmt = conn.createStatement();
//sql语句
String sql = "update USER SET PASSWORD='"+u.getPassword()+"' WHERE username='"+u.getUsername()+"'";
//executeUpdate
int line = stmt.executeUpdate(sql);
return line;
}
public int registerUser(User u) throws SQLException{
//连接数据库
Connection conn = DBUtils.getConnection();
//创建执行sql 的语句对象
Statement stmt = conn.createStatement();
//sql语句
String sql = "insert into user values(null,'"+u.getPassword()+"','"+u.getUsername()+"')";
System.out.println(sql);
int line = stmt.executeUpdate(sql);
return line;
}
}
com.qf.db包下的代码
1)DBUtils。java
public class DBUtils {
private static String driverclass;
private static String url;
private static String username;
private static String password;
//静态代码块 static关键字 与类相关 与对象无关 随着类的加载而加载 当DBUtils类被执行时 自动执行
static{
//创建对象 获取 DBConfig.properties配置文件
ResourceBundle rb = ResourceBundle.getBundle("DBConfig");
//获取 文件中的四个连接数据库要用到的值
driverclass = rb.getString("driverclass");
url = rb.getString("url");
username = rb.getString("username");
password = rb.getString("password");
//1.注册驱动
try {
Class.forName(driverclass);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//2.创建连接
public static Connection getConnection() throws SQLException{
Connection conn = DriverManager.getConnection(url,username,password);
return conn;
}
//3.释放资源
public static void closeAll(ResultSet rs,Statement stmt,Connection conn){
try {
rs.close();
stmt.close();
conn.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
com.qf.model包下的代码
1)Student .java
public class Student {
private int sid; //id
private String sname;//姓名
private String ssex;//性别
private int sage;//年龄
private String sdept;//系别
private String stel;//电话
private String saddress;//地址
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getSsex() {
return ssex;
}
public void setSsex(String ssex) {
this.ssex = ssex;
}
public int getSage() {
return sage;
}
public void setSage(int sage) {
this.sage = sage;
}
public String getSdept() {
return sdept;
}
public void setSdept(String sdept) {
this.sdept = sdept;
}
public String getStel() {
return stel;
}
public void setStel(String stel) {
this.stel = stel;
}
public String getSaddress() {
return saddress;
}
public void setSaddress(String saddress) {
this.saddress = saddress;
}
}
2)Stuscores.java
public class Stuscores {
private int id;
private int sid;
private String sname;
private String cname;
private double score;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
}
3)User.java
public class User {
private String username;
private String password;
private int id;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
DBConfig.properties文件
driverclass=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/stumanager?useEncoding=true&characterEncoding=utf8
username=root
password=123456
addStudent.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link href="${pageContext.request.contextPath}/bootstrap/css/bootstrap.css" rel="stylesheet">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>添加成绩页面</title>
</head>
<body>
<div>
<img src="${pageContext.request.contextPath}/images/logo.png"/>
</div>
<div>
<div class="row-fluid">
<div class="span12">
<div class="navbar">
<div class="navbar-inner">
<a class="brand" href="main.jsp">主页</a>
<ul class="nav">
<li>
<a href="addStudent.jsp">添加学生信息</a>
</li>
<li>
<a href="showAllStudents">学生信息维护</a>
</li>
<li>
<a href="updatePw.jsp">修改密码</a>
</li>
<li>
<a href="login.jsp">退出登录</a>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
<div style="margin-left: 20px">
<h2>修改成绩</h2>
<form action="addS" method="post">
<table>
<Tr>
<Td>学号</Td>
<Td><input type="text" name="sid" value="<%=request.getParameter("stuid")%>" readonly="readonly"/></Td>
</Tr>
<Tr>
<Td>姓名</Td>
<Td>
<input type="text" name="sname" value="<%=request.getParameter("sname")%>" readonly="readonly"/>
</Td>
</Tr>
<Tr>
<Td>学科</Td>
<Td><input type="text" name="cname" value="<%=request.getParameter("cname") == null ? "" : request.getParameter("cname")%>" /></Td>
</Tr>
<Tr>
<Td>成绩</Td>
<Td><input type="text" name="score" value="<%=request.getParameter("score") == null ? "" : request.getParameter("score")%>" /></Td>
</Tr>
<tr>
<Td>
<input type="submit" value="提交" class="btn btn-danger"/>
<input type="button" value="返回" onclick="window.location.href='main.jsp'" class="btn btn-success"/>
</Td>
</tr>
</table>
</form>
</div>
</body>
</html>
2)addStudent.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link href="${pageContext.request.contextPath}/bootstrap/css/bootstrap.css" rel="stylesheet">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>添加学生信息页面</title>
</head>
<body>
<div>
<img src="${pageContext.request.contextPath}/images/logo.png"/>
</div>
<div>
<div class="row-fluid">
<div class="span12">
<div class="navbar">
<div class="navbar-inner">
<a class="brand" href="main.jsp">主页</a>
<ul class="nav">
<li>
<a href="addStudent.jsp">添加学生信息</a>
</li>
<li>
<a href="showAllStudents">学生信息维护</a>
</li>
<li>
<a href="updatePw.jsp">修改密码</a>
</li>
<li>
<a href="login.jsp">退出登录</a>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
<div style="margin-left: 20px">
<h2>添加学生信息</h2>
<form action="addStudent" method="post">
<table>
<Tr>
<Td>姓名</Td>
<Td><input type="text" name="sname"/></Td>
</Tr>
<Tr>
<Td>性别</Td>
<Td>
<input type="radio" name="ssex" value="男"/>男
<input type="radio" name="ssex" value="女"/>女
</Td>
</Tr>
<Tr>
<Td>年龄</Td>
<Td><input type="text" name="sage"/></Td>
</Tr>
<Tr>
<Td>电话</Td>
<Td><input type="text" name="stel"/></Td>
</Tr>
<Tr>
<Td>系别</Td>
<Td><input type="text" name="sdept"/></Td>
</Tr>
<Tr>
<Td>地址</Td>
<Td><textarea name="saddress" rows="3" cols="5"></textarea></Td>
</Tr>
<tr>
<Td>
<input type="submit" value="提交" class="btn btn-danger"/>
<input type="button" value="返回" onclick="window.location.href='main.jsp'" class="btn btn-success"/>
</Td>
</tr>
</table>
</form>
</div>
</body>
</html>
3)login,jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link href="${pageContext.request.contextPath}/bootstrap/css/bootstrap.css" rel="stylesheet">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>添加学生信息页面</title>
</head>
<body>
<div>
<img src="${pageContext.request.contextPath}/images/logo.png"/>
</div>
<div>
<div class="row-fluid">
<div class="span12">
<div class="navbar">
<div class="navbar-inner">
<a class="brand" href="main.jsp">主页</a>
<ul class="nav">
<li>
<a href="addStudent.jsp">添加学生信息</a>
</li>
<li>
<a href="showAllStudents">学生信息维护</a>
</li>
<li>
<a href="updatePw.jsp">修改密码</a>
</li>
<li>
<a href="login.jsp">退出登录</a>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
<div style="margin-left: 20px">
<h2>添加学生信息</h2>
<form action="addStudent" method="post">
<table>
<Tr>
<Td>姓名</Td>
<Td><input type="text" name="sname"/></Td>
</Tr>
<Tr>
<Td>性别</Td>
<Td>
<input type="radio" name="ssex" value="男"/>男
<input type="radio" name="ssex" value="女"/>女
</Td>
</Tr>
<Tr>
<Td>年龄</Td>
<Td><input type="text" name="sage"/></Td>
</Tr>
<Tr>
<Td>电话</Td>
<Td><input type="text" name="stel"/></Td>
</Tr>
<Tr>
<Td>系别</Td>
<Td><input type="text" name="sdept"/></Td>
</Tr>
<Tr>
<Td>地址</Td>
<Td><textarea name="saddress" rows="3" cols="5"></textarea></Td>
</Tr>
<tr>
<Td>
<input type="submit" value="提交" class="btn btn-danger"/>
<input type="button" value="返回" onclick="window.location.href='main.jsp'" class="btn btn-success"/>
</Td>
</tr>
</table>
</form>
</div>
</body>
</html>
4)main.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link href="${pageContext.request.contextPath}/bootstrap/css/bootstrap.css" rel="stylesheet">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>学生信息管理系统 主页</title>
</head>
<body>
<div>
<img src="${pageContext.request.contextPath}/images/logo.png"/>
</div>
<div>
<div class="row-fluid">
<div class="span12">
<div class="navbar">
<div class="navbar-inner">
<a class="brand" href="main.jsp">主页</a>
<ul class="nav">
<li>
<a href="addStudent.jsp">添加学生信息</a>
</li>
<li>
<a href="showAllStudents">学生信息维护</a>
</li>
<li>
<a href="updatePw.jsp">修改密码</a>
</li>
<li>
<a href="login.jsp">退出登录</a>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
<div align="center">
<font color="red">欢迎进入学生管理系统</font>
</div>
</body>
</html>
5)register.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta charset="utf-8">
<title>用户注册</title>
<script type="text/javascript">
function inName(){
var username = document.regForm.username.value;
if(username == "请输入用户名"){
document.myform.username.value = "";
}
}
function usernameCheck(){
var username = document.regForm.username.value;
if(username == ""){
window.alert("请输入用户名");
document.myform.username.value = "请输入用户名";
}
}
</script>
</head>
<body>
<div align="center">
<!-- 注册表单 -->
<form name="regForm" method="post" action="RegistServlet" onsubmit="return form_submit(this)">
<table border="5" width="700px" style="color: blue">
<tr align="center">
<td colspan="2">用户注册</td>
</tr>
<tr>
<td width="100px" align="center" style="color: red">用户名:</td>
<td width="600px"><input type="text" name="username" id="username" onfocus="inName()" onblur="usernameCheck()"><span id="check_username" style="color: red; padding-left: 5px;"></span></td>
</tr>
<tr>
<td width="100px" align="center" style="color: red">密码:</td>
<td width="600px"><input type="password" name="pwd" id="pwd"></td>
</tr>
<tr>
<td width="100px" align="center" style="color: red">确认密码:</td>
<td width="600px"><input type="password" name="repassword" id="repassword"></td>
</tr>
<tr align="center">
<td colspan="2">
<input type="submit" style="color: DarkOrchid"value="注册">
<input type="button" onclick="window.location.href='login.jsp'" style="color: DarkOrchid" value="登录">
</td>
</tr>
</table>
</form>
</div>
</body>
</html>
6)show.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link href="${pageContext.request.contextPath}/bootstrap/css/bootstrap.css" rel="stylesheet">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>学生信息列表页面</title>
<script type="text/javascript">
function checkDelete(){
var flag = window.confirm("是否确定删除??")
return flag;
}
</script>
</head>
<body>
<div>
<img src="${pageContext.request.contextPath}/images/logo.png"/>
</div>
<div>
<div class="row-fluid">
<div class="span12">
<div class="navbar">
<div class="navbar-inner">
<a class="brand" href="main.jsp">主页</a>
<ul class="nav">
<li>
<a href="addStudent.jsp">添加学生信息</a>
</li>
<li>
<a href="showAllStudents">学生信息维护</a>
</li>
<li>
<a href="updatePw.jsp">修改密码</a>
</li>
<li>
<a href="login.jsp">退出登录</a>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
<div style="margin-left: 20px">
<div>
<form action="searchBySname" method="post">
<table>
<tr>
<Td>
姓名<input type="text" name="sname"/>
性别<input type="radio" name="ssex" value="男"/>男
<input type="radio" name="ssex" value="女"/>女
</Td>
</tr>
<tr>
<Td>
年龄<input type="text" name="minsage"/>-<input type="text" name="maxsage"/>
<input type="submit" value="查找" class="btn btn-inverse"/>
</Td>
</tr>
</table>
</form>
</div>
<table class="table table-hover table-condensed">
<tr>
<Td>编号</Td><Td>姓名</Td><Td>性别</Td><Td>年龄</Td><Td>系别</Td><Td>电话</Td><Td>地址</Td>
<Td>执行操作</Td>
</tr>
<c:forEach items="${students}" var="stu">
<tr>
<Td>${stu.sid}</Td>
<Td>${stu.sname}</Td>
<Td>${stu.ssex}</Td>
<Td>${stu.sage}</Td>
<Td>${stu.sdept}</Td>
<Td>${stu.stel}</Td>
<Td>${stu.saddress}</Td>
<Td>
<a href="deleteStudent?stuid=${stu.sid}" onclick="return checkDelete()" class="btn btn-danger">删除</a>
<a href="selectStudentById?stuid=${stu.sid}" class="btn btn-primary">修改</a>
<a href="addScore.jsp?stuid=${stu.sid}&sname=${stu.sname}" class="btn btn-success">添加成绩</a>
<a href="showS?stuid=${stu.sid}&sname=${stu.sname}" class="btn btn-warning">查看成绩</a>
</Td>
</tr>
</c:forEach>
</table>
</div>
</body>
</html>
7)showScores.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link href="${pageContext.request.contextPath}/bootstrap/css/bootstrap.css" rel="stylesheet">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>学生成绩列表页面</title>
<script type="text/javascript">
function checkDelete(){
var flag = window.confirm("是否确定删除??")
return flag;
}
</script>
</head>
<body>
<div>
<img src="${pageContext.request.contextPath}/images/logo.png"/>
</div>
<div>
<div class="row-fluid">
<div class="span12">
<div class="navbar">
<div class="navbar-inner">
<a class="brand" href="main.jsp">主页</a>
<ul class="nav">
<li>
<a href="addStudent.jsp">添加学生信息</a>
</li>
<li>
<a href="showAllStudents">学生信息维护</a>
</li>
<li>
<a href="updatePw.jsp">修改密码</a>
</li>
<li>
<a href="login.jsp">退出登录</a>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
<div style="margin-left: 20px">
<font color="blue">${sn}</font>
<table class="table table-hover table-condensed">
<tr>
<Td>编号</Td><Td>学科</Td><Td>成绩</Td><td>操作</td>
</tr>
<c:forEach items="${scores}" var="s" varStatus="ss">
<tr>
<Td>${ss.count}</Td>
<Td>${s.cname}</Td>
<Td>${s.score}</Td>
<Td>
<a href="addScore.jsp?stuid=${s.sid}&sname=${s.sname}&cname=${s.cname}&score=${s.score}" class="btn">修改</a>
<a href="deleteScoreById?sid=${s.sid}&cname=${s.cname}" onclick="return checkDelete()" class="btn">删除</a>
</Td>
</tr>
</c:forEach>
</table>
</div>
<a href="main.jsp" class="btn btn-success">返回系统主页</a>
</body>
</html>
8)update.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link href="${pageContext.request.contextPath}/bootstrap/css/bootstrap.css" rel="stylesheet">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>密码修改</title>
<script type="text/javascript">
function check(){
var psw1 = document.myform.newpassword.value;
var psw2 = document.myform.repassword.value;
if(psw2!=psw1){
window.alert("两次输入的密码不一致 请重新输入");
return false;
}
return true;
}
</script>
</head>
<body>
<div>
<img src="${pageContext.request.contextPath}/images/logo.png"/>
</div>
<div>
<div class="row-fluid">
<div class="span12">
<div class="navbar">
<div class="navbar-inner">
<a class="brand" href="main.jsp">主页</a>
<ul class="nav">
<li>
<a href="addStudent.jsp">添加学生信息</a>
</li>
<li>
<a href="showAllStudents">学生信息维护</a>
</li>
<li>
<a href="updatePw.jsp">修改密码</a>
</li>
<li>
<a href="login.jsp">退出登录</a>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
<div style="margin-left: 20px">
<form action="updatePw" name="myform">
<h2>修改密码</h2>
<table>
<Tr>
<Td>新密码</Td>
<td><input type="password" name="newpassword"/></td>
</Tr>
<Tr>
<Td>确认新密码</Td>
<td><input type="password" name="repassword"/></td>
</Tr>
<Tr>
<Td>
<!-- 当点击 submit按钮 同时触发JavaScript验证 验证 新密码 和 确认密码 是否输入一致 如果一致
才进入servlet 完成逻辑 如果不一致 提示 输入错误-->
<input type="submit" class="btn btn-primary" value="修改" onclick="return check()"/>
<input type="reset" class="btn btn-success" value="清除"/>
</Td>
</Tr>
</table>
</form>
</div>
</body>
</html>
9)updateStudent.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link href="${pageContext.request.contextPath}/bootstrap/css/bootstrap.css" rel="stylesheet">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>修改学生信息页面</title>
</head>
<body>
<div>
<img src="${pageContext.request.contextPath}/images/logo.png"/>
</div>
<div>
<div class="row-fluid">
<div class="span12">
<div class="navbar">
<div class="navbar-inner">
<a class="brand" href="main.jsp">主页</a>
<ul class="nav">
<li>
<a href="addStudent.jsp">添加学生信息</a>
</li>
<li>
<a href="showAllStudents">学生信息维护</a>
</li>
<li>
<a href="updatePw.jsp">修改密码</a>
</li>
<li>
<a href="login.jsp">退出登录</a>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
<div style="margin-left: 20px">
<h2>修改学生信息</h2>
<form action="updateStudent" method="post">
<!-- 隐藏域 type='hidden' 值不会在页面显示 但是在提交数据的时候 能够被传输 -->
<input type="hidden" name="sid" value="${student.sid}"/>
<table>
<Tr>
<Td>姓名</Td>
<Td><input type="text" name="sname" value="${student.sname}"/></Td>
</Tr>
<Tr>
<Td>性别</Td>
<Td><!-- 判断 数据库传回的性别 是男 还是女 如果是男 就在value="男" 单选框添加 checked
如果是女 就在value="女" 添加checked
x?y:z ---if...else...
-->
<input type="radio" name="ssex" value="男" ${student.ssex eq '男'?'checked':''}/>男
<input type="radio" name="ssex" value="女" ${student.ssex eq '女'?'checked':''}/>女
</Td>
</Tr>
<Tr>
<Td>年龄</Td>
<Td><input type="text" name="sage" value="${student.sage}"/></Td>
</Tr>
<Tr>
<Td>电话</Td>
<Td><input type="text" name="stel" value="${student.stel}"/></Td>
</Tr>
<Tr>
<Td>系别</Td>
<Td><input type="text" name="sdept" value="${student.sdept}"/></Td>
</Tr>
<Tr>
<Td>地址</Td>
<Td><textarea name="saddress" rows="3" cols="5">${student.saddress}</textarea></Td>
</Tr>
<tr>
<Td>
<input type="submit" value="提交" class="btn btn-danger"/>
<input type="button" value="返回" onclick="window.location.href='main.jsp'" class="btn btn-success"/>
</Td>
</tr>
</table>
</form>
</div>
</body>
</html>
运行界面如下
拿走拿走!