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是所有类的祖先 ...
随机推荐
- oracle存储过程和存储函数
存储过程 1.存储过程简介 下面先来简单介绍一下oracle的存储过程的语法,如下: create or replace procedure Tony_Process ( num in number, ...
- 【ABAP系列】SAP 如何用ABAP实现自动发送外部邮件
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP 如何用ABAP实现自动发 ...
- sklearn+nltk ——情感分析(积极、消极)
转载:https://www.iteye.com/blog/dengkane-2406703 步骤: 1 有标签的数据.数据:好评文本:pos_text.txt 差评文本:neg_text.txt ...
- Linux 自学shell
1.多个命令用";"分号分割 还可以使用alias 给命令取别名 alias foo='cd /usr ; ls; cd -'2.使用管道线"|" 一个命令的标 ...
- CentOS安装部署sha##dow**socks
注意事项,pip版本不能太低,实测9.0.3可行(需要python 2.7,低版本python升级办法另有文章介绍). pip --version 以shadowsocks-2.8.2为例: pip ...
- AngularJS——基础小知识(一)
$time0ut :定时器 $rootscope :全局的 $scope : 局部的作用域: 它下面的方法: $scope.$watch $scope.$apply 1)$scope.$wat ...
- [转帖]IDC发布2018下半年中国公有云市场报告
IDC发布2018下半年中国公有云市场报告:AWS以6.4%的份额名列第四 http://www.itpub.net/2019/05/06/1793/ 电信的公有云 好像是用的 华为的技术. AWS在 ...
- FTP-学习笔记(1)
1.简单的SFTP.FTP文件上传下载 SftpTools.java package com.lfy.mian; import com.jcraft.jsch.*; import java.io.Fi ...
- Java——HashMap使用Demo
package map; import java.util.Collection; import java.util.HashMap; import java.util.Set; public cla ...
- JAVA基础--JAVA 集合框架(泛型、file类)
一.集合总结 集合:Collection体系.Map体系. Collection体系:单列集合的共性操作规则. List:列表,可以重复,有下标,拥有特有的迭代器ListIterator. Array ...