按照图片要求设计添加新课程界面。(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. css让字体细长

    transform: scale(1,3); -ms-transform: scale(1,3); -webkit-transform: scale(1,3); -moz-transform: sca ...

  2. JSP———数据交互【1】

    JSP的内置对象 不用声明就可以在JSP页面中使用 request对象 内置对象 request 封装了用户提交的信息,主要用于处理客户端请求 <FORM action="tom.js ...

  3. UrlConnection发送http请求 中文乱码解决

    中文乱码 DataOutputStream dos = new DataOutputStream(httpConn.getOutputStream()); //dos.writeBytes(jsonD ...

  4. 【嵌入式 Linux文件系统】如何使用Initramfs文件系统

    (1)#cd ../rootfs/ #ln -s ./bin/busybox init 创建软链接 (2)进入Linux内核 #make menuconfig General setup-->I ...

  5. 【Linux 源码】Linux源码比较重要的目录

    (1)arch arch是architecture的缩写.内核所支持的每一种CPU体系,该目录下都有对应的子目录. 每个CPU的子目录,又进一步分解为boot.mm.kernel等子目录,分别包含控制 ...

  6. Python模块logging

    基本用法: import logging import sys # 获取logger实例,如果参数为空则返回root logger logger = logging.getLogger("A ...

  7. nginx 事件机制原理

    事件驱动模型是Nginx服务器保障完整功能和具有良好性能的重要机制之一. 事件驱动模型概述 实际上,事件驱动并不是计算机编程领域的专业词汇,它是一种比较古老的响应事件的模型,在计算机编程.公共关系.经 ...

  8. Java 向上造型详解

    子类的对象可以向上造型为父类的类型.即父类引用子类对象,这种方式被称为向上造型. 在日常生活中,我们都扮演着不一样的角色.我们有可能是老师,有可能是学生,有可能是……, 但是我们都有共同的属性,例如: ...

  9. C++中对象的构造顺序

    1,C++ 中的类可以定义多个对象,那么对象构造顺序是怎样的? 1,很多的 bug 是由对象的构造顺序造成的,虽然它不难: 2,对象的构造往往和构造函数牵涉在一起,构造函数的函数体又可能由非常复杂的程 ...

  10. [集合]HashMap和Hashtable区别

    底层 哈希算法,双列集合 HashMap 线程不安全,效率高,JDK1.2版本    Hashtable 线程安全,效率低,JDK1.0版本 HashMap 可以存储 null 键和 null 值   ...