按照图片要求设计添加新课程界面。(0.5分)
在后台数据库中建立相应的表结构存储课程信息。(0.5分)
实现新课程添加的功能。
要求判断任课教师为王建民、刘立嘉、刘丹、王辉、杨子光五位教师的其中一位。(0.5分)
要求上课地点开头为“一教、二教、三教、基教”中的一种。(0.5分)
实现数据存储功能。(3分)

设计思想:绘制jsp界面 、编写功能类、信息类、工具类、连接数据库,利用传参取到web界面的输入内容添加到数据库。

根据输入内容与要求判断的老师、地点对比判断。  查重课程: 从数据库中取值与键入web文本框内容对比,内容全部相同则存在课程。

源代码:

声明类

 public class test {
private String name;
private String teacher;
private String area;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTeacher() {
return teacher;
}
public void setTeacher(String teacher) {
this.teacher = teacher;
}
public String getArea() {
return area;
}
public void setArea(String area) {
this.area = area;
}
}

接口

 import studytest.model.test;

 public interface  Itest {
public void add(test test);
}

数据库工具类

 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 {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} String user="liu";
String password="980130";
String url="jdbc:sqlserver://localhost:1433; DatabaseName=Student";
Connection connection =null; try {
connection=DriverManager.getConnection(url, user, password);
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} return connection; }
//�ر���Դ
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();
}
} public static void close(Connection connection) {
// TODO Auto-generated method stub try {
if(connection!=null) {
connection.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

123

 
 
 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 {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} String user="**";
String password="****";
String url="jdbc:sqlserver://localhost:1433; DatabaseName=Student";
Connection connection =null; try {
connection=DriverManager.getConnection(url, user, password);
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} return connection; }
//�ر���Դ
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();
}
} public static void close(Connection connection) {
// TODO Auto-generated method stub try {
if(connection!=null) {
connection.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

异常处理类

 @SuppressWarnings("serial")
public class testException extends RuntimeException {
public testException() {
super();
// TODO Auto-generated constructor stub
} public testException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
} public testException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
} public testException(String message) {
super(message);
// TODO Auto-generated constructor stub
} public testException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
} }

add类

 <%@page import="studytest.Util.testException"%>
<%@page import="studytest.dao.testdaoimpl"%>
<%@page import="studytest.model.test"%>
<%@ 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>添加</title>
</head>
<body>
<%
//接收客户端传递过来的参数 String name = request.getParameter("name");
name = new String(name.getBytes("ISO-8859-1"), "UTF-8");
String teacher= request.getParameter("teacher");
teacher = new String(teacher.getBytes("ISO-8859-1"), "UTF-8");
String area = request.getParameter("area");
area= new String(area.getBytes("ISO-8859-1"), "UTF-8");
//String[] tea={"王建民","刘立嘉","刘丹","杨辉","杨子光"};
//String[] ar={"一教","二教","三教","基教"};
test test=new test();
test.setName(name);
test.setTeacher(teacher);
test.setArea(area); testdaoimpl testdao=new testdaoimpl(); try{ if((teacher.equals("王建民"))||(teacher.equals("刘立嘉"))||(teacher.equals("刘丹"))
||(teacher.equals("王辉"))||(teacher.equals("杨子光")))
{ if((area.startsWith("基教"))||(area.startsWith("一教"))
||(area.startsWith("二教"))||(area.startsWith("三教"))){
testdao.add(test);
%>
<script type="text/javascript">
alert("课程添加成功!!");
</script>
<%
}
}
else{
%>
<script type="text/javascript">
alert("发生错误");
</script> <%
} }catch(testException e){
%>
<h2 style="color:red ; font-size:20px">发生错误 : <%=e.getMessage() %></h2>
<%
}
%> </body>
</html>

addinput

 <%@page import="java.util.Map"%>
<%@page import="com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg"%>
<%@ 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>
<title>用户添加页面</title>
<style>
#header{
background-color:#0000FF;
color:white;
text-align:left;
padding:3px;
}
#section1{
background-color:white;
width:350px;
text-align:center;
margin: 0 auto;
padding:10px; }
</style>
</head>
<body bgcolor="pink" >
<div id="header">
<h1>用户注册</h1>
</div> <br><br><br><br><br><br>
<body> <form action="add.jsp" method="get">
<table align="center" border="1" width="500">
<tr>
<td>课程名称 : </td>
<td>
<input type="text" name="name" /> </td>
</tr>
<tr>
<td>任课教师:</td>
<td>
<input type="text" name="teacher" /> </tr>
<tr>
<td>上课地点:</td>
<td>
<input type="text" name="area" /> </td>
</tr>
<tr align="center">
<td colspan="2">
<input type="submit" style="background:#4DFFFF" value="保存" />
</td>
</tr>
</table>
</form>
</body>
</html>

接口调用 连接数据库添加内容

 import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException; import com.sun.xml.internal.bind.v2.runtime.Name; import studytest.Util.DBUtil;
