目录
studentDao:根据studentId传入对应的src值
一、学生选题
1. 学生查看所有选题
1. 点击我要选题后出现题目数据表和题目查询
(与allTitle.html中的数据表格部分一样)
URL:'SelectAllTitleServlet'(在教师端老师查看全部选题处已经写过了)
数据表新增的内容:选题和退选按钮,引入session中的学生ID,以便后面进行对该学生的选题操作
<!-- 编辑和删除按钮 -->
<script type="text/html" id="barDemo">
<a class="layui-btn layui-btn-xs" lay-event="choose">选题</a>
<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">退选</a>
</script>
<!-- 写java代码需要百分号括起来 ,获取StudentId,实现对学生的筛选-->
<%Student student=(Student)session.getAttribute("student");
String studentId=student.getStudentId();%>
2 选题操作
1. chooseMyTitle.jsp:添加工具行监听
table.on('tool(test)',function(obj) { //注:tool 是工具条事件名,test 是 table 原始容器的属性 lay-filter="对应的值"
var data = obj.data; //获得当前行数据
if (obj.event === 'choose') { //选题,lay-event=choose
layer.confirm('确定选择该题目作为您的毕业设计题目吗?', function(index) {
$.ajax({
type:'post' ,
url:'ChooseTitleServlet?titleId='+data.titleId+"&teacherId="+data.teacherId+"&studentId="+<%=studentId%>,
/* dataType:'text', */
success:function(res){
layer.msg(res);
}
});
layer.close(index);
});
}
else if(obj.event === 'del'){
//退选,lay-event=del
}
});
2. 主要逻辑
chooseMyTitle.jsp -> ChooseTitleServlet -> StudentDao中对选题进行判断和添加
studentDao:
判断是否已有选函数:ifHasTitle
添加选题函数:chooseTitle
二、退选
studentDao添加退选函数:delTitle
三、学生上传作品
1 点击上传作品的信息展示
1. 获取学生的用户名和所选题目名称
因为学生表中设置了用户名,而titleid需要到studentinfo表中查询,所以进行连表查找操作,改写一下userdao中的selectAllStudent方法,让学生查询时顺便查询一下对应题目号,然后赋值给student类,再在studentDao中添加一个根据titleid获取对应题目信息的方法,这样便可以在uploadStudentProject.jsp使用<%%>取得学生对应的选题信息
1. 改写userdao中的selectAllStudent
!sql语句的联表操作
设置学生的titleId
public Student selectAllStudent(String name, String password) throws SQLException{
// TODO Auto-generated method stub
DBUtil dbUtil = new DBUtil();
/*"select * from s_student where name =? and password =?"*/
String sql="\r\n"+"select * from s_student left join studentinfo on s_student.studentId=studentinfo.studentId where s_student.name=? and password=?";
PreparedStatement ps=(PreparedStatement)dbUtil.getPreparedStatement(sql);
ps.setString(1, name);
ps.setString(2, password);
ResultSet rs=ps.executeQuery();
Student student= new Student();
while(rs.next()){
student.setStudentId(rs.getString("studentId"));
student.setName(rs.getString("name"));
student.setPassword(rs.getString("password"));
student.setTitleId(rs.getInt("titleId"));
return student;
}
return null;
}
2. studentDao层中根据titleID获取titlename
public String getTitleNmae(int titleId) throws SQLException {
// TODO Auto-generated method stub
DBUtil dbUtil = new DBUtil();
String sql="select name from titleinfo where titleId=?";
PreparedStatement ps=(PreparedStatement)dbUtil.getPreparedStatement(sql);
ps.setInt(1,titleId);
ResultSet rs=ps.executeQuery();
while(rs.next()) {
return rs.getString("name");
}
return null;
}
3. 在uploadStudentProject.jsp中调用
<!-- 写java代码需要百分号括起来 ,获取StudentId,实现对学生的筛选-->
<%Student student=(Student)session.getAttribute("student");
StudentDao s = new StudentDaoImpl();
String studentId=student.getStudentId();
String name = student.getName();
int titleId=student.getTitleId();
String titlename=s.getTitleNmae(titleId);
%>
</head>
<body>
<h1><%=name%>同学,毕业设计管理系统欢迎您!</h1>
<br>
<h2>您当前的选题是:<%=titlename%></h2>
2. 上传作品
借用addTitle.jsp文件中的上传
studentinfo中添加一个src属性,对应到userdao中的selectAllStudent方法中进行修改
加一句:student.setSrc(rs.getString("src"));
public Student selectAllStudent(String name, String password) throws SQLException{
// TODO Auto-generated method stub
DBUtil dbUtil = new DBUtil();
/*"select * from s_student where name =? and password =?"*/
String sql="\r\n"+"select * from s_student left join studentinfo on s_student.studentId=studentinfo.studentId where s_student.name=? and password=?";
PreparedStatement ps=(PreparedStatement)dbUtil.getPreparedStatement(sql);
ps.setString(1, name);
ps.setString(2, password);
ResultSet rs=ps.executeQuery();
Student student= new Student();
while(rs.next()){
student.setStudentId(rs.getString("studentId"));
student.setName(rs.getString("name"));
student.setPassword(rs.getString("password"));
student.setTitleId(rs.getInt("titleId"));
student.setSrc(rs.getString("src"));
return student;
}
return null;
}
对应到jsp中获取src的值
<%Student student=(Student)session.getAttribute("student");
StudentDao s = new StudentDaoImpl();
String studentId=student.getStudentId();
String src=student.getSrc();
String name = student.getName();
int titleId=student.getTitleId();
String titlename=s.getTitleNmae(titleId);
%>
ajax:
if(res){ //上传成功
var src=res.src2;
$.ajax({
url:'/test/AddStudentProjectServlet',
type:'post',
dataType:'text',
data:{
'studentId':<%=studentId%>,
'src':src
},
success:function(res){//回调函数
if(res=="success"){
alert("添加成功!");
/* document.getElementById("name").value="";
document.getElementById("type").value=""; */
}
else alert("添加失败!");
}
})
AddStudentProjectServlet
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String studentId=request.getParameter("studentId");
String src=request.getParameter("src");
System.out.println(studentId+" "+src);
int a;
try {
a = studentDao.submitProject(studentId, src);
if(a==1) {
response.getWriter().write("success");
}else {
response.getWriter().write("fail");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
studentDao:根据studentId传入对应的src值
@Override
public int submitProject(String studentId, String src) throws SQLException {
// TODO Auto-generated method stub
DBUtil dbUtil = new DBUtil();
String sql="update studentinfo set src=? where studentId=?";
PreparedStatement ps=(PreparedStatement)dbUtil.getPreparedStatement(sql);
ps.setString(1, src);
ps.setString(2, studentId);
int rs=ps.executeUpdate();
if(rs==1) {
dbUtil.commit();
return 1;
}else {
dbUtil.connectionRollback();
return 0;
}
}
四、学生作品预览
在uploadStudentProject.jsp文件中添加的语句
<!-- 预览作品超链接 -->
<h1><a href="" id="link">预览作品</a></h1>
var mylink=document.getElementById("link");
mylink.setAttribute("href",".//upload/"+file.name);
五、查看成绩
五、补充:管理员删除学生和教师
allstudent.html中接口:removestudentservlet -> adminDao中的delStudent
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String studentId=request.getParameter("studentId");
Student student = new Student();
student.setStudentId(studentId);
try {
int a = adminDao.delStudent(studentId);
if(a==1) {
response.getWriter().write("success");
}
else {
response.getWriter().write("fail");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public int delStudent(String studentId) throws SQLException {
// TODO Auto-generated method stub
DBUtil dbUtil = new DBUtil();
String sql="delete from s_student where studentId=?";
PreparedStatement ps=(PreparedStatement)dbUtil.getPreparedStatement(sql);
ps.setString(1,studentId);
int rs=ps.executeUpdate();
System.out.println(rs);
if(rs==1) {
dbUtil.commit();
String sql2="delete from studentinfo where studentId=?";
PreparedStatement ps2=(PreparedStatement)dbUtil.getPreparedStatement(sql2);
ps2.setString(1,studentId);
int rs2=ps2.executeUpdate();
if(rs2==1) {
//提交
dbUtil.commit();
return 1;
}else{
//回滚
dbUtil.connectionRollback();
return 0;
}
}else {
dbUtil.connectionRollback();
return 0;
}
}