运行截图:
文件结构:
代码如下
bean包
public class StudentInfo {
private Integer id;
private String name, sex;
private Integer age;
private Float weight, height;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Float getWeight() {
return weight;
}
public void setWeight(Float weight) {
this.weight = weight;
}
public Float getHeight() {
return height;
}
public void setHeight(Float height) {
this.height = height;
}
}
public class Users {
private String username, userpwd, usertype;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUserpwd() {
return userpwd;
}
public void setUserpwd(String userpwd) {
this.userpwd = userpwd;
}
public String getUsertype() {
return usertype;
}
public void setUsertype(String usertype) {
this.usertype = usertype;
}
}
DAO包
public class StudentDao {
public int add(StudentInfo user) throws Exception {
Connection conn = null;
PreparedStatement pst = null;
try {
conn = JdbcUtil.getConnection();
String sql = "insert into stu_info values(?,?,?,?,?,?)";
;
pst = conn.prepareStatement(sql);
pst.setInt(1, user.getId());
pst.setString(2, user.getName());
pst.setString(3, user.getSex());
pst.setInt(4, user.getAge());
pst.setFloat(5, user.getWeight());
pst.setFloat(6, user.getWeight());
int n = pst.executeUpdate();
return n;
} finally {
JdbcUtil.free(null, pst, conn);
}
}
public int update(StudentInfo user) throws Exception {
Connection conn = null;
PreparedStatement ps = null;
try {
conn = JdbcUtil.getConnection();
String sql = "update stu_info set id=?,name=?,sex=?,age=?,weight=?,height=? where id=? ";
ps = conn.prepareStatement(sql);
ps.setInt(1, user.getId());
ps.setString(2, user.getName());
ps.setString(3, user.getSex());
ps.setInt(4, user.getAge());
ps.setFloat(5, user.getWeight());
ps.setFloat(6, user.getHeight());
ps.setInt(7, user.getId());
int n = ps.executeUpdate();
return n;
} finally {
JdbcUtil.free(null, ps, conn);
}
}
public StudentInfo findUserById(StudentInfo sInfo) throws Exception {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
StudentInfo sInfo2 = new StudentInfo();
try {
conn = JdbcUtil.getConnection();
String sql = "select * from stu_info where id=? ";
ps = conn.prepareStatement(sql);
ps.setInt(1, sInfo.getId());
rs = ps.executeQuery();
while (rs.next()) {
sInfo2.setId(rs.getInt(1));
sInfo2.setName(rs.getString(2));
sInfo2.setSex(rs.getString(3));
sInfo2.setAge(rs.getInt(4));
sInfo2.setHeight(rs.getFloat(5));
sInfo2.setWeight(rs.getFloat(6));
}
return sInfo2;
} finally {
JdbcUtil.free(null, null, null);
}
}
public List<StudentInfo> findUserById2(String sql1) throws Exception {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
List<StudentInfo> list = new ArrayList<StudentInfo>();
try {
conn = JdbcUtil.getConnection();
String sql = "select * from stu_info where 1=1" + sql1;
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
StudentInfo sInfo2 = new StudentInfo();
sInfo2.setId(rs.getInt(1));
sInfo2.setName(rs.getString(2));
sInfo2.setSex(rs.getString(3));
sInfo2.setAge(rs.getInt(4));
sInfo2.setHeight(rs.getFloat(5));
sInfo2.setWeight(rs.getFloat(6));
list.add(sInfo2);
}
return list;
} finally {
JdbcUtil.free(null, ps, conn);
}
}
public int deleteById(StudentInfo name) throws Exception {
Connection conn = null;
PreparedStatement ps = null;
try {
conn = JdbcUtil.getConnection();
String sql = "delete from stu_info where name like '%" + name.getName() + "%'";
ps = conn.prepareStatement(sql);
int n = ps.executeUpdate();
return n;
} finally {
JdbcUtil.free(null, ps, conn);
}
}
}
package Dao;
import dbc.JdbcUtil;
import java.sql.*;
import bean.Users;
public class UsersDao {
public int add(Users user) throws Exception {
Connection conn = null;
PreparedStatement ps = null;
try {
conn = JdbcUtil.getConnection();
String sql = "insert into users values (?,?,?) ";
ps = conn.prepareStatement(sql);
ps.setString(1, user.getUsername());
ps.setString(2, user.getUserpwd());
ps.setString(3, user.getUsertype());
int n = ps.executeUpdate();
return n;
} finally {
JdbcUtil.free(null, ps, conn);
}
}
public int update(String a, String b) throws Exception {
Connection conn = null;
PreparedStatement ps = null;
int n = 0;
try {
conn = JdbcUtil.getConnection();
String sql = "update users set userpwd=? where username=? ";
ps = conn.prepareStatement(sql);
ps.setString(1, a);
ps.setString(2, b);
ps.executeUpdate();
n++;
} finally {
JdbcUtil.free(null, ps, conn);
}
return n;
}
public int deleteById(String userId) throws Exception {
Connection conn = null;
PreparedStatement ps = null;
try {
conn = JdbcUtil.getConnection();
String sql = "delete from user where userid=?";
ps = conn.prepareStatement(sql);
ps.setString(1, userId);
int n = ps.executeUpdate();
return n;
} finally {
JdbcUtil.free(null, ps, conn);
}
}
public int findUserById(Users users) throws Exception {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
int n = 0;
try {
conn = JdbcUtil.getConnection();
String sql = "select * from users where username=? and userpwd=? and usertype=? ";
ps = conn.prepareStatement(sql);
ps.setString(1, users.getUsername());
ps.setString(2, users.getUserpwd());
ps.setString(3, users.getUsertype());
rs = ps.executeQuery();
if (rs.next()) {
n = 1;
}
return n;
} finally {
JdbcUtil.free(rs, ps, conn);
}
}
}
jdbc包
package jdbc;
import java.sql.*;
import java.util.Properties;
public class JdbcUtil {
private static String driver;
private static String url;
private static String user;
private static String password;
private static Properties pr = new Properties();
// 设计该工具类的静态初始化器中的代码,该代码在装入类时执行,且只执行一次
static {
try {
pr.load(JdbcUtil.class.getClassLoader().getResourceAsStream("db.properties"));
driver = pr.getProperty("driver");
url = pr.getProperty("url");
user = pr.getProperty("user");
password = pr.getProperty("password");
Class.forName(driver);
} catch (Exception e) {
throw new ExceptionInInitializerError(e);
}
}
// 设计获得连接对象的方法getConnection()
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, user, password);
}
// 设计释放结果集、语句和连接的方法free()
public static void free(ResultSet rs, Statement st, Connection conn) {
try {
if (rs != null)
rs.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (st != null)
st.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (conn != null)
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
servlet包
package servlet;
import java.io.IOException;
import Dao.*;
import bean.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class AddServlet
*/
@WebServlet("/shuju/AddServlet")
public class AddServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public AddServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
int n = 0;
int id = Integer.parseInt(request.getParameter("id"));
String name = request.getParameter("name");
String sex = request.getParameter("sex");
int age = Integer.parseInt(request.getParameter("age"));
float weight = Float.parseFloat(request.getParameter("weight"));
float height = Float.parseFloat(request.getParameter("height"));
StudentInfo student = new StudentInfo();
student.setId(id);
student.setName(name);
student.setSex(sex);
student.setAge(age);
student.setWeight(weight);
student.setHeight(height);
StudentDao studentDao = new StudentDao();
try {
n = studentDao.add(student);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (n == 1) {
request.setAttribute("result", "添加数据成功");
} else {
request.setAttribute("result", "数据添加失败");
}
request.getRequestDispatcher("shujuadd_process.jsp").forward(request, response);
}
}
package servlet;
import java.io.IOException;
import Dao.*;
import bean.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class DeleteServlet
*/
@WebServlet("/shuju/DeleteServlet")
public class DeleteServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public DeleteServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("utf-8");
int n = 0;
String name = request.getParameter("name");
StudentInfo a = new StudentInfo();
a.setName(name);
StudentDao studentDao = new StudentDao();
try {
n = studentDao.deleteById(a);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (n == 1) {
request.setAttribute("result", "删除数据成功");
} else {
request.setAttribute("result", "删除数据失败");
}
request.getRequestDispatcher("shujudelete_process.jsp").forward(request, response);
}
}
package servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import Dao.*;
import bean.*;
/**
* Servlet implementation class LoginServlet
*/
@WebServlet("/shuju/LoginServlet")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public LoginServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("utf-8");
HttpSession session = request.getSession();
String username = request.getParameter("username");
String userpwd = request.getParameter("userpwd");
String usertype = request.getParameter("usertype");
session.setAttribute("login", username);
int n = 0;
Users users = new Users();
users.setUsername(username);
users.setUserpwd(userpwd);
users.setUsertype(usertype);
UsersDao usersDao = new UsersDao();
try {
n = usersDao.findUserById(users);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (n == 1) {
request.setAttribute("outvalue", "登陆成功!!");
response.setHeader("refresh", "1;shujuadd_input.jsp");
} else {
request.setAttribute("outvalue", "登陆失败");
response.setHeader("refresh", "1;login.jsp");
}
request.getRequestDispatcher("show.jsp").forward(request, response);
}
}
package servlet;
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import Dao.UsersDao;
/**
* Servlet implementation class Password
*/
@WebServlet("/shuju/Password")
public class Password extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Password() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("utf-8");
HttpSession session = request.getSession();
String oldpwd = request.getParameter("oldpwd");
String newpwd = request.getParameter("newpwd");
String renewpwd = request.getParameter("renewpwd");
String username = (String) session.getAttribute("login");
String userpwd = (String) session.getAttribute("pwd");
// 判断新密码与确认密码一致
if (newpwd != renewpwd) {
request.setAttribute("result", "输入密码与确认新密码不一致");
response.setHeader("refresh", "1;passwordmodi_input.jsp");
}
// 判断登陆密码与旧密码是否一致
if (userpwd != oldpwd) {
request.setAttribute("result", "旧密码不正确");
response.setHeader("refresh", "1;passwordmodi_input.jsp");
}
UsersDao usersDao = new UsersDao();
int i = 0;
try {
i = usersDao.update(newpwd, username);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (i >= 1) {
request.setAttribute("result", "密码已更新!!请重新登陆!!");
session.removeAttribute("login");
} else {
request.setAttribute("result", "密码更新错误!");
}
request.getRequestDispatcher("passwordmodi_process.jsp").forward(request, response);
}
}
package servlet;
import java.io.IOException;
import Dao.*;
import bean.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class Regist
*/
@WebServlet("/shuju/Regist")
public class Regist extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Regist() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("utf-8");
String username = request.getParameter("username");
String userpwd = request.getParameter("userpwd");
String usertype = request.getParameter("usertype");
int i = 0;
Users users = new Users();
users.setUsername(username);
users.setUserpwd(userpwd);
users.setUsertype(usertype);
UsersDao usersDao = new UsersDao();
try {
i = usersDao.add(users);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (i >= 1) {
request.setAttribute("result", "注册成功,将转入登陆页面");
response.setHeader("refresh", "1;login.jsp");
} else {
request.setAttribute("result", "注册失败请重新注册");
response.setHeader("refresh", "1;regist_input.jsp");
}
request.getRequestDispatcher("regist_process.jsp").forward(request, response);
}
}
package servlet;
import java.io.IOException;
import java.util.*;
import Dao.*;
import bean.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class SelectServlet
*/
@WebServlet("/shuju/SelectServlet")
public class SelectServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public SelectServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("utf-8");
List<StudentInfo> studentInfos = new ArrayList<StudentInfo>();
StudentDao sDao = new StudentDao();
String sql = "";
int id;
String name;
float weight1;
float weight2;
if (!request.getParameter("id").equals("")) {
id = Integer.parseInt(request.getParameter("id"));
sql = sql + " and id=" + id;
}
if (!request.getParameter("name").equals("")) {
name = request.getParameter("name");
sql = sql + " and name='" + name + "'";
}
if ((request.getParameter("weight1") != "") && (request.getParameter("weight2") != "")) {
weight1 = Float.parseFloat(request.getParameter("weight1"));
weight2 = Float.parseFloat(request.getParameter("weight2"));
sql = sql + " and weight between " + weight1 + " and " + weight2;
}
try {
studentInfos = sDao.findUserById2(sql);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
request.setAttribute("info", studentInfos);
request.getRequestDispatcher("shujuselect_process.jsp").forward(request, response);
}
}
package servlet;
import java.io.IOException;
import Dao.*;
import bean.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* Servlet implementation class UpdateServlet
*/
@WebServlet("/shuju/UpdateServlet1")
public class UpdateServlet1 extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public UpdateServlet1() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("utf-8");
int idselect = Integer.parseInt(request.getParameter("id"));
HttpSession session = request.getSession();
session.setAttribute("id", idselect);
StudentInfo studentInfo = new StudentInfo();
StudentInfo studentInfo2 = new StudentInfo();
StudentDao studentDao = new StudentDao();
studentInfo.setId(idselect);
try {
studentInfo2 = studentDao.findUserById(studentInfo);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
request.setAttribute("info", studentInfo2);
request.getRequestDispatcher("shujuupdate_process.jsp").forward(request, response);
}
}
package servlet;
import java.io.IOException;
import Dao.*;
import bean.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class UpdateServlet2
*/
@WebServlet("/shuju/UpdateServlet2")
public class UpdateServlet2 extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public UpdateServlet2() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
int n = 0;
int id = Integer.parseInt(request.getParameter("id"));
String name = request.getParameter("name");
String sex = request.getParameter("sex");
int age = Integer.parseInt(request.getParameter("age"));
float weight = Float.parseFloat(request.getParameter("weight"));
float height = Float.parseFloat(request.getParameter("height"));
StudentInfo studet = new StudentInfo();
studet.setId(id);
studet.setName(name);
studet.setSex(sex);
studet.setAge(age);
studet.setWeight(weight);
studet.setHeight(height);
StudentDao studentDao = new StudentDao();
try {
n = studentDao.update(studet);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (n >= 1) {
request.setAttribute("result", "数据重写成功");
} else {
request.setAttribute("result", "数据重写失败");
}
request.getRequestDispatcher("shujuupdate_process_edit.jsp").forward(request, response);
}
}
jsp页面部分(由于代码比较多,这里省略部分代码):
主页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style>
#left {
float: left;
width: 29%;
height: 600px;
}
#right {
margin-left: 2px;
float: left;
width: 69%;
height: 600px;
}
iframe {
height: 600px;
width: 100%;
}
</style>
</head>
<body>
<div id="left">
<iframe src="left.jsp" name="left"></iframe>
</div>
<div id="right">
<iframe src="#" name="right"></iframe>
</div>
</body>
</html>
左侧任务栏表:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style>
a {
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<a href="login.jsp" target="right">登陆</a>
<br />
<a href="passwordmodi_input.jsp" target="right">密码修改</a>
<br />
<a href="#" target="right">数据操作</a>
<br />
<a href="shujuadd_input.jsp" target="right">数据添加</a>
<br />
<a href="shujudelete_input.jsp" target="right">数据删除</a>
<br />
<a href="shujuselect_input.jsp" target="right">数据查询</a>
<br />
<a href="shujuupdate_input.jsp" target="right">数据修改</a>
<br />
<a href="exit.jsp" target="right">退出</a>
<br />
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
if (session.getAttribute("login") == null) {
out.print("你从来没有登陆过,请先登陆后才能点击退出");
response.setHeader("refresh", "3;url='login.jsp'");
} else {
session.removeAttribute("login");
out.print("你的登陆session已经删除!!");
}
%>
</body>
</html>