JSP页面使用举例(单表的增删查功能实现)
来源:互联网 发布:淘宝举报假冒伪劣 编辑:程序博客网 时间:2024/06/05 02:01
主要内容:
用JSP页面实现学生表的增加记录、删除记录和查询记录功能。
一、前端代码
(1)主页面main.jsp
<%@page import="cn.sdut.po.Student"%><%@page import="cn.sdut.dao.StudentDao"%><%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>My JSP ’main.jsp’ starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <% StudentDao studentDao = new StudentDao(); List<Student> list = studentDao.queryAll(); int i=1; %> <table width="537" height="90" border="1"> <caption>学生管理</caption> <tr> <th width="56">序号 <th width="77">姓名</td> <th width="107">出生年月</td> <th width="92">成绩</td> <th width="171">操作</td> </tr> <% for(Student student:list){ %> <tr> <td align="center"><%=i++ %> </td> <td align="center"><%=student.getName()%> </td> <td align="center"><%=student.getBirthday() %> </td> <td align="center"><%=student.getScore() %></td> <td align="center"> <a href="doDelete.jsp?id=<%=student.getId() %>">删除</a> <a href=""> 修改</a> </td> </tr> <% } %> </table> <br> <a href="add.jsp">增加学生</a> </body></html>
(2)增加数据add.jsp和增加数据处理页面doAdd.jsp
add.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>My JSP ’add.jsp’ starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <script src="js/calendar.js"></script> </head> <body> <form name="form1" method="post" action="doAdd.jsp"> <table width="388" border="1"> <caption>增加学生<br> </caption> <tr> <td width="165" height="42" align="right">姓 名:</td> <td width="207"><input type="text" name="name" id="name"></td> </tr> <tr> <td height="43" align="right">出生年月:</td> <td><input type="text" name="birthday" id="birthday" onclick="new Calendar().show(this)" ></td> </tr> <tr> <td height="57" align="right">成 绩:</td> <td><input type="text" name="score" id="score"></td> </tr> <tr> <td height="59" colspan="2" align="center"><input type="submit" name="OK" id="OK" value="提交"> <input type="reset" name="Cancel" id="Cancel" value="重置"></td> </tr> </table> </form> </body></html>
doAdd.jsp
<%@page import="cn.sdut.dao.StudentDao"%><%@page import="cn.sdut.po.Student"%><%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>My JSP ’doAdd.jsp’ starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <% //1 获得来自客户端输入的数据. request.setCharacterEncoding("utf-8"); String name=request.getParameter("name"); String birthday=request.getParameter("birthday"); float score=Float.parseFloat(request.getParameter("score")); //2 封装成Student对象 Student student=new Student(); student.setName(name); student.setBirthday(birthday); student.setScore(score); //3 StudentDao类的方法add(Student stu),存入表中 StudentDao studentDao=new StudentDao(); int result=studentDao.addStudent(student); //4 转向main.jsp %> <jsp:forward page="main.jsp"></jsp:forward> </body></html>
(3)删除处理页面doDel.jsp
<%@page import="cn.sdut.dao.StudentDao"%><%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>My JSP ’doDelete.jsp’ starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <% //1 获得客户端数据 int id=Integer.parseInt(request.getParameter("id")); //2 利用StudentDao类的delStudent(id)方法删除 StudentDao dao=new StudentDao(); int result=dao.delStudent(id); //3 回到main.jsp %> <jsp:forward page="main.jsp"></jsp:forward> </body></html>
(4)修改页面updateStudent.jsp 和修改处理页面updateStudent.jsp
updateStudent.jsp
<%@page import="cn.sdut.po.Student"%><%@page import="cn.sdut.dao.StudentDao"%><%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>My JSP ’add.jsp’ starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <script src="js/calendar.js"></script> </head> <% //1 从客户端接收id int id=Integer.parseInt(request.getParameter("id")); //2 利用StudentDao类queryStudentById方法得到Student对象 StudentDao studentDao=new StudentDao(); Student stu=studentDao.queryStudentById(id); %> <body> <form name="form1" method="post" action="doUpdate.jsp"> <table width="388" border="1"> <caption>修改学生信息<br> </caption> <tr> <td width="165" height="42" align="right">姓 名:</td> <td width="207"> <input type="text" name="name" id="name" value="<%=stu.getName() %>"></td> </tr> <tr> <td height="43" align="right">出生年月:</td> <td> <input type="text" name="birthday" id="birthday" onclick="new Calendar().show(this)" value="<%=stu.getBirthday()%>"></td> </tr> <tr> <td height="57" align="right">成 绩:</td> <td> <input type="text" name="score" id="score" value="<%=stu.getScore() %>"></td> </tr> <tr> <td height="59" colspan="2" align="center"> <input type="submit" name="OK" id="OK" value="提交"> <input type="reset" name="Cancel" id="Cancel" value="重置"></td> </tr> </table> <input type="hidden" name="id" value="<%=stu.getId() %>"/> </form> </body></html>
(5)修改处理页面updateStudent.jsp
<%@page import="cn.sdut.dao.StudentDao"%><%@page import="cn.sdut.po.Student"%><%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>My JSP ’doUpdate.jsp’ starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <% //1 从客户端获得信息,封装成Student类对象 request.setCharacterEncoding("utf-8"); int id=Integer.parseInt(request.getParameter("id")); String name=request.getParameter("name"); String birthday=request.getParameter("birthday"); float score=Float.parseFloat(request.getParameter("score")); Student stu=new Student(); stu.setId(id); stu.setName(name); stu.setBirthday(birthday); stu.setScore(score); //2 利用StudentDao类的update方法,修改表中的记录 StudentDao studentDao=new StudentDao(); studentDao.updateStudent(stu); //3 转向main.jsp %> <jsp:forward page="main.jsp"></jsp:forward> </body></html>
二、后台代码
(1)cn.sdut.dao包
cn.sdut.dao.baseDao.javapackage cn.sdut.dao;import java.sql.*;public class BaseDao { // 1 定义公用的变量,con,pst,rs Connection con; PreparedStatement pst; ResultSet rs; // 2 得到数据库的连接——方法 public Connection getConn() { try { Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection( "jdbc:mysql://localhost:3306/school?useUnicode=true&characterEncoding=utf-8", "root", "usbw"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return con; } // 3 关闭con,pst,rs public void closeAll() { try { if (rs != null) { rs.close(); } if (pst != null) { pst.close(); } if(con!=null) { con.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}
cn.sdut.dao.studentDao.java
package cn.sdut.dao;import java.sql.SQLException;import java.util.ArrayList;import java.util.List;import cn.sdut.po.Student;public class StudentDao extends BaseDao { // 1 增加记录 public int addStudent(Student stu) { int result=0; con=getConn(); String sql="insert into student(name,birthday,score) values(?,?,?)"; try { pst=con.prepareStatement(sql); pst.setString(1, stu.getName()); pst.setString(2,stu.getBirthday()); pst.setFloat(3, stu.getScore()); result=pst.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } finally { closeAll(); } return result; } // 2 删除记录 public int delStudent(int id) { int result=0; con=getConn(); String sql="delete from student where id=?"; try { pst=con.prepareStatement(sql); pst.setInt(1, id); result=pst.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } finally { closeAll(); } return result; } // 3 修改记录 public int updateStudent(Student stu) { int result=0; con=getConn(); String sql="update student set name=?,birthday=?,score=? where id=?"; try { pst=con.prepareStatement(sql); pst.setString(1, stu.getName()); pst.setString(2,stu.getBirthday()); pst.setFloat(3, stu.getScore()); pst.setInt(4, stu.getId()); result=pst.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } finally { closeAll(); } return result; } // 4 查询所有记录 public List<Student> queryAll() { List<Student> stuList=new ArrayList<Student>(); con=getConn(); String sql="select * from student"; try { pst=con.prepareStatement(sql); rs=pst.executeQuery(); while(rs.next()) { Student stu=new Student(); stu.setId(rs.getInt(1)); stu.setName(rs.getString(2)); stu.setBirthday(rs.getString(3)); stu.setScore(rs.getFloat(4)); stuList.add(stu); } } catch (SQLException e) { e.printStackTrace(); } finally { closeAll(); } return stuList; } //5 根据主键查询当前记录信息 public Student queryStudentById(int id) { Student stu=new Student(); con=getConn(); String sql="select * from student where id=?"; try { pst=con.prepareStatement(sql); pst.setInt(1, id); rs=pst.executeQuery(); if(rs.next()) { stu.setId(rs.getInt(1)); stu.setName(rs.getString(2)); stu.setBirthday(rs.getString(3)); stu.setScore(rs.getFloat(4)); } } catch (SQLException e) { e.printStackTrace(); } finally { closeAll(); } return stu; }}
(2)cn.sdut.po包
cn.sdut.po.Student.javapackage cn.sdut.po;public class Student { private int id; private String name; private String birthday; private float score; public Student(String name, String birthday, float score) { super(); this.name = name; this.birthday = birthday; this.score = score; } public Student() { super(); } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getBirthday() { return birthday; } public void setBirthday(String birthday) { this.birthday = birthday; } public float getScore() { return score; } public void setScore(float score) { this.score = score; } @Override public String toString() { return "Student [id=" + id + ", name=" + name + ", birthday=" + birthday + ", score=" + score + "]"; }}
另外:
日历控件canlender.js请在下载工具栏目下载,将其置于webroot/js路径下.
数据库连接JAR包需要加载到项目中。
0 0
- JSP页面使用举例(单表的增删查功能实现)
- 简易jsp+servlet实现单表增删查改
- jsp+servlet+jdbc增删改查功能实现(一)
- jsp+servlet+jdbc增删改查功能实现(二)
- jsp+servlet的增删改查页面
- 使用Servlet实现增删改查的功能
- backbone实现单表的增删改查
- 初学Hibernate,简单实现单表的增删查改
- MyBatis实现单表增删改查(CURD)--新增
- MyBatis实现单表增删改查(CURD)--删除
- MyBatis实现单表增删改查(CURD)--修改
- Companys公司表的增删该查功能实现
- 数据结构顺序表的“增删改查”功能实现
- MVC、JSP实现mysql的增删改查功能的封装和简陋的界面交互
- jsp开发学生信息管理系统,实现了基本的增删改查的功能
- jsp&servlet的登陆界面以及增删改查功能的实现
- 同一个jsp页面实现增删改查,查询时遇到困难以及定时器遇到的情况
- Realm的简单使用(单表_增删改查_包含异步操作)
- 我的科研生活2017-2-17
- POJ 3530 A Modular Arithmetic Challenge 英文少
- bzoj2754&JZOJ2834【SCOI2012】喵星球上的点名 AC自动机+STL
- apk签名冲突 与旧版不同解决办法 apk签名比对
- 怎样解决ListView条目中的Button事件
- JSP页面使用举例(单表的增删查功能实现)
- 使用Hexo搭建博客
- CHM文档打不开的问题解决方法
- 判断n 是不是素数
- 使用Fiddler抓取模拟器APP的请求,并转发到本地调试
- POJ 3531 Alternating Sum of Digits 英文少
- 02、mysql数据类型和列属性
- Codeforces 766 C Mahmoud and a Message(DP)
- Mysql MySQLSyntaxErrorException Row size too large