JSP学生信息管理系统设计
本文实例为大家分享了JSP学生信息管理系统源码,供大家参考,具体内容如下
新建学生信息数据库
1.添加记录模块
<%@pagecontentType="text/html"pageEncoding="UTF-8"%>添加新学生 学号: 姓名 性别 年龄 专业
<%@pagelanguage="java"import="java.sql.*"pageEncoding="UTF-8"%>添加学生处理程序 <%Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); Stringurl="jdbc:sqlserver://localhost:1433;DatabaseName=p"; Stringuser="sa"; Stringpassword="123456"; Connectionconn=DriverManager.getConnection(url,user,password); request.setCharacterEncoding("UTF-8"); Stringid=request.getParameter("id"); Stringname=request.getParameter("name"); Stringsex=request.getParameter("sex"); intage=Integer.parseInt(request.getParameter("age")); Stringsdept=request.getParameter("sdept"); Stringsql=String.format("select*fromstudentwhereSno=%s",id); Statementstmt=conn.createStatement(); PreparedStatementpstmt; ResultSetrs=stmt.executeQuery(sql); if(!(rs.next())){ pstmt=conn.prepareStatement("insertintoStudentvalues(?,?,?,?,?)"); pstmt.setString(1,id); pstmt.setString(2,name); pstmt.setString(3,sex); pstmt.setInt(4,age); pstmt.setString(5,sdept); pstmt.executeUpdate(); out.print("添加成功"); %>返回菜单
<%}else{ out.println("该学号已存在");%> 重新添加 <%}%>
2.查询(all)模块
<%@pagelanguage="java"import="java.sql.*"pageEncoding="UTF-8"%> <%@pageimport="javax.sql.*"%> <%@pageimport="javax.naming.*"%>信息查看页面 <%DataSourceds=null; InitialContextctx=newInitialContext(); ds=(DataSource)ctx.lookup("java:comp/env/jdbc/sqlserver"); Connectionconn=ds.getConnection(); PreparedStatementpstmt=conn.prepareStatement("select*fromStudent"); ResultSetrs=pstmt.executeQuery(); %> <%if(rs!=null){rs.close();} if(pstmt!=null){pstmt.close();} if(conn!=null){conn.close();} %>记录条数 学号 姓名 性别 年龄 专业 <% while(rs.next()){ %><%=rs.getRow()%> <%=rs.getString(1)%> <%=rs.getString(2)%> <%=rs.getString(3)%> <%=rs.getString(4)%> <%=rs.getString(5)%> <%}%>
条件查询模块
<%@pagelanguage="java"pageEncoding="UTF-8"%>MyJSP'stufind2.jsp'startingpage 请选择查询条件性别 男 女 年龄范围: 最小:
最大:
<%@pagecontentType="text/html"import="java.sql.*"pageEncoding="UTF-8"%>条件查询处理页面 <%Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); Stringuser="sa"; Stringpassword="123456"; Stringurl="jdbc:sqlserver://localhost:1433;DatabaseName=p"; Connectionconn=DriverManager.getConnection(url,user,password); request.setCharacterEncoding("UTF-8"); Stringsex=request.getParameter("sex"); intage1=Integer.parseInt(request.getParameter("a1")); intage2=Integer.parseInt(request.getParameter("a2")); Stringsql="select*fromStudentwhereSsex=?andSage>=?andSage<=?"; PreparedStatementpstmt=conn.prepareStatement(sql); pstmt.setString(1,sex); pstmt.setInt(2,age1); pstmt.setInt(3,age2); ResultSetrs=pstmt.executeQuery(); %> 记录条数 学号 姓名 性别 年龄 专业 <%while(rs.next()){%><%=rs.getRow()%> <%=rs.getString(1)%> <%=rs.getString(2)%> <%=rs.getString(3)%> <%=rs.getString(4)%> <%=rs.getString(5)%> <%}%> <%if(rs!=null){rs.close();} if(pstmt!=null){pstmt.close();} if(conn!=null){conn.close();} %>
3.修改模块
<%@pagelanguage="java"import="java.util.*"pageEncoding="UTF-8"%>修改提交页面 请选择修改记录所满足的条件 姓名:
性别:男女
<%@pagecontentType="text/html"import="java.sql.*"pageEncoding="UTF-8"%>修改编辑页面 <%Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); Stringuser="sa"; Stringpassword="123456"; Stringurl="jdbc:sqlserver://localhost:1433;DatabaseName=p"; Connectionconn=DriverManager.getConnection(url,user,password); request.setCharacterEncoding("UTF-8"); Stringname=request.getParameter("name"); Stringsex=request.getParameter("sex"); session.setAttribute("sex",sex); session.setAttribute("name",name); Stringsql="select*fromStudentwhereSsex=?andSname=?"; PreparedStatementpstmt=conn.prepareStatement(sql); pstmt.setString(1,sex); pstmt.setString(2,name); ResultSetrs=pstmt.executeQuery(); if(rs.next()){ Stringid=rs.getString("Sno"); Stringname2=rs.getString("Sname"); Stringsex2=rs.getString("Ssex"); intage=rs.getInt("Sage"); Stringsdept=rs.getString("Sdept"); if(pstmt!=null){pstmt.close();} if(rs!=null){rs.close();} if(conn!=null){conn.close();} %>学号: > 姓名: > 性别: > 年龄: > 专业: > <%} else{%> 没有找到合适条件的记录!!<% if(rs!=null){rs.close();} if(pstmt!=null){pstmt.close();} if(conn!=null){conn.close();} }%>
<%@pagelanguage="java"import="java.sql.*"pageEncoding="UTF-8"%>修改后重写页面 <%Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); Stringurl="jdbc:sqlserver://localhost:1433;DatabaseName=p"; Stringuser="sa"; Stringpassword="123456"; Connectionconn=DriverManager.getConnection(url,user,password); Stringsql="updateStudentsetSno=?,Sname=?,Ssex=?,Sage=?,Sdept=?whereSname=?andSsex=?"; PreparedStatementpstmt=conn.prepareStatement(sql); request.setCharacterEncoding("UTF-8"); Stringid=request.getParameter("id"); Stringname2=request.getParameter("name2"); Stringsex2=request.getParameter("sex2"); intage=Integer.parseInt(request.getParameter("age")); Stringsdept=request.getParameter("sdept"); Stringname=(String)session.getAttribute("name"); Stringsex=(String)session.getAttribute("sex"); pstmt.setString(1,id);pstmt.setString(2,name2); pstmt.setString(3,sex2);pstmt.setInt(4,age); pstmt.setString(5,sdept);pstmt.setString(6,name); pstmt.setString(7,sex); intn=pstmt.executeUpdate(); if(n>=1){%>重写数据库操作成功!
<%} else{%>重写数据库操作失败!
<%} if(pstmt!=null){pstmt.close();} if(conn!=null){conn.close();} %>
4.删除记录模块
<%@pagelanguage="java"import="java.util.*"pageEncoding="UTF-8"%>条件删除提交页面 请选择删除记录条件<姓名:
性别:男女
年龄范围:最小:
最大:
<%@pagelanguage="java"import="java.sql.*"pageEncoding="UTF-8"%>条件删除处理页面 <%Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); Stringuser="sa"; Stringpassword="123456"; Stringurl="jdbc:sqlserver://localhost:1433;DatabaseName=p"; Connectionconn=DriverManager.getConnection(url,user,password); request.setCharacterEncoding("UTF-8"); Stringname=request.getParameter("name"); Stringsex=request.getParameter("sex"); Stringag1=request.getParameter("a1"); Stringag2=request.getParameter("a2"); inta3,a4; Stringsql="deletefromStudentwhereSname=?andSsex=?andSage>=?andSage<=?"; PreparedStatementpstmt=conn.prepareStatement(sql); if(!name.equals(""))pstmt.setString(1,name); if(sex!=null)pstmt.setString(2,sex); if(!ag1.equals("")){a3=Integer.parseInt(ag1);pstmt.setInt(3,a3);} if(!ag2.equals("")){a4=Integer.parseInt(ag2);pstmt.setInt(4,a4);} intn=pstmt.executeUpdate(); if(n==1){%>数据删除成功
<%} else{%>数据删除失败!
<%} if(pstmt!=null){pstmt.close();} if(conn!=null){conn.close();} %>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。