学生功能:
package com.dao;
import com.orm.Courses;
import com.orm.StuCou;
import com.orm.Students;
import com.util.BaseDAO;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class StudentDao extends BaseDAO {
Connection con = null;
PreparedStatement pst = null;
ResultSet rs = null;
//学生选课
public void chooseCourses(int stuID,int couID) throws Exception {
StuCou sc=new StuCou();
con=BaseDAO.getCon();
String sql="insert into stucourses(stuID,couID) values(?,?)";
try {
pst=con.prepareStatement(sql);
pst.setInt(1, stuID);
pst.setInt(2,couID);
int result=pst.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//查看全部课程
public void SClookCourse() throws Exception {
con=BaseDAO.getCon();
try {
String sql="select * from courses";
pst=con.prepareStatement(sql);
rs=pst.executeQuery();
int index=0;
while(rs.next())
{
Courses cou=new Courses();
int couID = rs.getInt("couID");//获取集合第1行,deviceid 这一列的值
String couName = rs.getString("couName");//获取集合第1行 devicename 列的值
String couTeacher = rs.getString("couTeacher");//获取集合第1行 devicetype 列的值
System.out.println(couID + " " + couName + " " + couTeacher );
}
con.close();
pst.close();
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//查询总成绩以及平均成绩
public void lookGrade() throws Exception{
con=BaseDAO.getCon();
try{
String sql="SELECT stuID,SUM(grade),AVG(grade) FROM grade GROUP BY stuID;";
pst=con.prepareStatement(sql);
rs=pst.executeQuery();
int index=0;
while(rs.next()){
int stuID=rs.getInt("stuID");
int sum=rs.getInt("sum(grade)");
double avg=rs.getInt("avg(grade)");
System.out.println("学号"+" "+ "总成绩"+" " +"平均成绩");
System.out.println(stuID+" "+sum+" "+avg+" ");
}
}catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
教师功能(部分代码):
package com.dao;
import com.orm.Courses;
import com.orm.StuCou;
import com.orm.Students;
import com.util.BaseDAO;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class UserDao extends BaseDAO {
Connection con = null;
PreparedStatement pst = null;
ResultSet rs = null;
//增加学生信息
public int add(Students stu) throws Exception {
//Students stu=new Students();
int result=0;
con=BaseDAO.getCon();
String sql="insert into students(stuID,stuName,stuYear,StuNumber,stuSex) values(?,?,?,?,?)";
try {
pst=con.prepareStatement(sql);
pst.setInt(1, stu.getStuID());
pst.setString(2,stu.getStuName());
pst.setInt(3, stu.getStuYear());
pst.setString(4,stu.getStuNumber());
pst.setString(5,stu.getStuSex());
result=pst.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
//删除学生
public int del(int stuID) throws Exception {
int result=0;
con=BaseDAO.getCon();
String sql="delete from students where stuID=?";
try {
pst=con.prepareStatement(sql);
pst.setInt(1, stuID);
result=pst.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
//修改学生
public int update(Students stu) throws Exception {
int result=0;
boolean flag = false;
try {
con=BaseDAO.getCon();
String sql="update students set stuName=?,stuYear=?,stuNumber=?,stuSex=? where stuID=?";
pst=con.prepareStatement(sql);
pst.setString(1,stu.getStuName());
pst.setInt(2,stu.getStuYear());
pst.setString(3,stu.getStuNumber());
pst.setString(4,stu.getStuSex());
pst.setInt(5,stu.getStuID());
if(pst.executeUpdate() !=0){
flag=true;
}
pst.close();
con.close();
//result=pst.executeUpdate();
//System.out.println(stu.getStuID());
System.out.println("修改完成!");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
//查找全部学生
public List<Students> queryAll() throws Exception {
List<Students> list=new ArrayList<Students>();
con=BaseDAO.getCon();
String sql="select * from students";
try {
pst=con.prepareStatement(sql);
rs=pst.executeQuery();
while(rs.next())
{
Students stu= new Students();
stu.setStuID(rs.getInt(1));
stu.setStuName(rs.getString(2));
stu.setStuYear(rs.getInt(3));
stu.setStuNumber(rs.getString(4));
stu.setStuSex(rs.getString(5));
list.add(stu);
System.out.println(stu.getStuID()+" "+stu.getStuName()+" "+stu.getStuYear()+" "+stu.getStuNumber()+" "+stu.getStuSex());
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
//查找指定学生
public void queryStudentById(int stuID) throws Exception {
Students stu=new Students();
con=BaseDAO.getCon();
String sql="select * from students where stuID=?;";
try {
pst=con.prepareStatement(sql);
pst.setInt(1,stuID);
rs=pst.executeQuery();
if(rs.next())
{
stu.setStuID(rs.getInt("stuID"));
stu.setStuName(rs.getString("stuName"));
stu.setStuYear(rs.getInt("stuYear"));
stu.setStuNumber(rs.getString ("stuNumber"));
stu.setStuSex(rs.getString("stuSex"));
System.out.println(stu.getStuID()+""+stu.getStuName()+""+stu.getStuYear()+""+stu.getStuNumber()+""+stu.getStuSex());
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//return stu;
}