按照图片要求设计添加新课程界面。(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. EMQ插件通过HTTP连接认证服务器实现认证

    需求 在EMQ中添加认证插件,将到来的MQTT连接的ClientID.UserName.Password通过HTTP协议发送到认证服务器,用返回的数据决定是否允许该连接: 在连接时和断开时向服务器发送 ...

  2. VS调试异常代码 HRESULT:0x80070057 (E_INVALIDARG)解决方法

    我目前在做的一个系统是VS2010写的的B/S架构程序, 主要技术是:C#.SQLSERVER2008.NHibernate,Python,Nhibernate 的*.hbn.xml是映射数据库的表结 ...

  3. PHP 对Memcache的使用实例

    <?php //连接Memcache$mem = new Memcache;$mem->connect("localhost", 11211) or die (&quo ...

  4. 一次性计划任务at与周期性计划任务crontab

    一.at一次性计划任务使用 at语法格式: at 时间 at设置计划任务 1.下载at程序 [root@li ~]# yum install at -y 2.启动atd服务 [root@li ~]# ...

  5. codeforces 1156E Special Segments of Permutation

    题目链接:https://codeforc.es/contest/1156/problem/E 题目大意: 在数组p中可以找到多少个不同的l,r满足. 思路: ST表+并查集. ST表还是需要的,因为 ...

  6. ELK7.4.0分析nginx json日志

    ELK7.4.0单节点部署 环境准备 安装系统,数据盘设置为/srv 内核优化参考 我们需要创建elk专用的账号,并创建所需要的目录并授权 useradd elk; mkdir /srv/{app,d ...

  7. js 中 json.stringfy()将对象、数组转换成字符串

    json.stringfy()将对象.数组转换成字符串 var student = new Object(); student.name = "Lanny"; student.ag ...

  8. adb 连接 mumu 模拟器

    [win版]adb connect 127.0.0.1:7555adb shell [mac版] adb kill-server && adb server && ad ...

  9. 通过Spark Streaming处理交易数据

    Apache Spark 是加州大学伯克利分校的 AMPLabs 开发的开源分布式轻量级通用计算框架. 由于 Spark 基于内存设计,使得它拥有比 Hadoop 更高的性能(极端情况下可以达到 10 ...

  10. 嵌入式软件工程师C语言经典笔试2

    1. 使用宏定义swap函数,不使用中间变量 #define swap(x,y) {(x) = (x) + (y);(y) = (x) - (y);(x) = (x) - (y)} 2. 实现字符串的 ...