軟工概論第一次課堂測試
設計思想:
編寫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()函數
軟工概論第一次課堂測試