当前位置:文档之家› 学生信息管理系统JSP版(Eclipse)

学生信息管理系统JSP版(Eclipse)

学生信息管理系统JSP版(Eclipse)1、新建new->Dynamic Web Project->名字为StudentJSP。

2、准备工作1)编写JSP测试文件,名称为hello.jsp修改hello.jsp的charset为UTF-8加入代码:<body>Hello!</body>代码:<%@page language="java"contentType="text/html; charset=UTF-8"pageEncoding=" UTF-8"%><!DOCTYPE html PUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN" "/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type"content="text/html; charset=UTF-8"> <title>Insert title here</title></head><body>Hello!</body></html>附:修改默认字符集步骤:Windows->Preference->Web->JSPfiles->将Encoding改为UTF-82)右击项目->Properties->Resurce->修改字符集为UTF-8.3)右击项目->Properties->Java Build Path改为如图(需先在WEB-INF下新建classes)4)将MySQL中一个jar包复制到/WEB-INF/lib下,如图:3、在/WebContent下new一个JSPfile,名字为display0.jsp代码如下:<%@page language="java"contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><%@page import="java.sql.*"%> //(注意要引入sql的包)<!DOCTYPE html PUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN""/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type"content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><%String connStr ="jdbc:mysql://localhost:3306/yStudent?characterEncoding=utf8";Connection conn = null;Statement stmt = null;ResultSet rs;try {Class.forName("com.mysql.jdbc.Driver");conn = DriverManager.getConnection(connStr, "root", "123456");stmt = conn.createStatement();} catch (Exception ex) {ex.printStackTrace();System.out.println("exception in getConnection");}String sql = "select * from studentinfo"; //表明为studentinfotry {rs = stmt.executeQuery(sql);%><table align=center width=600border=1><tr><th>编号</th><th>姓名</th><th>年龄</th></tr><%while (rs.next()) { %><tr><td><%= rs.getString("id")%></td><td><%= rs.getString("name")%></td><td><%= rs.getInt("age") %></td></tr><%} %></table><%stmt.close();conn.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}%>%></body></html>4、在项目下新建一个文件夹DataBase,将用到的学生数据库脚本文件复制到此目录下5、启动mysql,Tomcat],运行测试display0.6、改进版本display11)将display0.jsp复制粘贴到本项目的/WebCont下并修改命名为display1.jsp2)在/src下new一个新包,包名为com.ls.student.bean并在此包下新建两个新类来处理SQL的Helper。

