1.设计思想

先写类DBUtil用来连接数据库。在UserDaoImpl2类中写在数据库中添加课程表信息的方法。然后定义类Calss2来写保存超级课表数据:课程名称,任课教师,上课地点的属性及其get和set方法。新建Calss.jsp文件来写保存功能的界面,doClass.jsp文件来接受Class.jsp文件传过来的参数,如果参数不正确,则给出提示并且依旧在保存功能页面,如果正确则跳转页面提示保存成功。新建ValidateUtil类,在类中的方法中判断三个参数是否正确,若不正确并返回错误信息。在doCalss类中调用此方法。

2.源程序代码

package com.jaovo.msg.Util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException; public class DBUtil { public static Connection getConnection() {
try {
//1 加载驱动
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String user = "root";
String password = "root";
String url = "jdbc:mysql://localhost:3307/jaovo_msg";//定义工具类
Connection connection = null;
try {
//2 创建链接对象connection
connection = DriverManager.getConnection(url,user,password);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return connection;
} //关闭资源的方法
public static void close(Connection connection ) {
try {
if (connection != null) {
connection.close();
} } catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void close(PreparedStatement preparedStatement ) {
try {
if (preparedStatement != null) {
preparedStatement.close();
} } catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void close(ResultSet resultSet ) {
try {
if (resultSet != null) {
resultSet.close();
} } catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
package com.jaovo.msg.model;

public class Class2 {
private String classname;
private String teacher;
private String address;
public String getClassname() {
return classname;
}
public void setClassname(String classname) {
this.classname = classname;
}
public String getTeacher() {
return teacher;
}
public void setTeacher(String teacher) {
this.teacher = teacher;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
} }
package com.jaovo.msg.dao;
import com.jaovo.msg.model.*; public interface IUserDao2 {
public void add(Class2 class2);
}
package com.jaovo.msg.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException; import com.jaovo.msg.Util.DBUtil; import com.jaovo.msg.model.Class2; public class UserDaoImpl2 implements IUserDao2{
@Override
public void add(Class2 class2) {
//获得链接对象
Connection connection = DBUtil.getConnection();
//准备sql语句 PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
String sql = "insert into t_class(classname,teacher,address) value (?,?,?)";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, class2.getClassname());
preparedStatement.setString(2,class2.getTeacher());
preparedStatement.setString(3,class2.getAddress());
preparedStatement.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
//关闭资源
DBUtil.close(resultSet);
DBUtil.close(preparedStatement);
DBUtil.close(connection);
} } }
 


package com.jaovo.msg.Util;

import java.util.HashMap;
import java.util.Map; import javax.servlet.http.HttpServletRequest; public class ValidateUtil {
public static boolean validateNull(HttpServletRequest request,String[] fileds) {
boolean validate = true;
//map对象用来装载不同的错误信息
Map<String,String> errorMsg = new HashMap();
for(int i=0;i<3;i++) {
String value = request.getParameter(fileds[i]); if(i==1) { if(value.equals("王建民")||value.equals("刘立嘉")||value.equals("刘丹")||value.equals("王辉")||value.equals("杨子光"))
{
validate=true;
}
else
{
validate = false;
errorMsg.put(fileds[i], "教师不正确");
}
} if(i==2) { boolean a[]=new boolean[4];
String[] sub={"一教","二教","三教","基教"};
for(int j=0;j<4;j++) {
a[i]=value.startsWith(sub[j]);
}
boolean is=false;
for(int k=0;k<4;k++) {
if(a[i]==true)
is=true;
} if(is==true)
{
validate=true;
}
else
{
validate = false;
errorMsg.put(fileds[i], "教室不正确");
} if (!validate) {
request.setAttribute("errormsg", errorMsg);
}
}
} return validate;
}
public static String showError(HttpServletRequest request , String filed) {
Map<String, String> errorMsg = (Map<String,String>)request.getAttribute("errormsg");
if (errorMsg == null) {
return "";
}
String msg = errorMsg.get(filed);
if (msg == null) {
return "";
}
return msg;
} }

 

<%@page import="java.util.Map"%>
<%@ page import="com.jaovo.msg.Util.ValidateUtil" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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">
<title>Insert title here</title>
</head>
<body>
<%
Map<String,String> errorMsg=(Map<String,String>)request.getAttribute("errormsg");
%> <form action="doClass.jsp" method="post">
<table align="center" width="50%" border="1">
<tr><td>课程名称</td>
<td><input type="text" name="classname"/>
<%=ValidateUtil.showError(request, "classname")%></td></tr> <tr><td> 任课教师</td>
<td><input type="text" name="teacher"/>
<%=ValidateUtil.showError(request, "teacher")%></td></tr>
<tr><td> 上课地点</td>
<td><input type="text" name="address"/>
<%=ValidateUtil.showError(request, "address")%> </td></tr>
<tr><td colspan="2" align="center">
<input type="submit" value="保存"/>
</td></tr> </table>
</form> </body>
</html>
<%@page import="com.jaovo.msg.dao.UserDaoImpl2"%>
<%@page import="com.jaovo.msg.model.Class2"%>
<%@page import="java.util.Map"%>
<%@ page import="com.jaovo.msg.Util.ValidateUtil" %> <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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">
<title>Insert title here</title>
</head>
<body>
<%
//接收客户端传递过来的参数
String classname= request.getParameter("classname");
String teacher= request.getParameter("teacher");
String address = request.getParameter("address");
boolean validate = ValidateUtil.validateNull(request, new String[]{"classname","teacher","address"});
if(!validate){ %>
<jsp:forward page="Class.jsp"></jsp:forward>
<%
}
Class2 class2 = new Class2();
class2.setClassname(classname);
class2.setTeacher(teacher);
class2.setAddress(address);
UserDaoImpl2 userDao2 = new UserDaoImpl2();
userDao2.add(class2);
out.println("添加成功!!");
%> </body>
</html>

 

课程增加功能(java web)的更多相关文章

  1. Java Web -【分页功能】详解

    分页简介 分页功能在网页中是非常常见的一个功能,其作用也就是将数据分割成多个页面来进行显示. 使用场景: 当取到的数据量达到一定的时候,就需要使用分页来进行数据分割. 当我们不使用分页功能的时候,会面 ...

  2. java web课程设计截图和服务器地址

    企业办公测试截图和服务器地址 本篇博客主要围绕以下几个部分展开,登录.系统管理.工作流.个人事务管理.内部邮件.公共信息共六个部分.主要有界面截图和简要介绍. 一.登录.更改密码界面 登录界面包括以管 ...

  3. java web实现 忘记密码(找回密码)功能及代码

    java web实现 忘记密码(找回密码)功能及代码 (一).总体思路 (二).部分截图 (三).部分代码 (一).总体思路: 1.在 找回密码页面 录入 姓名.邮箱和验证码,录入后点击[提交]按钮, ...

  4. 【Servlet】java web 文件下载功能实现

    需求:实现一个具有文件下载功能的网页,主要下载压缩包和图片 两种实现方法: 一:通过超链接实现下载 在HTML网页中,通过超链接链接到要下载的文件的地址 <!DOCTYPE html> & ...

  5. atitit.D&D drag&drop拖拽文件到界面功能 html5 web 跟个java swing c#.net c++ 的总结

    atitit.D&D drag&drop拖拽文件到界面功能 html5 web 跟个java swing c#.net c++ 的总结 1. DND的操作流程 1 2. Html5 注 ...

  6. java web开发_购物车功能实现

    java web开发_购物车功能实现 之前没有接触过购物车的东东,也不知道购物车应该怎么做,所以在查询了很多资料,总结一下购物车的功能实现. 查询的资料,找到三种方法: 1.用cookie实现购物车: ...

  7. java web Session会话技术(原理图解+功能+与Cookie的区别+基本使用)

    java web Session会话技术(原理图解+功能+与Cookie的区别+基本使用) 这是我关于会话技术的第二篇文章,对 Cookie有不了解的兄弟可以点击下方的Cookie跳转 Cookie链 ...

  8. Java Web实现用户登录功能

    java web 学习记录一下 mvc结构实现mysql 连接 什么是mvc MVC是模型(model).视图(view).控制(controller)这三个单词上的首字母组成.它是一种应用模型,它的 ...

  9. Java Web快速入门——全十讲

    Java Web快速入门——全十讲 这是一次培训的讲义,就是我在给学生讲的过程中记录下来的,非常完整,原来发表在Blog上,我感觉这里的学生可能更需要. 内容比较长,你可以先收藏起来,慢慢看. 第一讲 ...

随机推荐

  1. HDU 6170 Two strings (dp)

    /** * 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6170 * 字符串match, '.'代表匹配任意一个字符,"*" 代表 ...

  2. codeforces 558C C. Amr and Chemistry(bfs)

    题目链接: C. Amr and Chemistry time limit per test 1 second memory limit per test 256 megabytes input st ...

  3. 集训Day13

    我这个肥宅一点都不快乐嘤嘤嘤 bzoj3456 求n个点的无向简单连通图的个数 我们可以用容斥推出递推式 首先我们令为 于是有 这样就是可以用CDQ分治解决的一类递推式了 不是那么明显的变形一下得到 ...

  4. ACM学习历程—HDU4956 Poor Hanamichi(模拟)

    Poor Hanamichi Problem Description Hanamichi is taking part in a programming contest, and he is assi ...

  5. Python中定时任务框架APScheduler的快速入门指南

    前言 大家应该都知道在编程语言中,定时任务是常用的一种调度形式,在Python中也涌现了非常多的调度模块,本文将简要介绍APScheduler的基本使用方法. 一.APScheduler介绍 APSc ...

  6. 用linqpad来插入多条数据

    其中Customers为数据库的某个表名, Custom自动被默认为单条记录的对象, 利用构造,InsertOnSubmit, 以及SubmitChanges实现插入数据. 注意:linqpad的la ...

  7. 【转】unittest执行测试用例的N种姿势总结

    原文地址:https://www.cnblogs.com/fighter007/p/9514453.html 1.我们写几个方法,用来做测试用例 2.我们在另一文件中引用这个模块下面的所有类方法,先看 ...

  8. MODBUS ASCII和RTU两种模式的区别、优缺点

    转自:http://blog.sina.com.cn/s/blog_89f286ad0102uzju.html 下表是MODBUS ASCII协议和RTU协议的比较: 协议 开始标记 结束标记 校验 ...

  9. SQLServer中连接个数及超时问题

    超时时间已到.超时时间已到,但是尚未从池中获取连接.出现这种情况可能是因为所有池连接均在使用,并且达到了最大池大小. 解决办法1.在代码里面,把未关闭的连接关闭2.扩大共享池,方法如下:解决方法可以是 ...

  10. 利用html5看雪花飘落的效果

    html5飘落的雪花堆积动画特效 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-T ...