设计思想:
编写Java代码实现对数据库的增删改查,创建jsp页面表单里表单里面有,课程名称,任课教师,上课地址,,对每个输入的文本进行判断,输出是否符合要求,如果符合要求则跳转到下个页面,向数据库里面添加数据,然后输出成功
代码:
//index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
<form name="form1" action="add.jsp" method="get" onSubmit="check()">
<table align="center" border="1" width="500">
<tr>
<td>课程名称 : </td>
<td>
<input type="text" name="kechengname" />
</td>
</tr>
<tr>
<td>教师名称:</td>
<td>
<input type="text" name="teachername" />
</td>
</tr>
<tr>
<td>上课地点:</td>
<td>
<input type="text" name="address" />
</td>
</tr>
<tr align="center">
<td colspan="2">
<input type="submit" value="提交" />
<input type="reset" value="重置" />
</td>
</tr>
</table>
</form>
<script type="text/javascript">
function check(){
if(form1.teachername.value!="wjm"&&form1.teachername.value!="llj"){
alert("请输入正确教师名!");
form1.teachername.focus();
return;
}
else if(form1.address.value!="jijiao"&&form1.address.value!="erjiao"){
alert("请输入密码!");
form1.pwd.focus();
return;
}
return true;
}
</script>
</body>
</html>
//add.jsp
<%@page import="com.jaovo.msg.Util.UserException"%>
<%@page import="com.jaovo.msg.dao.UserDaoImpl"%>
<%@page import="com.jaovo.msg.model.User"%>
<%@ 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>Insert title here</title>
</head>
<%
String kechengname = request.getParameter("kechengname");
String teachername = request.getParameter("teachername");
String address = request.getParameter("address");
User user = new User();
user.setKechengname(kechengname);
user.setTeachername(teachername);
user.setAddress(address);
UserDaoImpl userDao = new UserDaoImpl();
try{
userDao.add(user);
out.print("保存成功!!!");
%>
<%
}catch(UserException e){
%>
<h2 style="color:red ; font-size:50px">发生错误 : <%=e.getMessage() %></h2>
<%
}
%>
</html>
运行结果:
总结:
本次实验的难点在于对数据库的增删改查,网上可以搜到对SQL server的sql语句,改一下可以直接用,还有一个难点是判断输入的信息是否符合要求,这个可以用脚本语言function 函数,判断信息,相关函数为String的equals()和startwith()函数