SQLHelper.java代码如下:package com.ls.student.bean;import java.sql.*; //引入sql相关包public class SQLHelper {private String driverStr = "com.mysql.jdbc.Driver";private String connStr ="jdbc:mysql://localhost:3306/yStudent?characterEncoding=utf8";private String dbusername = "root";private String dbpassword = "123456";private Connection conn = null;private Statement stmt = null;public SQLHelper() {try {Class.forName(driverStr);conn = DriverManager.getConnection(connStr, dbusername, dbpassword);stmt = conn.createStatement();} catch (Exception ex) {System.out.println("exception in SQLHelper");}}public int executeUpdate(String sql) {int result = 0;try {result = stmt.executeUpdate(sql);} catch (Exception ex) {System.out.println("Exception in executeUpdate");}return result;}public ResultSet executeQuery(String sql) {ResultSet rs = null;try {rs = stmt.executeQuery(sql);if(!rs.next()) return null;rs.previous();} catch (Exception ex) {System.out.println("Exception in executeQuery");}return rs;}public void close() {try {stmt.close();conn.close();} catch (Exception e) {}}}在此包下新建student类,代码如下package com.ls.student.bean;public class student {private String id,name;private int age;public student() {super();}public student(String id, String name, int age) {super();this.id = id; = name;this.age = age;}public String getId() {return id;}public void setId(String id) {this.id = id;}public String getName() {return name;}public void setName(String name) { = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}@Overridepublic String toString() {return"Student [id=" + id + ", name=" + name + ", age=" + age + "]";}}3)display1.jsp代码修改如下:注意先引入java.sql.*包和com.ls.student.bean包主要代码修改为<body></body>内的修改,代码如下:<%@page language="java"contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><%@page import="java.sql.*;import com.ls.student.bean.*"%> //引入包<!DOCTYPE html PUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN""/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type"content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><%SQLHelper db= new SQLHelper();String sql = "select * from studentinfo";ResultSet rs = db.executeQuery(sql);%><table align=center width=600border=1><tr><th>编号</th><th>姓名</th><th>年龄</th><th>操作</th></tr> <%while (rs.next()) { %><tr><td><%= rs.getString("id")%></td><td><%= rs.getString("name")%></td><td><%= rs.getInt("age") %></td><td><div align=center><ahref="update.jsp?id=<%=rs.getString("id") %>">修改</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="delete.jsp?id=<%=rs.getString("id") %>">删除</a></div></td></tr><%} %></table><% db.close();%></body></html>7、实现增加功能新建insert.jsp文件<%@page language="java"contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><%@page import="java.sql.*"%><!DOCTYPE html PUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN""/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type"content="text/html; charset=UTF-8"><title>Insert student</title></head><body><%@include file="head.jsp"%><%request.setCharacterEncoding("UTF-8");String id=request.getParameter("id");String name=request.getParameter("name");int age=Integer.parseInt(request.getParameter("age"));String sql="insert into studentinfo values('"+id+"','"+ name+"',"+ age+")";Class.forName("com.mysql.jdbc.Driver");Connectionconn=DriverManager.getConnection("jdbc:mysql://localhost:3306/student" ,"ro ot","123456");Statement stmt= conn.createStatement();int result= stmt.executeUpdate(sql);if(result>0)o ut.println("添加记录成功!");else out.println("添加记录失败!");stmt.close();conn.close();%></body></html>以及插入的html显示页面,代码:<!DOCTYPE html PUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN" "/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type"content="text/html; charset=UTF-8"> <title>Insert title here</title></head><body><div align="center"><h2><strong>请输入学生信息</strong></h2></div><form method="post"action="insert.jsp"><table width="200"border="0"align="center"><tr><td>学号</td><td><input type="text"name="id"id="id"/></td></tr><tr><td>姓名</td><td><input type="text"name="name"id="name"/></td></tr><tr><td>年龄</td><td><input type="text"name="age"id="age"/></td></tr><tr><td><input type="submit"value="提交"/></td><td align="center"><input type="reset"value="重置"/></td> </tr></table></form></body></html>9、删除实现,delete.jsp<%@page language="java"contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><%@page import="java.sql.*"%><!DOCTYPE html PUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN""/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type"content="text/html; charset=UTF-8"><title>delete student</title></head><body><%@include file="head.jsp"%><%request.setCharacterEncoding("UTF-8");String id=request.getParameter("id");String sql="delete from studentinfo where id='"+id+"'";Class.forName("com.mysql.jdbc.Driver");Connectionconn=DriverManager.getConnection("jdbc:mysql://localhost:3306/student" ,"ro ot","123456");Statement stmt= conn.createStatement();int result= stmt.executeUpdate(sql);if(result>0)o ut.println("删除记录成功!");else out.println("删除记录失败!");stmt.close();conn.close();%></body></html>10、查找query.jsp<%@page language="java"contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><%@page import="java.sql.*"%><!DOCTYPE html PUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN""/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type"content="text/html; charset=UTF-8"><title>query result</title></head><body><%@include file="head.jsp"%>request.setCharacterEncoding("UTF-8");String id=request.getParameter("id");String name=request.getParameter("name");String minAge=request.getParameter("minAge");String maxAge=request.getParameter("maxAge");String sql="select * from studentinfo where true ";if(!id.isEmpty())s ql += " and id='"+ id+"'";if(!name.isEmpty())s ql += " and name='"+ name+"'";if(!minAge.isEmpty())s ql += " and age>="+ minAge;if(!maxAge.isEmpty())s ql += " and age<="+ maxAge;Class.forName("com.mysql.jdbc.Driver");Connectionconn=DriverManager.getConnection("jdbc:mysql://localhost:3306/student" ,"ro ot","123456");Statement stmt= conn.createStatement();ResultSet rs= stmt.executeQuery(sql);%><table align="center"border="1"width="600"><tr><th bgcolor=#cccccc>编号</th><th>姓名</th><th>年龄</th><th>操作</th></tr><%while (rs.next()) {%><tr><td><%=rs.getString("id") %></td><td><%=rs.getString("name") %></td><td><%=rs.getInt("age") %></td><td><div align=center><ahref="update.jsp?id=<%=rs.getString("id") %>">修改</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="delete.jsp?id=<%=rs.getString("id") %>">删除</a></div></td></tr><% }rs.close();stmt.close();conn.close();%></table></body></html>Query的html页面:<!DOCTYPE html PUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN" "/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type"content="text/html; charset=UTF-8"> <title>Insert title here</title></head><body><div align="center"><h2><strong>请输入查询条件</strong></h2></div><form method="post"action="query.jsp"><table width="200"border="0"align="center"><tr><td>学号</td><td><input type="text"name="id"id="id"/></td></tr><tr><td>姓名</td><td><input type="text"name="name"id="name"/></td></tr><tr><td>最小年龄</td><td><input type="text"name="minAge"id="class"/></td></tr><tr><td>最大年龄</td><td><input type="text"name="maxAge"id="age"/></td></tr><tr><td><input type="submit"value="提交"/></td><td align="center"><input type="reset"value="重置"/></td> </tr></table></form></body></html>11、查询的query.jsp<%@page language="java"contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><%@page import="java.sql.*"%><!DOCTYPE html PUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN""/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type"content="text/html; charset=UTF-8"><title>query result</title></head><body><%@include file="head.jsp"%><%request.setCharacterEncoding("UTF-8");String id=request.getParameter("id");String name=request.getParameter("name");String minAge=request.getParameter("minAge");String maxAge=request.getParameter("maxAge");String sql="select * from studentinfo where true ";if(!id.isEmpty())s ql += " and id='"+ id+"'";if(!name.isEmpty())s ql += " and name='"+ name+"'";if(!minAge.isEmpty())s ql += " and age>="+ minAge;if(!maxAge.isEmpty())s ql += " and age<="+ maxAge;Class.forName("com.mysql.jdbc.Driver");Connectionconn=DriverManager.getConnection("jdbc:mysql://localhost:3306/student" ,"ro ot","123456");Statement stmt= conn.createStatement();ResultSet rs= stmt.executeQuery(sql);%><table align="center"border="1"width="600"><tr><th bgcolor=#cccccc>编号</th><th>姓名</th><th>年龄</th><th>操作</th></tr><%while (rs.next()) {%><tr><td><%=rs.getString("id") %></td><td><%=rs.getString("name") %></td><td><%=rs.getInt("age") %></td><td><div align=center><ahref="update.jsp?id=<%=rs.getString("id") %>">修改</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="delete.jsp?id=<%=rs.getString("id") %>">删除</a></div></td></tr><% }rs.close();stmt.close();conn.close();%></table></body></html>12、修改的update.jsp,<%@page language="java"contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><%@page import="java.sql.*"%><!DOCTYPE html PUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN""/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type"content="text/html; charset=UTF-8"><title>update the result</title></head><body><%@include file="head.jsp"%><%request.setCharacterEncoding("UTF-8");String id=request.getParameter("id");String name=request.getParameter("name");// String className=request.getParameter("class");int age=Integer.parseInt(request.getParameter("age"));String sql="update studentinfo set name='"+ name+"',age="+ age;Class.forName("com.mysql.jdbc.Driver");Connectionconn=DriverManager.getConnection("jdbc:mysql://localhost:3306/student" ,"ro ot","123456");Statement stmt= conn.createStatement();int result= stmt.executeUpdate(sql);if(result>0)o ut.println("更新记录成功!");else out.println("更新记录失败!");stmt.close();conn.close();%></body></html>以及update1.jsp<%@page language="java"contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><%@page import="java.sql.*"%><!DOCTYPE html PUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN""/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type"content="text/html; charset=UTF-8"><title>update the result</title></head><body><%@include file="head.jsp"%><%request.setCharacterEncoding("UTF-8");String id=request.getParameter("id");String name=request.getParameter("name");// String className=request.getParameter("class");int age=Integer.parseInt(request.getParameter("age"));String sql="update studentinfo set name='"+ name+"',age="+ age;Class.forName("com.mysql.jdbc.Driver");Connectionconn=DriverManager.getConnection("jdbc:mysql://localhost:3306/student" ,"ro ot","123456");Statement stmt= conn.createStatement();int result= stmt.executeUpdate(sql);if(result>0)o ut.println("更新记录成功!");else out.println("更新记录失败!");stmt.close();conn.close();%></body></html>12、head.jsp<%@page language="java"contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><div align="center"style="height:120px;background-image:url('images/gxu.jpg');"></div><hr><div align="center"style="height:30;background-color:#ccc;border:solid;color:red;"><a href="insert.html">增加记录</a><a href="query.html">查询记录</a></div><hr>13、footer.jsp<TABLE align="center"id="bottom"cellSpacing=0cellPadding=0height="50" border=0width="100%"bgcolor="f5f5f5"style="border-top:#068500 solid 0px;"> <TR><TD style="font-family:宋体;font-size:12px;color:#002200"align="center">Copyright &copy; 2011 - 2012&nbsp;<A style="vertical-align:middle;font-size:12px;color:#002200"href="/">数学与信息科学学院</A>&nbsp;All Rights Reserved</TD></TR></TABLE>测试:测试Hello页面:Display0.jsp(测试文件):Display1.jsp。

相关主题