Student的实现步骤
第一步
Myeclipse 创建项目
部署项目 运行项目 --测试项目
第二步
SQLServer 创建库创建表 创建约束 (暂时不要添加数据)
第三步
创建包
Dao
BaseDao
获得链接的方法
URL=””
关闭和释放资源的方法
ResulteSet
Statement
Connection
StudentDAO extends BaseDAO
{
Public boolean addStudent(Student stu)
{
}
Public Boolean checkStudentNO(String stuNo)
{
Sql = “select count(*) from student where stuNo=?”;
Rs.getInt(1);
}
Public List getAllStudent()
{
Conncetion con
Statement st;
ResulteSet rs ;
Try{
St = con.createStatement();
Rs = st.execute();
List list = new Arraylist();
While(rs.next())
{
Student stu = new Student();
Stu.setName(rs.getString(“name”))
….
}
}catch(Exception ex)
{
}finally{
closeAll(rs,st,con);
}
}
}
Model
Data--àobject
Student
Attribute
getXXX()
setXX()
第四步
创建JSP
List.Jsp
<%
StudentDAO dao = new StudentDAO();
List list = dao.getAllStudent();
For(int i=0; i<list.size(); i++)
{
Student stu = list.get(i);
%>
<tr>
<td><%=stu.getName()%></td>
……
</tr>
<%
}
%>
第5步
页面跳转
<a href=””>添加学员</a>
<input type=”button” value=” 添加学员” οnclick=”window.location.href=’’”>
第6步
添加
<form action=”doadd.jsp” name=”myform” οnsubmit=
”return yanzheng()” >
学号: <input type=”text”>
。。。。。
<input type=”submit” value=”提交”>
<form>
<script >
function yangzheng()
{
Var ageValue = document.myForm.txtAge.value; //获得用户输入的值
If(isNaN(ageValue))
{
Alert(“年龄必须是数字”)
}else{
Var nage = parseInt(ageValue)
If(nage<16 ||nage>30)
{
Return false;
}
}
Return true;
}
</script>
第6步
实现添加
doAdd.jsp
<%@ page….
<%
//1\获得用户输入的数据
String name = request.getParameter(“txtName”);
String strAge = request.getParameter(“txtAge”);
Int age = 16;
If(strAge!=null)
{
Age = Integer.parseInt(strAge);
}
//2\将数据进行对象封装
Student stu = new Student()
Stu.setName(name);
….
//3\将对象作为参数传递给Dao实现数据库数据的添加
StudetnDAO dao = new StudentDAO();
Boolean isAdd = Dao.addStudent(stu);
//页面跳转
Response.sendRediract(“showInfo.jsp?isAdd=”+ isAdd);
%>
第7步
显示消息
showInfo.Jsp
………
<%
String addState = request.getParameter(“isAdd”);
%>
<script language=”javascript”>
If(
<%=addState!=null%>&&
<%=addState.equals(“false”)%>)
{
Alert(“添加失败”);
}else{
Alert(“添加成功”)
}
</script>
转载于:https://www.cnblogs.com/xqbk/archive/2011/11/11/2246020.html