标签:
Servlet总结
本程序采用Servlet开发技术,MVC分层,所有程序在设计时都要接口为操作的标准,主要逻辑操作只有增删改查。
具体实现操作请看源代码。
本程序采用的是MYSQL数据库,需加入相应的jar包
目录结构
首先贴上底层数据层:
连接数据库
package com.student.dbc ;
import java.sql.* ;
public class DatabaseConnection {
private static final String DBDRIVER = "org.gjt.mm.mysql.Driver" ;
private static final String DBURL = "jdbc:mysql://localhost:3306/java_web?useUnicode=true&characterEncoding=UTF-8" ;
private static final String DBUSER = "root" ;
private static final String DBPASSWORD = "root" ;
private Connection conn = null ;
public DatabaseConnection() throws Exception{
try{
Class.forName(DBDRIVER) ;
this.conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD) ;
}catch(Exception e){
throw e ;
}
}
public Connection getConnection(){
return this.conn ;
}
public void close() throws Exception{
if(this.conn != null){
try{
this.conn.close() ;
}catch(Exception e){
throw e ;
}
}
}
}
实体类
package com.student.vo;
public class Student {
private String id;
private String name;
private int age;
private int sex;
private String major;
private String college;
private String introduction;
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;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public String getCollege() {
return college;
}
public void setCollege(String college) {
this.college = college;
}
public String getIntroduction() {
return introduction;
}
public void setIntroduction(String introduction) {
this.introduction = introduction;
}
}
业务逻辑类
package com.student.action;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import com.mysql.jdbc.Connection;
import com.student.dbc.DatabaseConnection;
import com.student.vo.Student;
public class StudentAction {
private static Connection conn = null ;
/**
* 增加学生
* @param id
* @param name
* @param age
* @param sex
* @param major
* @param college
* @param introduction
* @return
*/
public static boolean addStudent(String id,String name,int age,int sex,String major,String college,String introduction) {
try {
java.sql.Connection conn=new DatabaseConnection().getConnection();
PreparedStatement st=conn.prepareStatement("insert into student values(?,?,?,?,?,?,?)");
st.setString(1, id);
st.setString(2, name);
st.setInt(3, age);
st.setInt(4, sex);
st.setString(5, major);
st.setString(6, college);
st.setString(7, introduction);
st.execute();
conn.close();
return true;
} catch (Exception e) {
// TODO: handle exception
return false;
}
}
/**
* 更新学生
* @param id
* @param name
* @param age
* @param sex
* @param major
* @param college
* @param introduction
* @return
*/
public static boolean updateStudent(String id,String name,int age,int sex,String major,String college,String introduction) {
try {
java.sql.Connection conn=new DatabaseConnection().getConnection();
PreparedStatement st=conn.prepareStatement("update student set name=?,age=?,sex=?,major=?,college=?,introduction=? where id=?");
st.setString(1, name);
st.setInt(2, age);
st.setInt(3, sex);
st.setString(4, major);
st.setString(5, college);
st.setString(6, introduction);
st.setString(7, id);
st.execute();
conn.close();
return true;
} catch (Exception e) {
// TODO: handle exception
return false;
}
}
/**
* 删除
* @param id
* @return
*/
public static boolean deleteStudent(String id) {
try {
java.sql.Connection conn=new DatabaseConnection().getConnection();
PreparedStatement st=conn.prepareStatement("delete from student where id=?");
st.setString(1, id);
st.execute();
conn.close();
return true;
}catch (Exception e) {
// TODO: handle exception
return false;
}
}
/**
* 获取全部学生
* @return
*/
public static ArrayList getAllstudent() {
ArrayList students=new ArrayList();
try {
java.sql.Connection conn=new DatabaseConnection().getConnection();
PreparedStatement st=conn.prepareStatement("select * from student");
st.execute();
ResultSet rs=st.getResultSet();
while(rs.next()){
Student student=new Student();
student.setId(rs.getString("id"));
student.setName(rs.getString("name"));
student.setAge(rs.getInt("age"));
student.setSex(rs.getInt("sex"));
student.setMajor(rs.getString("major"));
student.setCollege(rs.getString("college"));
student.setIntroduction(rs.getString("introduction"));
students.add(student);
}
conn.close();
} catch (Exception e) {
// TODO: handle exception
}
return students;
}
/**
* 按学号查询学生
* @param id
* @return
*/
public static Student getStudent(String id) {
Student student=null;
try {
java.sql.Connection conn=new DatabaseConnection().getConnection();
PreparedStatement st=conn.prepareStatement("select * from student where id=?");
st.setString(1, id);
st.execute();
ResultSet rs=st.getResultSet();
while(rs.next()){
student=new Student();
student.setId(rs.getString("id"));
student.setName(rs.getString("name"));
student.setAge(rs.getInt("age"));
student.setSex(rs.getInt("sex"));
student.setMajor(rs.getString("major"));
student.setCollege(rs.getString("college"));
student.setIntroduction(rs.getString("introduction"));
}
conn.close();
} catch (Exception e) {
// TODO: handle exception
}
return student;
}
}
JSP与数据交换层
package com.student.servlet;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.student.action.StudentAction;
public class StudentServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
if(request.getRequestURI().endsWith("/viewStudent")){
RequestDispatcher dispatcher = request.getRequestDispatcher("viewstudent.jsp");
dispatcher .forward(request, response);
}else if(request.getRequestURI().endsWith("/addStudent")){
doAddStudent(request,response);
}
else if (request.getRequestURI().endsWith("/updateStudent")) {
doUpdateStudent(request,response);
}else if (request.getRequestURI().endsWith("/deleteStudent")) {
doDeleteStudent(request,response);
}
}
private void doAddStudent(HttpServletRequest request, HttpServletResponse response) throws IOException{
String id=request.getParameter("id");
String name=request.getParameter("name");
String age=request.getParameter("age");
String sex=request.getParameter("sex");
String major=request.getParameter("major");
String college=request.getParameter("college");
String introduction=request.getParameter("introduction");
StudentAction.addStudent(id, name,new Integer(age), new Integer(sex), major, college, introduction);
response.sendRedirect("index.jsp");
}
private void doUpdateStudent(HttpServletRequest request, HttpServletResponse response) throws IOException {
String id=request.getParameter("id");
String name=request.getParameter("name");
String age=request.getParameter("age");
String sex=request.getParameter("sex");
String major=request.getParameter("major");
String college=request.getParameter("college");
String introduction=request.getParameter("introduction");
StudentAction.updateStudent(id, name, new Integer(age), new Integer(sex), major, college, introduction);
response.sendRedirect("index.jsp");
}
private void doDeleteStudent(HttpServletRequest request, HttpServletResponse response) throws IOException {
String id=request.getParameter("id");
StudentAction.deleteStudent(id);
response.sendRedirect("index.jsp");
}
}
数据库表,可直接复制
JSP页面
<%@page import="com.student.vo.Student"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="com.student.action.StudentAction"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>学生管理系统</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<link id="bs-css" href="css/bootstrap-cerulean.min.css" rel="stylesheet">
<link href="css/charisma-app.css" rel="stylesheet">
</head>
<body>
<div class="box col-md-12" >
<div class="box-inner">
<div class="box-header well" data-original-title="">
<h2><i class="glyphicon glyphicon-user"></i> 学生管理系统</h2>
<div class="box-icon">
<a href="addstudent.jsp" class="btn btn-minimize btn-round btn-default"><i
class="glyphicon glyphicon-chevron-up"></i>添加学生</a>
</div>
</div>
<div class="box-content">
<table class="table table-striped table-bordered responsive" width="80%">
<thead>
<tr>
<th class="center"> 学号</th>
<th class="center"> 姓名</th>
<th> 年龄</th>
<th> 性别</th>
<