import studytest.Util.testException;
import studytest.model.test; public class testdaoimpl implements Itest{ @SuppressWarnings("resource")
@Override
public void add(test test) {
// TODO Auto-generated method stub //获得连接对象
Connection connection=DBUtil.getConnection();
//准备sql语句
String s="select * from test where name=?";
//创建语句传输对象
PreparedStatement preparedStatement= null;
ResultSet resultSet =null;
try {
preparedStatement = connection.prepareStatement(s);
preparedStatement.setString(1, test.getName());
//接收结果集
resultSet =preparedStatement.executeQuery(); //遍历结果集
while(resultSet.next()) {
if(resultSet.getString("name").equals(test.getName())&&
resultSet.getString("teacher").equals(test.getTeacher())&&
resultSet.getString("area").equals(test.getArea())) {
throw new testException("课程存在 ");
}
} s ="insert into test(name,teacher,area) values(?,?,?)";
preparedStatement =connection.prepareStatement(s);
preparedStatement.setString(1, test.getName());
preparedStatement.setString(2, test.getTeacher());
preparedStatement.setString(3, test.getArea());
preparedStatement.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
DBUtil.close(resultSet);
DBUtil.close(preparedStatement);
DBUtil.close(connection); }

截图:

java web 增加信息课堂测试00的更多相关文章

  1. 20172306 2018-2019《Java程序设计与数据结构课堂测试补充报告》

    学号 2017-2018-2 <程序设计与数据结构>课堂测试补充报告 课程:<程序设计与数据结构> 班级: 1723 姓名: 刘辰 学号:20172306 实验教师:王志强 必 ...

  2. java web 学生信息录入

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  3. Java Web学生信息保存

    Course.javapackage entity; public class Course { private int id; private String num; private String ...

  4. 20145209刘一阳《JAVA程序设计》第九周课堂测试

    第九周课堂测试 1.域名解析服务器(ARP)负责将域名转化为IP地址,从而与主机连接.(B) A .true B .false 2.下列关于URL类的说法,正确的是(BD) A .URL 类自身可根据 ...

  5. 20145209刘一阳《JAVA程序设计》第一周课堂测试

    第一周课堂测试 1.下列不属于Java后继技术的是(D) A .Android B .JSP C .XML D .Python 2.下列关于Java语言特点的描述,正确的一组是(C) A .面向过程: ...

  6. 渗透测试之信息收集(Web安全攻防渗透测试实战指南第1章)

    收集域名信息 获得对象域名之后,需要收集域名的注册信息,包括该域名的DNS服务器信息和注册人的联系方式等. whois查询 对于中小型站点而言,域名所有人往往就是管理员,因此得到注册人的姓名和邮箱信息 ...

  7. 2017-2018-1 20155232 《信息安全系统设计基础》第十周课堂测试(ch06)补交

    # 2017-2018-1 20155232 <信息安全系统设计基础>第十周课堂测试(ch06)补交 上课时完成测试后在提交的时候,没有提交成功,进行补交. 1.下面代码中,对数组x填充后 ...

  8. 20145209刘一阳《JAVA程序设计》第五周课堂测试

    第五周课堂测试 1.下列关于内部类的说法,正确的是(ABD) A .其他类不可以用某个类的内部类声明对象. B .内部类字节码文件的名字格式是"外嵌类名$内部类名". C .内部类 ...

  9. 20145209刘一阳《JAVA程序设计》第四周课堂测试

    第四周课堂测试 1.下列说法正确的是(ACD) A .使用extends关键字定义一个类的子类. B .Java与C++类似,支持多继承,即子类可以有一个或多个父类. C .Object是所有类的祖先 ...

随机推荐

  1. Git-Runoob:Git 基本操作

    ylbtech-Git-Runoob:Git 基本操作 1.返回顶部 1. Git 基本操作 Git 的工作就是创建和保存你项目的快照及与之后的快照进行对比.本章将对有关创建与提交你的项目快照的命令作 ...

  2. 写入mongodb

    https://blog.csdn.net/u013421629/article/details/78885079 https://www.jianshu.com/p/7d14c3ad810f  可视 ...

  3. php5.6编译安装apache

    1.下载源码包wget 网址/php-5.6.30.tar.gz2.解压源码包tar -zxvf php-5.6.30.tar.gz3.创建一个安装目录mkdir /usr/local/php4.进入 ...

  4. 阶段3 1.Mybatis_08.动态SQL_03.mybatis中动态sql语句-foreach和sql标签

    foreach标签 in的查询 sql语句好写,但是传参在映射文件里面改怎么传呢 定义一个List<Integer>成员变量,然后生成get和set 定义一个新的查询方法 open:开始符 ...

  5. 阶段3 1.Mybatis_06.使用Mybatis完成DAO层的开发_2 Mybatis中编写dao实现类的使用-保存操作

    再完善.saveUser的方法 测试保存的操作 报错了 SqlSession的insert的源码 我们在执行Insert的时候,并没有把user对象传过去 usersex改成sex 再次测试

  6. python学习笔记:(八)条件语句

    if语句,python中if语句的一般形式如下: conditon1为真,执行statement_block_1 condition1为假,判断conition_2,如果condition_2为真,执 ...

  7. android 程序的运行步骤(备忘)

    java代码: public class HelloWorld { public static void main(String[] args) { System.out.println(" ...

  8. elasticsearch7.0安装及配置优化

    简单讲ES开箱即用,不用任何配置也能玩转搜索引擎:以下内容是根据易企秀线上实际使用场景进行的安装和配置,支持冷热数据分离 1.安装 Linux 环境下载安装包 curl -L -O https://a ...

  9. 2019暑假第二周(hadoop在个人电脑上的搭建)

    一,Hadoop和NoSQL数据库的学习,大多需要Linux环境. 搭建Linux环境可以分为两种方式: (1)在电脑上安装双操作系统,即同时安装Linux和Windows操作系统,在电脑启动的时候, ...

  10. Django中的自定义过滤器

    一.为什么要自定义Django中的自定义过滤器:Django中提供了很多内置的过滤器和标签,详见链接django官网,主要有以下几个: autoescape(自动转义)block(模板继承)csrf_ ...