码迷,mamicode.com
首页 > 其他好文 > 详细

软件工程概论第一次上机作业

时间:2017-11-28 19:55:46      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:doctype   html 4.01   vax   w3c   必须   ted   传输   getc   request   

设计思路:

源代码:

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="project7.DBUtil"%>
<%@page import="project7.Servlet"%>
<%@page import="project7.User"%>
<%@page import="project7.UserDaoImpl"%>


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript">
function check(form){
course=form.course.value;
teacher=form.teacher.value;
place=form.place.value;
if(course==""){
alert("课程名字不能为空");
return false;
}
if(teacher!="王建民"&&teacher!="刘力嘉"&&teacher!="刘丹"&&teacher!="王辉"&&teacher!="杨子光"){
alert("老师必须为王建民 刘立嘉 刘丹 王辉 杨子辉");
return false;
}
if(place!="一教"&&place!="二教"&&place!="三教"&&place!="基教"){
alert("教室必须为一教 二教 三教 基教");
return false;
}
return true;
}
</script>
<title>Insert title here</title>
</head>
<body>

<form name="form1" method="post" onsubmit="return check(form1)" action="${pageContext.request.contextPath}/Servlet?method=add">
<table>
<tr>
<td>课程名称</td>
<td><input type="text" name="course"></td>
</tr>
<tr>
<td>任课教师</td>
<td><input type="text" name="teacher"></td>
</tr>
<tr>
<td>上课地点</td>
<td><input type="text" name="place"></td>
</tr>
<tr>
<td><input type="submit" value="保存"></td>
</tr>

</table>

</form>

</body>
</html>

 

package project7;

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;

/**
* Servlet implementation class Servlet
*/
@WebServlet("/Servlet")
public class Servlet extends HttpServlet {
private static final long serialVersionUID = 1L;

public void service(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
//获取method判断调用哪一个方法
request.setCharacterEncoding("utf-8");
String method=request.getParameter("method");
switch(method){
case "add" :add(request,response);break;
case "delete":delete(request,response);break;
case "update":update(request,response);break;
}
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//获取具体信息
request.setCharacterEncoding("utf-8");
String course=request.getParameter("course");
String teacher=request.getParameter("teacher");
String place=request.getParameter("place");
UserDaoImpl userDao=new UserDaoImpl();
User user= new User();
user.setCourse(course);
user.setTeacher(teacher);
user.setPlace(place);
userDao.add(user);
response.sendRedirect("./Folder/list.jsp");
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
UserDaoImpl userDao=new UserDaoImpl();
int id=Integer.parseInt(request.getParameter("id"));
userDao.delete(id);
response.sendRedirect("./Folder/list.jsp");

}
protected void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

}
}

 

 

 

package project7;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

 

public class UserDaoImpl {
public void add(User user) {
//获得链接对象
Connection connection = DBUtil.getConnection();
String sql = "select count(*) from table4";
//创建语句传输对象
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
//添加信息到SQL
sql = "insert into table4(course,teacher,place) values(?,?,?)";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, user.getCourse());
preparedStatement.setString(2, user.getTeacher());
preparedStatement.setString(3, user.getPlace());
preparedStatement.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
//关闭资源
DBUtil.close(resultSet);
DBUtil.close(preparedStatement);
DBUtil.close(connection);
}

}
public void delete(int id) {
Connection connection = DBUtil.getConnection();
String sql = "delete from table4 where id = ?";
PreparedStatement preparedStatement = null;

try {
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, id);
preparedStatement.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
DBUtil.close(preparedStatement);
DBUtil.close(connection);
}

}
public void update(User user) {
Connection connection = DBUtil.getConnection();
//准备sql语句
String sql = "update table4 set password = ? , nickname=? where id = ?";
//创建语句传输对象
PreparedStatement preparedStatement = null;
try {
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, user.getCourse());
preparedStatement.setString(2, user.getTeacher());
preparedStatement.setString(3, user.getPlace());
preparedStatement.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
DBUtil.close(preparedStatement);
DBUtil.close(connection);
}
}

public List<User> load() {
Connection connection = DBUtil.getConnection();
//准备sql语句
String sql = "select * from table4 ";
//创建语句传输对象
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
//集合中只能放入user对象
List<User> users = new ArrayList<User>();
User user = null;
try {
preparedStatement = connection.prepareStatement(sql);
resultSet = preparedStatement.executeQuery();
while(resultSet.next()) {
user = new User();
user.setCourse(resultSet.getString(1));;
user.setTeacher(resultSet.getString(2));
user.setPlace(resultSet.getString(3));
users.add(user);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
DBUtil.close(resultSet);
DBUtil.close(preparedStatement);
DBUtil.close(connection);
}
return users;
}

}

软件工程概论第一次上机作业

标签:doctype   html 4.01   vax   w3c   必须   ted   传输   getc   request   

原文地址:http://www.cnblogs.com/t1314/p/7911539.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!