数据库:student
表:user
表格结构:
DAO,数据访问对象,主要的功能就是数据操作,在标准的程序开发架构中属于数据层的操作。其主要分为以下几层:
客户层:因为现在大多采用B/S架构,所以一般客户都是用浏览器进行访问,当然也可以使用其它的访问程序。
显示层:使用JSP/Servlet进行页面效果的显示。
业务层:会将多个原子性的DAO操作进行组合,组成一个完整的业务逻辑。
数据层(DAO):提供原子性的操作,例如增删改查等。
DAO设计流程:
1设计一个专门负责打开连接数据库和关闭数据库操作的类。命名规则:xxx.dbc.DatabaseConnection
2设计VO(值对象),其主要有属性和setter和getter方法构成,以数据库中的字段项对应。命名规则:xxx.vo.ttt;ttt要与数据库的表的名字一致
3.DAO设计:定义一系列的原子性操作,如增删改查等操作的接口。命名规则:xxx.dao.IXxx.DAO
4设计DAO接口的真正实现的类,完成具体的数据库操作。但是不再去负责数据库的打开和关闭。命名规则:xxx.dao.impl.xxxDAOImpl
5.proxy代理类的实现:主要将1,4结合起来,完成整个操作过程。命名规则:xxx.dao.proxy.XxxProxy
6.Factory类主要用来获得一个DAO类的实例对象。命名规则:xxx.factory.DAOFactory
引用
我写的DAO并不是真正的DAO设计模式,因为我没有添加代理类来解耦数据库连接与数据库操作,也没有加工厂模式类返回代理类的对象。但是基本的框架还是有的。
如有错误,请指出,谢谢!
首先先列出基本的框架,分别有三个包,bean包,DAO包,Servlets包。DAO包下的为数据库对象类,一个对象代表着数据库中的一个记录,我认真把它放在beans包下比较好,但也懒得改了。
文章目录
- JSP
- index.html(主页面)
- title.html(标题页面)
- lable.html(目录页面)
- display.html(初始页面)
- showall.jsp(显示所有记录)
- showFind.jsp(显示模糊查找结果页面)
- AddStudent.jsp(添加页面)
- addsuccess.jsp(添加成功页面)
- addfail.jsp(添加失败页面)
- find.jsp(查找页面)
- delete.jsp(删除页面)
- update.jsp(更新页面)
- updateEditor.jsp(更新编辑页面)
- java
- beans.JDBCUtils.java(数据库连接工具类)
- DAO.IStudentDAO.java(DAO接口)
- DAO.Student.java(数据库对象类)
- DAO.StudentDAO.java(DAO接口实现类)
- Servlets.AddStudent.java(控制学生记录添加的Servlet)
- Servlets.Studentall.java(控制信息展示的Servlet)
- Servlets.StudentDelete.java(控制删除记录)
- Servlets.StudentFind.java(控制查找)
- Servlets.Studentupdate.java(控制更新)
- Servlets.Studentupdate2.java(控制更新)
JSP
index.html(主页面)
利用frameset框架来设计网页布局
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<frameset rows="15%,*">
<frame src = "title.html" noresize=“noresize”>
<frameset cols="10%,*">
<frame src="lable.html" noresize=“noresize”>
<frame src="display.html" name="view_frame" noresize=“noresize”>
</frameset>
</frameset>
</html>
title.html(标题页面)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<style>
h1{
text-align:center;
color:#CD5C5C;
font-family:"宋体";
}
body{
background-color:#F0F8FF;
}
</style>
<body>
<img src="http://img.sccnn.com/simg/338/64538.jpg" style="float:left" width="60" height="50">
<h1>
学生体质信息管理
</h1>
</body>
</html>
lable.html(目录页面)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h3>目录</h3>
<ul>
<li><a href="Studentall" target="view_frame">主页</a></li>
<li><a href="AddStudent.jsp" target="view_frame">添加</a></li>
<li><a href="find.jsp" target="view_frame">查找</a></li>
<li><a href="update.jsp" target="view_frame">更新</a></li>
<li><a href="delete.jsp" target="view_frame">删除</a></li>
</ul>
</body>
</html>
display.html(初始页面)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<div align="center">
<h1>
欢迎使用学生体质信息管理系统
</h1>
</div>
</body>
</html>
showall.jsp(显示所有记录)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import = "java.util.*"%>
<%@ page import = "DAO.Student" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style>
body {
padding: 40px 100px;
}
table {
*border-collapse: collapse; /* IE7 and lower */
border-spacing: 0;
width: 100%;
}
</style>
</head>
<body>
<div align="center">
<%
//int cnt = 1;
List<Student> list = (List<Student>)request.getAttribute("list");
if(list == null||list.size()==0){
%>
<h3>无记录</h3>
<%
}
else{
%>
<table border="1">
<br><br><br><br>
<tr>
<th>编号</th>
<th>姓名</th>
<th>性别</th>
<th>年龄</th>
<th>身高</th>
<th>体重</th>
</tr>
<%
for(Student st:list){
%>
<tr>
<td><%=st.getId()%></td>
<td><%=st.getName() %></td>
<td><%=st.getSex() %>
<td><%=st.getAge() %></td>
<td><%=st.getHeight() %></td>
<td><%=st.getWeight() %></td>
</tr>
<%
}
}
%>
</table>
</div>
</body>
</html>
showFind.jsp(显示模糊查找结果页面)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import = "java.util.*"%>
<%@ page import = "DAO.Student" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style>
body {
padding: 40px 100px;
}
table {
*border-collapse: collapse; /* IE7 and lower */
border-spacing: 0;
width: 100%;
}
</style>
</head>
<body>
<div align="center">
<%
List<Student> list = (List<Student>)request.getAttribute("list");
if(list == null||list.size()==0){
%>
<h3>无记录</h3>
<%
}
else{
%>
<table border="1">
<br><br><br><br>
<tr>
<th>姓名</th>
<th>性别</th>
<th>年龄</th>
<th>身高</th>
<th>体重</th>
</tr>
<%
for(Student st:list){
%>
<tr>
<td><%=st.getName() %></td>
<td><%=st.getSex() %>
<td><%=st.getAge() %></td>
<td><%=st.getHeight() %></td>
<td><%=st.getWeight() %></td>
</tr>
<%
}
}
%>
</table>
</div>
</body>
</html>
AddStudent.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>
</head>
<body>
<div align="center">
<br>
<h3>添加记录</h3>
<form action="AddStudent" method="post">
<table border="0" cellpadding="10" cellspacing="0">
<tr>
<th>姓名</th>
<td><input name="t_name"></td>
</tr>
<tr>
<th>性别</th>
<td><input name="t_sex"></td>
</tr>
<tr>
<th>年龄</th>
<td><input name="t_age"></td>
</tr>
<tr>
<th>身高</th>
<td><input name="t_height"></td>
</tr>
<tr>
<th>体重</th>
<td><input name="t_weight"></td>
</tr>
<tr>
<th><input type="submit" value="提交"></th>
<th><input type="reset" value="重置"></th>
</tr>
</table>
</form>
</div>
</body>
</html>
addsuccess.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>
</head>
<body>
<b>添加成功!</b>
</body>
</html>
addfail.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>
</head>
<body>
<b>添加失败!</b>
</body>
</html>
find.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>
</head>
<body>
<div align="center">
<h3>模糊查找</h3>
<form action="StudentFind" method="post">
请输入带查找学生的名字或部分名字: <input name="t_name">
<br>
<br>
<input type="submit" value="查找">
</form>
</div>
</body>
</html>
delete.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>
</head>
<body>
<div align="center">
<h3>学生信息删除</h3>
<form action="StudentDelete" action="post">
请输入待删除的学生编号:<input name="t_id"><br><br>
<input type="submit" value="删除">
</form>
</div>
</body>
</html>
update.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>
</head>
<body>
<div align="center">
<h3>学生信息更新</h3>
<form action="Studentupdate" action="post">
请输入待更新的学生编号:<input name="t_id"><br><br>
<input type="submit" value="更新">
</form>
</div>
</body>
</html>
updateEditor.jsp(更新编辑页面)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="DAO.Student"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</style>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
Student stu = (Student)request.getAttribute("stu");
String id = (String)request.getAttribute("id");
%>
<div align="center">
<br>
<h3>更新记录</h3>
<form action="Studentupdate2" method="post">
<table border="0" cellpadding="10" cellspacing="0">
<tr>
<th>姓名</th>
<td><input name="t_name" value="<%=stu.getName()%>"></td>
</tr>
<tr>
<th>性别</th>
<td><input name="t_sex" value="<%=stu.getSex()%>"></td>
</tr>
<tr>
<th>年龄</th>
<td><input name="t_age" value="<%=stu.getAge()%>"></td>
</tr>
<tr>
<th>身高</th>
<td><input name="t_height" value="<%=stu.getHeight()%>"></td>
</tr>
<tr>
<th>体重</th>
<td><input name="t_weight" value="<%=stu.getWeight()%>"></td>
</tr>
<tr>
<th><input type="submit" value="更改"></th>
<th><input type="reset" value="重置"></th>
</tr>
<tr>
<td><input type="hidden" value="<%=id%>" name="id"></input></td>
</tr>
</table>
</form>
</div>
</body>
</html>
java
beans.JDBCUtils.java(数据库连接工具类)
package beans;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBCUtils {
//这个工具类,主要为我们获取一个数据库连接
private static String driverName = "com.mysql.jdbc.Driver";
private static String url = "jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=UTF8";
private static String username = "root";
private static String password = "123456";
//静态代码块,目的,让第一次使用到JDBCUtils中加载驱动,第二次以后不再加载了
static {
//1.加载驱动
try {
Class.forName(driverName);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
//System.out.println("驱动加载失败..请检查驱动包");
throw new RuntimeException("驱动加载失败..请检查驱动包");
}
}
public static Connection getConnection() throws Exception {
//2.获取和数据库的连接
Connection conn = DriverManager.getConnection(url, username, password);
//3.返回连接对象
return conn;
}
//关闭所有资源的统一代码
public static void closeAll(Connection conn, Statement st, ResultSet rs) {
//负责关闭
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (st != null) {
try {
st.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
DAO.IStudentDAO.java(DAO接口)
package DAO;
import java.util.List;
public interface IStudentDAO {
int create(Student stu) throws Exception;
void remove(String id) throws Exception;
List<Student> find(String x) throws Exception;
List<Student> findall() throws Exception;
void update(Student stu,String id) throws Exception;
}
DAO.Student.java(数据库对象类)
package DAO;
public class Student {
private int id;
private String name;
private int age;
private String sex;
private float height,weight;
public Student(){
}
public Student(int id,String name,int age,String sex,float height,float weight) {
this.id = id;this.name = name ;this.age = age;this.sex = sex;this.height = height;this.weight = weight;
}
public Student(String name,int age,String sex,float height,float weight) {
this.name = name ;this.age = age;this.sex = sex;this.height = height;this.weight = weight;
}
public int getId() {
return id;
}
public void setId(int 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;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public float getHeight() {
return height;
}
public void setHeight(float height) {
this.height = height;
}
public float getWeight() {
return weight;
}
public void setWeight(float weight) {
this.weight = weight;
}
}
DAO.StudentDAO.java(DAO接口实现类)
package DAO;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.LinkedList;
import java.util.List;
import beans.JDBCUtils;
public class StudentDAO implements IStudentDAO{
@Override
public int create(Student stu) throws Exception {
// TODO Auto-generated method stub
Connection con = JDBCUtils.getConnection();
String sql = "insert into user(name,age,sex,height,weight) values(?,?,?,?,?)";
PreparedStatement pst = con.prepareStatement(sql);
pst.setString(1, stu.getName());
pst.setInt(2, stu.getAge());
pst.setString(3, stu.getSex());
pst.setFloat(4, stu.getHeight());
pst.setFloat(5,stu.getWeight());
int n = pst.executeUpdate();
return n;
}
@Override
public void remove(String id) throws Exception {
// TODO Auto-generated method stub
Connection con = JDBCUtils.getConnection();
Statement smt = con.createStatement();
//System.out.println(id);
String sql = "delete from user where id = " + id;
smt.executeUpdate(sql);
}
@Override
public List<Student> find(String x) throws Exception {
// TODO Auto-generated method stub
List<Student> list = new LinkedList<Student>();
Connection con = JDBCUtils.getConnection();
String sql = "select * from user where name like \"%\"?\"%\"";
PreparedStatement pst = con.prepareStatement(sql);
pst.setString(1, x);
ResultSet rs = pst.executeQuery();
while(rs.next()) {
list.add(new Student(rs.getString(2),rs.getInt(3),rs.getString(4),rs.getFloat(5),rs.getFloat(6)));
}
return list;
}
@Override
public List<Student> findall() throws Exception{
// TODO Auto-generated method stub
List<Student> list = new LinkedList<Student>();
Connection con = JDBCUtils.getConnection();
String sql = "select * from user";
Statement smt = con.createStatement();
ResultSet rs = smt.executeQuery(sql);
while(rs.next()) {
list.add(new Student(rs.getInt(1),rs.getString(2),rs.getInt(3),rs.getString(4),rs.getFloat(5),rs.getFloat(6)));
}
return list;
}
@Override
public void update(Student stu,String id) throws Exception {
Connection con = JDBCUtils.getConnection();
String sql = "update user set name = ? ,age = ? ,sex = ? ,height = ?,weight = ? where id = " + id;
PreparedStatement pst = con.prepareStatement(sql);
pst.setString(1, stu.getName());
pst.setInt(2, stu.getAge());
pst.setString(3, stu.getSex());
pst.setFloat(4, stu.getHeight());
pst.setFloat(5, stu.getWeight());
pst.executeUpdate();
}
}
Servlets.AddStudent.java(控制学生记录添加的Servlet)
package Servlets;
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 DAO.Student;
import DAO.StudentDAO;
/**
* Servlet implementation class AddStudent
*/
@WebServlet("/AddStudent")
public class AddStudent extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public AddStudent() {
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");
String name = request.getParameter("t_name");
int age = Integer.parseInt(request.getParameter("t_age"));
String sex = request.getParameter("t_sex");
float height = Float.parseFloat(request.getParameter("t_height"));
float weight = Float.parseFloat(request.getParameter("t_weight"));
StudentDAO std = new StudentDAO();
int n = 0;
try {
n = std.create(new Student(name,age,sex,height,weight));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(n == 0) request.getRequestDispatcher("addfail.jsp").forward(request, response);
else request.getRequestDispatcher("addsuccess.jsp").forward(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
Servlets.Studentall.java(控制信息展示的Servlet)
package Servlets;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
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 DAO.Student;
import DAO.StudentDAO;
/**
* Servlet implementation class Studentall
*/
@WebServlet("/Studentall")
public class Studentall extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Studentall() {
super();
// TODO Auto-generated constructor stub
}
/**
* @throws IOException
* @throws ServletException
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
// TODO Auto-generated method stub
StudentDAO sd = new StudentDAO();
try {
List<Student> list = sd.findall();
request.setAttribute("list", list);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
request.getRequestDispatcher("showall.jsp").forward(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
Servlets.StudentDelete.java(控制删除记录)
package Servlets;
import java.io.IOException;
import java.io.PrintWriter;
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 DAO.StudentDAO;
/**
* Servlet implementation class StudentDelete
*/
@WebServlet("/StudentDelete")
public class StudentDelete extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public StudentDelete() {
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
String id = request.getParameter("t_id");
try {
new StudentDAO().remove(id);
//PrintWriter pw = response.getWriter();
//pw.print(id);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
Servlets.StudentFind.java(控制查找)
package Servlets;
import java.io.IOException;
import java.util.List;
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 DAO.Student;
import DAO.StudentDAO;
/**
* Servlet implementation class StudentFind
*/
@WebServlet("/StudentFind")
public class StudentFind extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public StudentFind() {
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
String name = request.getParameter("t_name");
StudentDAO std = new StudentDAO();
try {
List<Student> list = std.find(name);
request.setAttribute("list", list);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
request.getRequestDispatcher("showFind.jsp").forward(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
Servlets.Studentupdate.java(控制更新)
package Servlets;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
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 DAO.Student;
import beans.JDBCUtils;
/**
* Servlet implementation class Studentupdate
*/
@WebServlet("/Studentupdate")
public class Studentupdate extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Studentupdate() {
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
String id = request.getParameter("t_id");
if(id==null||id.equals("")) id = "1";
Connection con = null;
try {
con = JDBCUtils.getConnection();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String sql = "select * from user where id = " + id;
Statement smt = null;
try {
smt = con.createStatement();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ResultSet rs = null;
try {
rs = smt.executeQuery(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Student stu = new Student();
try {
while(rs.next()) {
stu.setName(rs.getString(2));
stu.setAge(rs.getInt(3));
stu.setSex(rs.getString(4));
stu.setHeight(rs.getFloat(5));
stu.setWeight(rs.getFloat(6));
}
request.setAttribute("stu", stu);
request.setAttribute("id", id);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
request.getRequestDispatcher("updateEditor.jsp").forward(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
Servlets.Studentupdate2.java(控制更新)
package Servlets;
import java.io.IOException;
import java.io.PrintWriter;
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 DAO.Student;
import DAO.StudentDAO;
/**
* Servlet implementation class Studentupdate2
*/
@WebServlet("/Studentupdate2")
public class Studentupdate2 extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Studentupdate2() {
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");
String name = request.getParameter("t_name");
String id = request.getParameter("id");
int age = Integer.parseInt(request.getParameter("t_age"));
String sex = request.getParameter("t_sex");
float height = Float.parseFloat(request.getParameter("t_height"));
float weight = Float.parseFloat(request.getParameter("t_weight"));
StudentDAO std = new StudentDAO();
//PrintWriter pw = response.getWriter();
//pw.print(id);
try {
std.update(new Student(name,age,sex,height,weight),id);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}