资源描述
学生信息管理系统JSP版(Eclipse)
1、 新建new->Dynamic Web Project->名字为StudentJSP。
2、 准备工作
1) 编写JSP测试文件,名称为hello.jsp
修改hello.jspcharset为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" "">
<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-8
2) 右击项目->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" "">
<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"; //表明为studentinfo
try {
rs = stmt.executeQuery(sql);
%>
<table align=center width=600 border=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 block
e.printStackTrace();
}
%>
%>
</body>
</html>
4、 在项目下新建一个文件夹DataBase,将用到学生数据库脚本文件复制到此目录下
5、 开启mysql,Tomcat],运行测试display0.
6、 改善版本display1
1)将display0.jsp复制粘贴到本项目标/WebCont下并修改命名为display1.jsp
2)在/src下new一个新包,包名为com.ls.student.bean
并在此包下新建两个新类来处理SQLHelper。
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;
this.name = 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) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public 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" "">
<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=600 border=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><a href="update.jsp?id=<%=rs.getString("id") %>" >修改 </a>
<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" "">
<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");
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/student" ,"root","123456");
Statement stmt= conn.createStatement();
int result= stmt.executeUpdate(sql);
if(result>0)
out.println("添加统计成功!");
else out.println("添加统计失败!");
stmt.close();
conn.close();
%>
</body>
</html>
和插入html显示页面,代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<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" "">
<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");
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/student" ,"root","123456");
Statement stmt= conn.createStatement();
int result= stmt.executeUpdate(sql);
if(result>0)
out.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" "">
<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())
sql += " and id='"+ id+"'";
if(!name.isEmpty())
sql += " and name='"+ name+"'";
if(!minAge.isEmpty())
sql += " and age>="+ minAge;
if(!maxAge.isEmpty())
sql += " and age<="+ maxAge;
Class.forName("com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/student" ,"root","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><a href="update.jsp?id=<%=rs.getString("id") %>" >修改 </a>
<a href="delete.jsp?id=<%=rs.getString("id") %>"> 删除 </a></div></td> </tr>
<% }
rs.close();stmt.close();conn.close();
%>
</table>
</body>
</html>
Queryhtml页面:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<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" "">
<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())
sql += " and id='"+ id+"'";
if(!name.isEmpty())
sql += " and name='"+ name+"'";
if(!minAge.isEmpty())
sql += " and age>="+ minAge;
if(!maxAge.isEmpty())
sql += " and age<="+ maxAge;
Class.forName("com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/student" ,"root","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><a href="update.jsp?id=<%=rs.getString("id") %>" >修改 </a>
<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" "">
<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");
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/student" ,"root","123456");
Statement stmt= conn.createStatement();
int result= stmt.executeUpdate(sql);
if(result>0)
out.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" "">
<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");
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/student" ,"root","123456");
Statement st
展开阅读全文