Java_Web之俱乐部会员信息管理系统
使用 Jsp实现俱乐部会员信息管理功能,orac1e11g作为后台数据库,该系统包括
查看俱乐部会员信息列表和修改俱乐部会员信息两人功能,具体耍求如下:
打开俱乐部会员信息列表页面,以列表方式显示所有俱乐部会员信息。
具体要求及推荐实现步骤:
第一步:创建数据表(memberisnfo)
create table memberisnfo( id number(10) primary key,--会员编号 mname varchar2(30) not null, --会员姓名 mgender varchar2(2) not null, --会员性别 mage number(10) not null,--会员年龄 maddress varchar2(100),--家庭位置 memail varchar2(50) --电子邮件 ) --自增序列 CREATE SEQUENCE id_seq START WITH 1 INCREMENT BY 1 NOMAXVALUE CACHE 10; --插入数据 insert into memberisnfo values (1,'黄鼠狼','男','36','江西生物科技职业学院','1210486771@qq.com'); insert into memberisnfo values (id_seq.Nextval,'东东','男','20','江西生物科技职业学院','558899520@qq.com'); insert into memberisnfo values (id_seq.Nextval,'薇薇','女','25','南昌理工学院','258258456@qq.com'); insert into memberisnfo values (id_seq.Nextval,'花花','女','12','江西生物科技职业学院','2315429277@qq.com'); select * from memberisnfo --查询数据
第二步:创建实体类
package entity; /* * 创建俱乐部会员系统实体参数 * * */ public class Club_Name { private String id;//创建id private String mname;//创建名字实体 private String mgender;//创建日期实体 private String mage;//创建年龄实体 private String maddress;//创建地址实体 private String memail;//创建Emaile实体 public String getId() { return id; } public void setId(String id) { this.id = id; } public String getMname() { return mname; } public void setMname(String mname) { this.mname = mname; } public String getMgender() { return mgender; } public void setMgender(String mgender) { this.mgender = mgender; } public String getMage() { return mage; } public void setMage(String mage) { this.mage = mage; } public String getMaddress() { return maddress; } public void setMaddress(String maddress) { this.maddress = maddress; } public String getMemail() { return memail; } public void setMemail(String memail) { this.memail = memail; } }
创建数据库帮助类
package Dao_Base; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class Base_Dao { //数据库名和登入密码 String driver="oracle.jdbc.driver.OracleDriver"; String url="jdbc:oracle:thin:@localhost:1521:ORCL"; String user = "epet"; String pwd = "123456"; //建立数据库连接方法 public Connection getConnection(){ //加载驱动 try { //第一步:加载驱动 Class.forName(driver); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } Connection con =null; //获取连接对象 try { con =DriverManager.getConnection(url,user,pwd); //连接数据库 } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return con; } //======释放资源方法======= public void ShiFang(ResultSet rs, Statement st,Connection con){ //如果结果集不为空,则释放成功 ,否则失败 try { if(rs!=null){ rs.close(); }if(st!=null){ st.close(); }if(con!=null){ con.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
第三步:三层架构(业务逻辑层+数据访问层+实现层)
package Dao; import java.util.List; import entity.Club_Name; public interface Club_Name_Select { //创建查询类所有人的位子的情况 public List<Club_Name> Culb(); //创建根据ID查询内容的方法 public List<Club_Name> Culb2(String id); //根据id进行修改 public String Culd3(String id,String mname,String mgender,String mage,String maddress,String memail ); }
package Dao_Impl; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import entity.Club_Name; import Dao.Club_Name_Select; import Dao_Base.Base_Dao; public class Club_Name_Impl extends Base_Dao implements Club_Name_Select{ //创建数据库连接对象 Connection con=this.getConnection(); //创建写入sql语句的参数 PreparedStatement ps=null; //创建结果集接收 ResultSet rs=null; //创建sql语句 String sql=""; public List<Club_Name> Culb() { //创建数据库查询 sql="select * from memberisnfo"; //创建实体类集合接收 List<Club_Name> list=new ArrayList<Club_Name>(); try { //写入sql ps=con.prepareStatement(sql); //执行sql rs=ps.executeQuery(); //循环输出并存入集合装入实体类 while(rs.next()){ //实例化实体类对象 Club_Name name=new Club_Name(); //开始存入 name.setId(rs.getString (1)); name.setMname(rs.getString(2)); name.setMgender(rs.getString(3)); name.setMage(rs.getString (4)); name.setMaddress(rs.getString(5)); name.setMemail(rs.getString(6)); //将查询的对象存入集合中去 list.add(name); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return list; } public List<Club_Name> Culb2(String id) { //创建数据库查询 sql="select * from memberisnfo where id=?"; //创建实体类集合接收 List<Club_Name> list=new ArrayList<Club_Name>(); try { //写入sql ps=con.prepareStatement(sql); ps.setString (1, id); //执行sql rs=ps.executeQuery(); //循环输出并存入集合装入实体类 while(rs.next()){ //实例化实体类对象 Club_Name name=new Club_Name(); //开始存入 name.setId(rs.getString (1)); name.setMname(rs.getString(2)); name.setMgender(rs.getString(3)); name.setMage(rs.getString (4)); name.setMaddress(rs.getString(5)); name.setMemail(rs.getString(6)); //将查询的对象存入集合中去 list.add(name); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return list; } public String Culd3(String id,String mname, String mgender, String mage, String maddress, String memail) { //创建数据库查询 sql="update memberisnfo set mname=?,mgender=?,mage=?,maddress=?,memail=? where id=?"; int q=0; try { //写入sql ps=con.prepareStatement(sql); ps.setString(1, mname); ps.setString(2, mgender); ps.setString (3, mage); ps.setString(4, maddress); ps.setString(5, memail); ps.setString (6, id); //执行sql q=ps.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(q>0){ return "ok"; }else{ return "no"; } } }
package servlet; import java.io.IOException; import java.io.UnsupportedEncodingException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import Dao_Impl.Club_Name_Impl; public class Club_Name extends HttpServlet{ //创建post提交方法 public void doPost(HttpServletRequest request,HttpServletResponse response){ try { //获取传过来的id值 String id=request.getParameter("id"); id=new String(id.getBytes("ISO-8859-1"),"utf-8"); //获取传过来的性别值 String sex=(String)request.getParameter("va"); sex=new String(sex.getBytes("ISO-8859-1"),"utf-8"); //获取名字的默认值 String name=request.getParameter("name"); name=new String(name.getBytes("ISO-8859-1"),"utf-8"); //获取年龄 String Mage=request.getParameter("year"); Mage=new String (Mage.getBytes("ISO-8859-1"),"utf-8"); //获取家庭住址 String Maddress=request.getParameter("location"); Maddress=new String(Maddress.getBytes("ISO-8859-1"),"utf-8"); //获取家庭住址 String Email=request.getParameter("email"); Email=new String(Email.getBytes("ISO-8859-1"),"utf-8"); //实例化更新对象 Club_Name_Impl impl=new Club_Name_Impl(); //调用更新的方法 String update=impl.Culd3(id, name, sex, Mage, Maddress, Email); request.setAttribute("update", update); //传送值到首页 request.getRequestDispatcher("index.jsp").forward(request, response); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ServletException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
配置web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!--接收需要提交的值--> <servlet> <servlet-name>Club_Name</servlet-name> <servlet-class>servlet.Club_Name</servlet-class> </servlet> <!-- 映射servlet --> <servlet-mapping> <servlet-name>Club_Name</servlet-name> <url-pattern>/Club_Name</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
jsp页面显示层
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@page import="Dao_Impl.Club_Name_Impl"%> <%@page import="entity.Club_Name"%> <% //实例化查询对象 Club_Name_Impl impl=new Club_Name_Impl(); //利用集合接收对象 List<Club_Name> list=impl.Culb(); //接收传过来的结果值 String update=(String)request.getAttribute("update"); //开始判断 if("ok".equals(update)){ %> <script type="text/javascript"> alert("修改成功!!!"); </script> <% }else if("no".equals(update)){ %> <script type="text/javascript"> alert("修改失败!!!"); </script> <% }else{ } %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>查询俱乐部会员信息</title> </head> <body> <div style="width:800px;margin:auto;border:0px solid red"> <table border="1px solid red" width="800px" style="margin:auto"> <tr style="background:#A5A4A7"> <td colspan="6"><center><b>俱乐部会员信息号</b></center></td> </tr> <tr style="background:#A5A4A7"> <td><center><b>编号</b></center></td> <td><center><b>姓名</b></center></td> <td><center><b>姓名</b></center></td> <td><center><b>年龄</b></center></td> <td><center><b>家庭住址</b></center></td> <td><center><b>Email</b></center></td> </tr> <% //传出参数并进行赋值调用 for(int i=0;i<list.size();i++){ //实例化实体类 Club_Name room=list.get(i); %> <tr> <td><center><a href="Club_Name_Amend.jsp?id=<%=room.getId() %>"><%=room.getId() %></a></center></td> <td><center><%=room.getMname()%></center></td> <td><center><%=room.getMgender() %></center></td> <td><center><%=room.getMage() %></center></td> <td><center><%=room.getMaddress() %></center></td> <td><center><%=room.getMemail() %></center></td> </tr> <% } %> </table> </div> </body> </html>
需改页面:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@page import="Dao_Impl.Club_Name_Impl"%> <%@page import="entity.Club_Name"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <% //获取接收过来的id值 String id=request.getParameter("id"); //吧获取的id值进行转发 request.setAttribute("id",id); //实例化查询对象 Club_Name_Impl impl=new Club_Name_Impl(); //利用集合接收对象 List<Club_Name> list=impl.Culb2(id); %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>修改页面</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"> </head> <body> <div style="width:400px;margin:auto;border:0px solid red"> <form action="Club_Name?id=<%=id %>" method="post"> <table border="1px solid red" width="400px" style="background:#A5A4A7;margin:auto"> <tr > <td colspan="2"><center><b>会员信息</b></center></td> </tr> <% for(int i=0;i<list.size();i++){ //实例化实体类 Club_Name room=list.get(i); %> <tr> <td ><center><b>姓名</b></center></td> <td ><b><input type="text" value="<%=room.getMname()%>" name="name"></b></td> </tr> <tr> <td ><center><b>性别</b></center></td> <td ><input type="radio" value="男" name="va" id="va1" />男 <input type="radio" value="女" name="va" id="va2"/>女</td> </tr> <tr> <td ><center><b>年龄</b></center></td> <td ><b><input type="text" value="<%=room.getMage() %> " name="year"/></b></td> </tr> <tr> <td ><center><b>家庭住址</b></center></td> <td ><b><input type="text" value="<%=room.getMaddress() %>" name="location"></b></td> </tr> <tr> <td ><center><b>Email</b></center></td> <td ><b><input type="text"value="<%=room.getMemail() %>" name="email"></b></td> </tr> <script type="text/javascript"> //获取男的按钮的值 var name=document.getElementById("va1"); //获取女的按钮值 var name1=document.getElementById("va2"); //判断是男或者女 if(name.value=="<%=room.getMgender()%>"){ name.checked="checked"; }else{ name1.checked="checked"; } </script> <%} %> <tr> <td colspan="2"><center><b><input type="submit" value="提交"/> <input type="reset" value="重置"/></b></center></td> </tr> </table> </form> </div> </body> </html>
责任重于泰山,任重而道远。