package com.zx.dao;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.Statement;
import com.zx.model.DBCon;
import com.zx.model.SC;
public class SCDao {
private static SCDao instance = null;
public static SCDao getInstance() {
if (instance == null)
instance = new SCDao();
return instance;
}
// 插入数据
public boolean saveSC(SC sc) {
boolean result = false;
Connection con = null;
try {
con = DBCon.getMySQLCon();
String sql = "insert into SC(Sno,Cno,Grade) values(?,?,?)";
PreparedStatement stmt = (PreparedStatement) con
.prepareStatement(sql);
stmt.setString(1, sc.getSno());
stmt.setString(2, sc.getCno());
stmt.setInt(3, sc.getGrade());
int i = stmt.executeUpdate();
if (i == 1) {
// System.out.println("插入数据成功");
result = true;
} else {
// System.out.println("插入数据失败");
result = false;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
// 修改数据
public boolean updateSC(SC sc) {
boolean result = false;
Connection con = null;
try {
con = DBCon.getMySQLCon();
String sql1 = "delete from SC where Sno=? and Cno=?";
String sql2 = "insert into SC(Sno,Cno,Grade) values(?,?,?)";
PreparedStatement stmt1 = (PreparedStatement) con
.prepareStatement(sql1);
PreparedStatement stmt2 = (PreparedStatement) con
.prepareStatement(sql2);
stmt1.setString(1, sc.getSno());
stmt1.setString(2, sc.getCno());
stmt2.setString(1, sc.getSno());
stmt2.setString(2, sc.getCno());
stmt2.setInt(3, sc.getGrade());
stmt1.executeUpdate();
int i = stmt2.executeUpdate();
if (i == 1) {
// System.out.println("插入数据成功");
result = true;
} else {
// System.out.println("插入数据失败");
result = false;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
// 删除数据
public boolean deleteSC(SC sc) {
boolean result = false;
Connection con = null;
try {
con = DBCon.getMySQLCon();
String sql1 = "delete from SC where Sno=? and Cno=?";
PreparedStatement stmt = (PreparedStatement) con
.prepareStatement(sql1);
stmt.setString(1, sc.getSno());
stmt.setString(2, sc.getCno());
// System.out.println(stmt.toString());
int i = stmt.executeUpdate();
if (i == 1) {
// System.out.println("插入数据成功");
result = true;
} else {
// System.out.println("插入数据失败");
result = false;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
// 查询列表
public List<SC> selectSC() {
List<SC> scList = new ArrayList<SC>();
Connection conn = DBCon.getMySQLCon();
try {
Statement stmt = (Statement) conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from SC ");
while (rs.next()) {
SC sc = new SC();
sc.setCno(rs.getString("Cno"));
sc.setSno(rs.getString("Sno"));
sc.setGrade(rs.getInt("Grade"));
scList.add(sc);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return scList;
}
// 通过学号查询数据
public List<SC> selectSCBySno(String Sno) {
List<SC> scList = new ArrayList<SC>();
Connection conn = DBCon.getMySQLCon();
try {
Statement stmt = (Statement) conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from SC where Sno = "
+ Sno);
while (rs.next()) {
SC sc = new SC();
sc.setCno(rs.getString("Cno"));
sc.setSno(rs.getString("Sno"));
sc.setGrade(rs.getInt("Grade"));
scList.add(sc);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return scList;
}
// 通过课程号查询
public List<SC> selectSCByCno(String Cno) {
List<SC> scList = new ArrayList<SC>();
Connection conn = DBCon.getMySQLCon();
try {
Statement stmt = (Statement) conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from SC where Cno="
+ Cno);
while (rs.next()) {
SC sc = new SC();
sc.setCno(rs.getString("Cno"));
sc.setSno(rs.getString("Sno"));
sc.setGrade(rs.getInt("Grade"));
scList.add(sc);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return scList;
}
// 获得个人平均成绩
public int selectAVGGradeBySno(String Sno) {
int max = 0;
Connection conn = DBCon.getMySQLCon();
try {
Statement stmt = (Statement) conn.createStatement();
ResultSet rs = stmt
.executeQuery("select AVG(GRade) from SC where Sno = "
+ Sno);
while (rs.next()) {
max = rs.getInt(1);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return max;
}
// 获得个人总学分
public int selectAllCcreditBySno(String Sno) {
int max = 0;
Connection conn = DBCon.getMySQLCon();
try {
String sql = "select SUM(Ccredit) from SC,Course where Sno=? AND SC.Cno=Course.Cno";
PreparedStatement stmt = (PreparedStatement) conn
.prepareStatement(sql);
stmt.setString(1, Sno);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
max = rs.getInt(1);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return max;
}
// 获得个人最高成绩
public int selectMAXGradeBySno(String Sno) {
int max = 0;
Connection conn = DBCon.getMySQLCon();
try {
Statement stmt = (Statement) conn.createStatement();
ResultSet rs = stmt
.executeQuery("select MAX(Grade) from SC where Sno = "
+ Sno);
while (rs.next()) {
max = rs.getInt(1);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return max;
}
// 获得个人最低成绩
public int selectMINGradeBySno(String Sno) {
int max = 0;
Connection conn = DBCon.getMySQLCon();
try {
Statement stmt = (Statement) conn.createStatement();
ResultSet rs = stmt
.executeQuery("select MIN(Grade) from SC where Sno = "
+ Sno);
while (rs.next()) {
max = rs.getInt(1);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return max;
}
// 获得课程平均成绩
public int selectAVGGradeByCno(String Cno) {
int max = 0;
Connection conn = DBCon.getMySQLCon();
try {
Statement stmt = (Statement) conn.createStatement();
ResultSet rs = stmt
.executeQuery("select AVG(GRade) from SC where Cno = "
+ Cno);
while (rs.next()) {
max = rs.getInt(1);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return max;
}
// 获得个人总人数
public int selectAllCcreditByCno(String Cno) {
int max = 0;
Connection conn = DBCon.getMySQLCon();
try {
String sql = "select Count(Sno) from SC where Cno=?";
PreparedStatement stmt = (Pre
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
sql_homework_servlet_JDBC.zip (98个子文件)
sql_homework_servlet_JDBC
WebContent
updateStudent.jsp 2KB
course.jsp 2KB
addSC.jsp 1KB
addCourse.jsp 2KB
META-INF
MANIFEST.MF 39B
WEB-INF
sss.sql 0B
web.xml 7KB
lib
mysql-connector-java-5.1.21-bin.jar 809KB
c.tld 16KB
deleteCourse.jsp 2KB
stuList.jsp 2KB
scList.jsp 1KB
SQL.sql 2KB
student.jsp 2KB
courseList.jsp 2KB
student
index.jsp 833B
erro.jsp 443B
addStudent.jsp 2KB
deleteStudent.jsp 2KB
updateCourse.jsp 2KB
updateSC.jsp 1KB
sc.jsp 3KB
deleteSC.jsp 2KB
success.jsp 449B
.settings
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.common.project.facet.core.xml 345B
org.eclipse.wst.common.component 527B
org.eclipse.wst.jsdt.ui.superType.container 49B
.jsdtscope 503B
org.eclipse.jdt.core.prefs 364B
src
com
zx
dao
StudentDao.java 6KB
CourseDao.java 7KB
CreateTableDao.java 2KB
SCDao.java 9KB
model
Course.java 549B
Student.java 675B
DBCon.java 1KB
SC.java 394B
test
TestStudent.java 1KB
TestCourse.java 1KB
TestSC.java 1KB
servlet
SelectStudent.java 1KB
UpdateStudent.java 1KB
UpdateSC.java 1KB
SelectSC.java 1KB
SelectStudentBySname.java 1KB
SelectCourseByCname.java 1KB
DeleteCourse.java 1KB
SelectGradeByCno.java 2KB
CreateTable.java 1KB
SelectCourse.java 1KB
AddCourse.java 1KB
AddSC.java 1KB
SelectGradeBySno.java 2KB
UpdateCourse.java 1KB
SelectSCBySno.java 946B
AddStudent.java 1KB
DeleteSC.java 1KB
SelectStudentBySno.java 1KB
DeleteStudent.java 1KB
CharactorFilter.java 1KB
SelectCourseByCno.java 1022B
SelectSCByCno.java 946B
.project 1KB
.classpath 834B
build
classes
com
zx
dao
CourseDao.class 7KB
StudentDao.class 6KB
SCDao.class 11KB
CreateTableDao.class 3KB
model
Student.class 1KB
Course.class 1KB
DBCon.class 2KB
SC.class 905B
test
TestSC.class 2KB
TestCourse.class 2KB
TestStudent.class 2KB
servlet
SelectSC.class 2KB
CharactorFilter.class 2KB
UpdateStudent.class 2KB
DeleteCourse.class 2KB
SelectStudentBySno.class 2KB
SelectCourseByCname.class 2KB
UpdateCourse.class 2KB
DeleteSC.class 2KB
SelectCourseByCno.class 2KB
CreateTable.class 2KB
SelectSCByCno.class 2KB
SelectGradeByCno.class 3KB
DeleteStudent.class 2KB
AddSC.class 2KB
SelectSCBySno.class 2KB
AddCourse.class 2KB
AddStudent.class 2KB
UpdateSC.class 2KB
SelectStudentBySname.class 2KB
SelectStudent.class 2KB
SelectCourse.class 2KB
SelectGradeBySno.class 3KB
共 98 条
- 1
资源评论
JaniceLu
- 粉丝: 78
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功