package cn.com.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import cn.com.common.AllSql;
import cn.com.common.ConnHelp;
import cn.com.dao.IStudentDao;
import cn.com.vo.JiaoyiVO;
import cn.com.vo.StuLoginVO;
import cn.com.vo.StudentVO;
public class StudentDao implements IStudentDao {
Connection conn = null; // 获取连接
PreparedStatement ps = null;
ResultSet rs = null;
public int changeMoney(int id, float money) {
// TODO Auto-generated method stub
String sql = AllSql.updateStuMon;
conn = ConnHelp.getConnection();
try {
ps = conn.prepareStatement(sql);
ps.setFloat(1, money);
ps.setInt(2, id);
ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return 0;
}finally{
try {
conn.close();
rs.close();
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return 1;
}
// 插入基本信息
public int insertBasicMes(StudentVO vo) {
// TODO Auto-generated method stub
String sql = "insert into student values (?,?,?,?,?,?,?,?)";
//AllSql.insertStu;
conn = ConnHelp.getConnection();
try {
ps = conn.prepareStatement(sql);
ps.setInt(1, vo.getId());
ps.setString(2, vo.getRealname());
ps.setString(3, vo.getSex());
ps.setInt(4, vo.getAge());
ps.setString(5, vo.getGrade());
ps.setString(6, vo.getCla());
ps.setString(7, vo.getDepartment());
ps.setFloat(8, vo.getMoney());
ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return 0;
}finally{
try {
conn.close();
rs.close();
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return 1;
}
// 插入账号信息
public int insertAccount(StuLoginVO vo) {
// TODO Auto-generated method stub
String sql = AllSql.insertAcc;
conn = ConnHelp.getConnection();
try {
ps = conn.prepareStatement(sql);
ps.setInt(1, vo.getId());
ps.setString(2, vo.getStuPass());
ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return 0;
}finally{
try {
conn.close();
rs.close();
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return 1;
}
public List selectAll(int page) {
// TODO Auto-generated method stub
String sql = AllSql.selectAllStu;
conn = ConnHelp.getConnection();
List list = new ArrayList();
try {
ps = conn.prepareStatement(sql);
ps.setInt(1, page * 5);
ps.setInt(2, (page - 1) * 5 + 1);
rs = ps.executeQuery();
while (rs.next()) {
StudentVO vo = new StudentVO();
vo.setId(rs.getInt(2)); // rs的第一个值是分页信息rownum,所以从第二个值开始获取
vo.setRealname(rs.getString(3));
vo.setSex(rs.getString(4));
vo.setAge(rs.getInt(5));
vo.setGrade(rs.getString(6));
vo.setCla(rs.getString(7));
vo.setDepartment(rs.getString(8));
vo.setMoney(rs.getFloat(9));
list.add(vo);
// System.out.println(vo.getSex()+" ");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
conn.close();
rs.close();
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return list;
}
// 查询个人信息
public List selectOne(int id) {
// TODO Auto-generated method stub
String sql = AllSql.selectOneStu;
List list = new ArrayList();
conn = ConnHelp.getConnection();
try {
ps = conn.prepareStatement(sql);
ps.setInt(1, id);
rs = ps.executeQuery();
while (rs.next()) {
StudentVO vo = new StudentVO();
vo.setId(rs.getInt(1));
vo.setRealname(rs.getString(2));
vo.setSex(rs.getString(3));
vo.setAge(rs.getInt(4));
vo.setGrade(rs.getString(5));
vo.setCla(rs.getString(6));
vo.setDepartment(rs.getString(7));
vo.setMoney(rs.getFloat(8));
list.add(vo);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
conn.close();
rs.close();
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return list;
}
// 更新基本信息
public int update(StudentVO vo) {
// TODO Auto-generated method stub
String sql = AllSql.updateStu;
conn = ConnHelp.getConnection();
try {
ps = conn.prepareStatement(sql);
ps.setString(1, vo.getRealname());
ps.setString(2, vo.getSex());
ps.setInt(3, vo.getAge());
ps.setString(4, vo.getGrade());
ps.setString(5, vo.getCla());
ps.setString(6, vo.getDepartment());
ps.setFloat(7, vo.getMoney());
ps.setInt(8, vo.getId());
ps.execute();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return 0;
}finally{
try {
conn.close();
rs.close();
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return 1;
}
// 更新密码
public int changePassword(int id, String pass) {
// TODO Auto-generated method stub
conn = ConnHelp.getConnection();
String sql = AllSql.changePassword;
try {
ps = conn.prepareStatement(sql);
ps.setString(1, pass);
ps.setInt(2, id);
ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return 0;
}finally{
try {
conn.close();
rs.close();
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return 1;
}
// 不知如何同时删除两个表的内容 一个ACTION调用两个DAO写两个DAO计科
public int deleteStu(int id) {
// TODO Auto-generated method stub
String sql = AllSql.deleteStu;
conn = ConnHelp.getConnection();
try {
ps = conn.prepareStatement(sql);
ps.setInt(1, id);
ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return 0;
}finally{
try {
conn.close();
rs.close();
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return 1;
}
// 不知如何同时删除两个表的内容 一个ACTION调用两个DAO写两个DAO计科
public int deleteStuLogin(int id) {
// TODO Auto-generated method stub
String sql = AllSql.deleteStuLogin;
conn = ConnHelp.getConnection();
try {
ps = conn.prepareStatement(sql);
ps.setInt(1, id);
ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return 0;
}finally{
try {
conn.close();
rs.close();
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return 1;
}
public int getStuCount() {
// TODO Auto-generated method stub
String sql = AllSql.getStuCount;
int count = 0;
conn = ConnHelp.getConnection();
try {
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
count = rs.getInt(1);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return 0;
}finally{
try {
conn.close();
rs.close();
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
- 1
- 2
前往页