该部分内容实现的功能主要是:从数据库中查询数据并展示到页面,在页面中新增数据信息,在页面中修改数据信息,在页面中删除数据信息。 =================stuList.jsp================
//本页为数据显示页面
<%@ page import="java.util.List" %>
<%@ page import="com.ibeifeng.student.Student" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>stuList</title>
</head>
<body>
<a href="addStu.jsp">新增学员</a>
<table width="100%" border="1px solid black">
<tr>
<td>学号</td>
<td>姓名</td>
<td>密码</td>
<td>性别</td>
<td>邮箱</td>
<td>操作</td>
</tr>
<%
List<Student> list = (List<Student>)request.getAttribute("list");
for(Student stu : list){
%>
<tr>
<td><%=stu.getId()%></td>
<td><%=stu.getName()%></td>
<td><%=stu.getPwd()%></td>
<td><%=stu.getSex()%></td>
<td><%=stu.getEmail()%></td>
<td><a href="updateStu.jsp?sid=<%=stu.getId()%>">修改</a> | <a href="doDel.jsp?sid=<%=stu.getId()%>">删除</a></td>
</tr>
<% }
%>
</table>
</body>
</html> ===============doList02.jsp==================
//本页是数据显示页面stuList的后台数据处理部分
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="com.ibeifeng.student.Student" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.List" %>
<%@ page import="com.ibeifeng.student.ConfigUtil" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
Class.forName(new ConfigUtil().getValue("driver"));
Connection conn = DriverManager.getConnection(
new ConfigUtil().getValue("url"),
new ConfigUtil().getValue("username"),
new ConfigUtil().getValue("password")); String sql = "select * from student";
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
List<Student> list = new ArrayList<Student>();
while(rs.next()){
Student stu = new Student();
stu.setId(rs.getInt(1));
stu.setName(rs.getString(3));
stu.setPwd(rs.getString(2));
stu.setSex(rs.getInt(4));
stu.setEmail(rs.getString(5));
list.add(stu);
} if(rs != null){
rs.close();
}
if(ps != null){
ps.close();
}
if(conn != null){
conn.close();
} request.setAttribute("list",list);
request.getRequestDispatcher("stuList.jsp").forward(request,response);
%> ====================addStu.jsp==============
//本页为新增数据的页面展示部分
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>新增学员信息</title>
</head>
<body>
<form action="doSave.jsp" method="post">
<table align="center" border="1px solid black">
<tr>
<td>学号</td>
<td><input type="text" name="stuId" > </td>
</tr>
<tr>
<td>姓名</td>
<td><input type="text" name="stuName" ></td>
</tr>
<tr>
<td>密码</td>
<td><input type="password" name="pwd" ></td>
</tr>
<tr>
<td>性别</td>
<td>
<input type="radio" name="gender" value="0">女
<input type="radio" name="gender" value="1">男
</td>
</tr>
<tr>
<td>邮箱</td>
<td><input type="text" name="email" ></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" name="submit" value="保存">
<input type="button" name="return" value="取消">
</td>
</tr>
</table>
</form>
</body>
</html> ===================doSave.jsp==============
//本页为数据新增页面,保存按钮的数据处理部分
<%@ page import="com.ibeifeng.student.ConfigUtil" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8"); Class.forName(new ConfigUtil().getValue("driver"));
Connection conn = DriverManager.getConnection(
new ConfigUtil().getValue("url"),new ConfigUtil().getValue("username"),new ConfigUtil().getValue("password")); String sql = "insert into student(stu_id,stu_name,pwd,sex,email) values(?,?,?,?,?)";
PreparedStatement ps = conn.prepareStatement(sql); String rid = request.getParameter("stuId");
int id = Integer.parseInt(rid);
ps.setInt(1,id);
ps.setString(2,request.getParameter("stuName"));
ps.setString(3,request.getParameter("pwd"));
ps.setInt(4,1);
ps.setString(5,request.getParameter("email")); int isSuccess = ps.executeUpdate();
System.out.print(isSuccess);
if(isSuccess <= 0){
System.out.print("新增有误,请重试");
request.getRequestDispatcher("/stuInfo/addStu.jsp").forward(request,response);
}else{
request.getRequestDispatcher("/stuInfo/doList02.jsp").forward(request,response);
} if(ps != null){
ps.close();
}
if(conn != null){
conn.close();
}
%> ================updateStu.jsp===============
//本页为数据修改页面展示部分
<%@ page import="com.ibeifeng.student.StudentDao" %>
<%@ page import="com.ibeifeng.student.Student" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
//获取前端页面传递的参数
String sid = request.getParameter("sid"); //该参数由stuList页面的<a href="updateStu.jsp?sid=stu.getStudentId()">传递过来
//创建StudentDao对象
StudentDao stuDao = new StudentDao();
Student stu = stuDao.getStuByStuId(sid); if(stu != null){
request.setAttribute("stu",stu);
} %> <html>
<head>
<title>新增学员信息</title>
</head>
<body>
<form action="doUpdate.jsp" method="post">
<table align="center" border="1px solid black">
<tr>
<td>学号</td>
<td><input type="text" name="stuId" readonly="readonly" value="<%=stu.getId()%>"> </td>
</tr>
<tr>
<td>姓名</td>
<td><input type="text" name="stuName" value="<%=stu.getName()%>"></td>
</tr>
<tr>
<td>密码</td>
<td><input type="password" name="pwd" value="<%=stu.getPwd()%>"></td>
</tr>
<tr>
<td>性别</td>
<td>
<input type="radio" name="gender" value="0" <%if(stu.getSex() == 0){%>checked="checked"<%}%>>女
<input type="radio" name="gender" value="1" <%if(stu.getSex() == 1){%>checked="checked"<%}%>>男
</td>
</tr>
<tr>
<td>邮箱</td>
<td><input type="text" name="email" value="<%=stu.getEmail()%>"></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" name="submit" value="保存">
<input type="button" name="return" value="取消">
</td>
</tr>
</table>
</form>
</body>
</html> =================doUpdate.jsp==============
//本页为数据修改部分,后台数据处理操作的内容
<%@ page import="com.ibeifeng.student.ConfigUtil" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="com.ibeifeng.student.StudentDao" %>
<%@ page import="com.ibeifeng.student.Student" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8"); Class.forName(new ConfigUtil().getValue("driver"));
Connection conn = DriverManager.getConnection(
new ConfigUtil().getValue("url"),new ConfigUtil().getValue("username"),new ConfigUtil().getValue("password")); String stuid = request.getParameter("stuId");
int sid = Integer.parseInt(stuid);
String name = request.getParameter("stuName");
String pwd = request.getParameter("pwd");
String gender = request.getParameter("gender");
int gen = Integer.parseInt(gender);
String email = request.getParameter("email"); StudentDao stuDao = new StudentDao();
Student stu = new Student();
stu.setId(sid);
stu.setName(name);
stu.setPwd(pwd);
stu.setSex(gen);
stu.setEmail(email); int resule = stuDao.updateStu(stu);
if(resule > 0){
request.getRequestDispatcher("doList02.jsp").forward(request,response);
}else{
request.getRequestDispatcher("updateStu.jsp?sno="+stuid).forward(request,response);
}
%> ===============doDel.jsp===================
//本页为数据删除部分后台处理内容
<%@ page import="com.ibeifeng.student.StudentDao" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8"); String sid = request.getParameter("sid");
StudentDao stuDao = new StudentDao();
stuDao.delStu(sid);
request.getRequestDispatcher("doList02.jsp").forward(request,response);
%> ===============BaseDao.java=================
//本页为封装的方法,主要是涉及到数据库连接的初始化、查询、更新、资源释放
package com.ibeifeng.student; import java.sql.*; /*
* JDBC操作DAO通用类方法封装
* */
public class BaseDao {
static Connection conn = null;
static PreparedStatement ps = null;
static ResultSet rs = null;
//静态语句块
static{
try {
Class.forName(ConfigUtil.getValue("driver")); } catch (ClassNotFoundException e) {
e.printStackTrace();
}
} public static Connection getConn(){
try {
conn = DriverManager.getConnection(ConfigUtil.getValue("url"),
ConfigUtil.getValue("username"),
ConfigUtil.getValue("password"));
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
} /*
* 查询
* */
public ResultSet search(String sql){
try {
rs = ps.executeQuery();
return rs;
} catch (SQLException e) {
throw new RuntimeException();
}
} /*
* 更新数据
* */
public int modify(String sql){
try {
return ps.executeUpdate(sql);
} catch (SQLException e) {
throw new RuntimeException();
}
} public static void close(ResultSet rs, PreparedStatement ps, Connection conn){
try {
if(rs != null) {
rs.close();
}
if(ps != null) {
ps.close();
}
if(conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
} =================ConfigUtil.java==============
//本页封装了从配置文件读取数据的方法
package com.ibeifeng.student; import java.io.IOException;
import java.io.InputStream;
import java.util.Properties; public class ConfigUtil {
//通过key获取jdbc配置的value
public static String getValue(String key){
Properties pro = new Properties();
//获取流信息
InputStream is = ConfigUtil.class.getResourceAsStream("/jdbc.properties");
try {
//加载流文件
pro.load(is);
//获取属性值
return pro.getProperty(key);
} catch (IOException e) {
throw new RuntimeException();
}
}
} ================StudentDao.java==============
//本页封装了对于数据库,数据处理的一些方法
package com.ibeifeng.student; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException; public class StudentDao {
/*
* Student Dao用于处理Student类
* */
Connection conn = null;
PreparedStatement ps = null;
public int updateStu(Student stu){
/*
* 更新学员信息
* */
String sql = "update student set stu_name=?,pwd=?,sex=?,email=? where stu_id=?";
try {
ps = BaseDao.getConn().prepareStatement(sql);
ps.setString(1,stu.getName());
ps.setString(2,stu.getPwd());
ps.setInt(3,stu.getSex());
ps.setString(4,stu.getEmail());
ps.setInt(5,stu.getId()); return ps.executeUpdate();
} catch (SQLException e) {
throw new RuntimeException();
}finally {
BaseDao.close(null,ps,conn);
}
} public Student getStuByStuId(String stu_id){
String sql = "select * from student where stu_id=?";
Student stu = null;
try {
conn = BaseDao.getConn();
ps = conn.prepareStatement(sql);
int sid = Integer.parseInt(stu_id);
ps.setInt(1,sid);
ResultSet rs = ps.executeQuery(); if(rs.next()){
stu = new Student();
stu.setId(rs.getInt("stu_id"));
stu.setName(rs.getString("stu_name"));
stu.setPwd(rs.getString("pwd"));
stu.setSex(rs.getInt("sex"));
stu.setEmail(rs.getString("email"));
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
BaseDao.close(null,ps,conn);
}
return stu;
} /*
* 根据学号删除学生信息
* */
public int delStu(String sid){
String sql = "delete from student where stu_id = ?";
conn = BaseDao.getConn();
try {
ps = conn.prepareStatement(sql);
int id = Integer.parseInt(sid);
ps.setInt(1,id);
return ps.executeUpdate();
} catch (SQLException e) {
throw new RuntimeException();
}
}
} ===================Student.java==============
//封装类对象
package com.ibeifeng.student; public class Student {
private int id;
private String name;
private String pwd;
private int sex;
private String email; public Student(){} public void setId(int id) {
this.id = id;
} public void setName(String name) {
this.name = name;
} public void setPwd(String pwd) {
this.pwd = pwd;
} public void setSex(int sex) {
this.sex = sex;
} public void setEmail(String email) {
this.email = email;
} public int getId() {
return id;
} public String getName() {
return name;
} public String getPwd() {
return pwd;
} public int getSex() {
return sex;
} public String getEmail() {
return email;
}
} =============jdbc.properties=============
//配置文件信息
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test
username=root
password=123456