數據庫: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);
}
}