java web 增加信息课堂测试00
按照图片要求设计添加新课程界面。(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的更多相关文章
- 20172306 2018-2019《Java程序设计与数据结构课堂测试补充报告》
学号 2017-2018-2 <程序设计与数据结构>课堂测试补充报告 课程:<程序设计与数据结构> 班级: 1723 姓名: 刘辰 学号:20172306 实验教师:王志强 必 ...
- java web 学生信息录入
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- Java Web学生信息保存
Course.javapackage entity; public class Course { private int id; private String num; private String ...
- 20145209刘一阳《JAVA程序设计》第九周课堂测试
第九周课堂测试 1.域名解析服务器(ARP)负责将域名转化为IP地址,从而与主机连接.(B) A .true B .false 2.下列关于URL类的说法,正确的是(BD) A .URL 类自身可根据 ...
- 20145209刘一阳《JAVA程序设计》第一周课堂测试
第一周课堂测试 1.下列不属于Java后继技术的是(D) A .Android B .JSP C .XML D .Python 2.下列关于Java语言特点的描述,正确的一组是(C) A .面向过程: ...
- 渗透测试之信息收集(Web安全攻防渗透测试实战指南第1章)
收集域名信息 获得对象域名之后,需要收集域名的注册信息,包括该域名的DNS服务器信息和注册人的联系方式等. whois查询 对于中小型站点而言,域名所有人往往就是管理员,因此得到注册人的姓名和邮箱信息 ...
- 2017-2018-1 20155232 《信息安全系统设计基础》第十周课堂测试(ch06)补交
# 2017-2018-1 20155232 <信息安全系统设计基础>第十周课堂测试(ch06)补交 上课时完成测试后在提交的时候,没有提交成功,进行补交. 1.下面代码中,对数组x填充后 ...
- 20145209刘一阳《JAVA程序设计》第五周课堂测试
第五周课堂测试 1.下列关于内部类的说法,正确的是(ABD) A .其他类不可以用某个类的内部类声明对象. B .内部类字节码文件的名字格式是"外嵌类名$内部类名". C .内部类 ...
- 20145209刘一阳《JAVA程序设计》第四周课堂测试
第四周课堂测试 1.下列说法正确的是(ACD) A .使用extends关键字定义一个类的子类. B .Java与C++类似,支持多继承,即子类可以有一个或多个父类. C .Object是所有类的祖先 ...
随机推荐
- Django学习之Cookie和Session
一.Cookie 1.Cookie的由来 2.什么是Cookie 3.Cookie的原理 4.查看Cookie 二.Django中操作Cookie 1.获取Cookie 2.设置Cookie 3.删除 ...
- Spring 缓存切面
缓存切面:[通知+目标方法调用] 缓存操作执行过程: 1)如果是同步调用[sync=true],则首先尝试从缓存中读取数据,读取到则直接返回: 否则执行目标方法,将结果缓存后返回. 2)如果不是同步调 ...
- golang 标准库 sync.Map 中 nil 和 expunge 区别
本文不是 sync.Map 源码详细解读,而是聚焦 entry 的不同状态,特别是 nil 状态和 expunge 状态的区分. entry 是 sync.Map 存放值的结构体,其值有三种,分别为 ...
- jmeter线程组基本设置
线程组基本设置 在线程组界面中可以设置以下数据,进行控制线程组: 1.取样器错误后要执行的动作: 继续:忽略错误,继续执行 Start Next Thread Loop: 忽略错误,线程当前循环终止, ...
- Python文档操作
1.打开和关闭文件 open('C:\Users\Second One\Desktop\a.txt')文件路径必须完整路径且为字符串格式 有三种方式: open('C:\\Users\\Second ...
- Discrete Mathematics and Its Applications | 1 CHAPTER The Foundations: Logic and Proofs | 1.2 Applications of Propositional Logic
Translating English Sentences System Specifications Boolean Searches Logic Puzzles Logic Circuits
- unittest中的断言方法
方法 用途 assertEqual(a,b) a=b assertNotEqual(a,b) a!=b assertTrue(x) x为True assertFa ...
- python 并发编程 多进程 模拟抢票
抢票是并发执行 多个进程可以访问同一个文件 多个进程共享同一文件,我们可以把文件当数据库,用多个进程模拟多个人执行抢票任务 db.txt {"count": 1} 并发运行,效率高 ...
- 第六周学习总结&(实验报告四)
一.实验目的 (1)掌握类的继承方法 (2)变量的继承和覆盖,方法的继承,重载和覆盖实现 二.实验内容 一.实验目的 (1)掌握类的继承 (2)变量的继承和覆盖,方法的继承,重载和覆盖的实现: 二.实 ...
- C#打印条码BarTender SDK打印之路和离开之路(web平凡之路)(转)
C#打印条码BarTender SDK打印之路和离开之路(web平凡之路) 从来没想过自己会写一篇博客,鉴于这次从未知的探索到一个个难点的攻破再到顺利打印,很想记录这些点滴,让后人少走弯路. 下面走进 ...