package com.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import com.entity.*;
import com.util.DBUtil;
public class StudentDao {
public boolean isValid(Student student) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = DBUtil.getConnection();
String sql = "select * from student where sid=? and spwd=?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, student.getSid());
pstmt.setString(2, student.getSpwd());
rs = pstmt.executeQuery();
if (rs != null && rs.next()) {
student.setSname(rs.getString("sname"));
return true;
} else {
return false;
}
} catch (SQLException ex) {
ex.printStackTrace();
}
return false;
}
public ArrayList<OptCourse> getSelectedCourse(Student student) {
ArrayList<OptCourse> list = new ArrayList<OptCourse>();
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = DBUtil.getConnection();
String sql = "select course.cid, cname, credit, score from student, course, optcou where student.sid = optcou.sid and course.cid = optcou.cid and student.sid=?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, student.getSid());
rs = pstmt.executeQuery();
while (rs != null && rs.next()) {
OptCourse oc = new OptCourse();
oc.setCid(rs.getInt(1));
oc.setCname(rs.getString("cname"));
oc.setCredit(rs.getInt("credit"));
oc.setScore(rs.getDouble("score"));
list.add(oc);
}
} catch (SQLException ex) {
ex.printStackTrace();
}
return list;
}
public ArrayList<Student> getAllStudent() {
ArrayList<Student> list = new ArrayList<>();
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = DBUtil.getConnection();
String sql = "select * from student";
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
while (rs != null && rs.next()) {
Student student = new Student();
student.setSid(rs.getInt(1));
student.setSpwd(rs.getString(2));
student.setSname(rs.getString(3));
list.add(student);
}
} catch (SQLException ex) {
ex.printStackTrace();
}
return list;
}
public boolean deleteCourse(Student student, int cid) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = DBUtil.getConnection();
String sql = "select score from optcou where sid=? and cid=? and score > 0";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, student.getSid());
pstmt.setInt(2, cid);
rs = pstmt.executeQuery();
if (rs != null && rs.next()) {
// 存在成绩 不可以删除
return false;
} else {
// 成绩不存在,可以删除
sql = "delete from optcou where sid=? and cid=?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, student.getSid());
pstmt.setInt(2, cid);
pstmt.executeUpdate();
return true;
}
} catch (SQLException ex) {
ex.printStackTrace();
}
return false;
}
public boolean addCourse(Student student, int cid) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = DBUtil.getConnection();
String sql = "select * from optcou where sid=? and cid=?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, student.getSid());
pstmt.setInt(2, cid);
rs = pstmt.executeQuery();
if (rs != null && rs.next()) {
// 该同学已经选过该门课 不能重复选择
return false;
} else {
// 没选过 可以选择
sql = "insert into optcou(sid, cid) values(?, ?)";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, student.getSid());
pstmt.setInt(2, cid);
pstmt.executeUpdate();
return true;
}
} catch (SQLException ex) {
ex.printStackTrace();
}
return false;
}
public void addStudent(Student student) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = DBUtil.getConnection();
String sql = "insert into student(sname, spwd) values(?, ?)";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, student.getSname());
pstmt.setString(2, student.getSpwd());
pstmt.executeUpdate();
} catch (SQLException ex) {
ex.printStackTrace();
} finally {
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
StudentDao dao = new StudentDao();
Student student = new Student();
student.setSid(100000000);
student.setSpwd("1");
dao.isValid(student);
System.out.println(student.getSname());
//
// ArrayList<OptCourse> list = dao.getSelectedCourse(student);
// for (OptCourse oc: list) {
// System.out.println(oc.getCname() + "," + oc.getCredit() + "," + oc.getScore());
// }
dao.deleteCourse(student, 100000003);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
学生成绩管理系统(MySQL+JSP)实例源码下载
共54个文件
jsp:19个
java:8个
class:8个
4星 · 超过85%的资源 需积分: 50 504 下载量 162 浏览量
2017-01-19
22:26:24
上传
评论 10
收藏 2.34MB ZIP 举报
温馨提示
简单的学生成绩管理系统(MySQL+JSP) http://www.cnblogs.com/wenruo/p/6351787.html
资源推荐
资源详情
资源评论
收起资源包目录
StudentManagement.zip (54个子文件)
StudentManagement
.project 1KB
src
com
controller
QueryServlet.java 1KB
dao
StudentDao.java 5KB
AdminDao.java 204B
CourseDao.java 4KB
util
DBUtil.java 775B
entity
Course.java 453B
Student.java 449B
OptCourse.java 640B
WebRoot
sql
scoreInfo.sql 1KB
studentSelectResult.jsp 900B
adminSearchCourse.jsp 3KB
WEB-INF
classes
com
controller
QueryServlet.class 2KB
dao
AdminDao.class 565B
StudentDao.class 5KB
CourseDao.class 5KB
util
DBUtil.class 1KB
entity
Course.class 902B
Student.class 899B
OptCourse.class 1KB
lib
mysql-connector-java-5.1.7-bin.jar 693KB
web.xml 619B
adminAddCourse.jsp 2KB
doAddCourse.jsp 2KB
index.jsp 3KB
images
addCourse.jpg 163KB
updateCourse.jpg 7KB
studentSelected.jpg 121KB
searchCourse.jpg 188KB
studentMain.jpg 847KB
login.jpg 390KB
studentSelectCourse.jsp 872B
adminDeleteCourse.jsp 1KB
studentDelete.jsp 1KB
adminUpdateCourse.jsp 2KB
failure.jsp 732B
dologin.jsp 3KB
adminAddStudent.jsp 2KB
META-INF
MANIFEST.MF 39B
doAddStudent.jsp 1KB
adminSearchStudent.jsp 2KB
studentSearchCourse.jsp 3KB
studentSelected.jsp 2KB
studentMain.jsp 1KB
adminMain.jsp 1KB
doUpdateCourse.jsp 1KB
.settings
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.common.project.facet.core.xml 398B
org.eclipse.jdt.core.prefs 364B
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.core.resources.prefs 74B
org.eclipse.wst.common.component 510B
.jsdtscope 564B
.classpath 1KB
共 54 条
- 1
文_弱
- 粉丝: 9
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页