package dao;
import java.sql.*;
import java.util.*;
import entity.Student;
import base.ConnectionManager;
//提供对StuInfo表的操作
/**
* 1.添加
* 2.删除
* 3.修改
* 4.查询
*/
public class StudentDAO
{
Connection conn=null;
PreparedStatement pstm=null;
ResultSet rs=null;
//添加学员信息
public int addStudent(Student objStu)
{
int rowCount=-1;//执行添加,所影响的行数
try
{
conn=ConnectionManager.getConnection();
String sql="insert into stuinfo(stuNo,stuName,stuSex,stuAge,stuLikes,stuAddress)values(?,?,?,?,?,?)";
pstm=conn.prepareStatement(sql);
pstm.setString(1, objStu.getStuNo());
pstm.setString(2,objStu.getStuName());
pstm.setString(3,objStu.getStuSex());
pstm.setInt(4, objStu.getStuAge());
pstm.setString(5, objStu.getStuLikes());
pstm.setString(6, objStu.getStuAddress());
rowCount=pstm.executeUpdate();//执行添加,接受影响的行数,并返回
}
catch(Exception ee)
{
ee.printStackTrace();
}
return rowCount;
}
//查询某个学生的信息
public Student findStu(String no){
Student stu=new Student();
String str="select * from stuinfo where stuNo=?";
try{
conn=ConnectionManager.getConnection();
pstm=conn.prepareStatement(str);
pstm.setString(1, no);
rs=pstm.executeQuery();
while(rs.next()){
stu.setStuNo(rs.getString(2));
stu.setStuName(rs.getString(3));
stu.setStuSex(rs.getString(4));
stu.setStuAge(rs.getInt(5));
stu.setStuLikes(rs.getString(6));
stu.setStuAddress(rs.getString(7));
}
}catch(Exception e){
e.printStackTrace();
}finally{
ConnectionManager.closeResultSet(rs);
ConnectionManager.closeStatement(pstm);
ConnectionManager.closeConnection(conn);
}
return stu;
}
public int seacherStudent()
{
int rowCount=-1;//执行添加,所影响的行数
try
{
conn=ConnectionManager.getConnection();
String sql="select * from stuinfo";
pstm=conn.prepareStatement(sql);
rs=pstm.executeQuery();//执行添加,接受影响的行数,并返回
}
catch(Exception ee)
{
ee.printStackTrace();
}
return rowCount;
}
// 查询出数据库中所有的学生的信息
public ArrayList selectStu(){
ArrayList stuList=new ArrayList();
String str="select * from stuinfo ";
try{
conn=ConnectionManager.getConnection();
pstm=conn.prepareStatement(str);
rs=pstm.executeQuery();
while(rs.next()){
Student stu=new Student();
stu.setStuNo(rs.getString(2));
stu.setStuName(rs.getString(3));
stu.setStuSex(rs.getString(4));
stu.setStuAge(rs.getInt(5));
stu.setStuLikes(rs.getString(6));
stu.setStuAddress(rs.getString(7));
stuList.add(stu);
}
}catch(Exception e){
e.printStackTrace();
}finally{
ConnectionManager.closeResultSet(rs);
ConnectionManager.closeStatement(pstm);
ConnectionManager.closeConnection(conn);
}
return stuList;
}
//删除某个学生的信息
public void deleteStu(String no){
String str="delete from stuinfo where stuNo=?";
try{
conn=ConnectionManager.getConnection();
pstm=conn.prepareStatement(str);
pstm.setString(1, no);
pstm.executeUpdate();
}catch(Exception e){
e.printStackTrace();
}finally{
ConnectionManager.closeStatement(pstm);
ConnectionManager.closeConnection(conn);
}
}
//修改学员信息
public void modifyStu(Student objStu){
try
{
conn=ConnectionManager.getConnection();
String sql="update stuinfo set stuName=?,stuSex=?,stuAge=?,stuLikes=?,stuAddress=? where stuNo=?";
pstm=conn.prepareStatement(sql);
pstm.setString(1,objStu.getStuName());
pstm.setString(2,objStu.getStuSex());
pstm.setInt(3, objStu.getStuAge());
pstm.setString(4, objStu.getStuLikes());
pstm.setString(5, objStu.getStuAddress());
pstm.setString(6, objStu.getStuNo());
pstm.executeUpdate();
}
catch(Exception ee)
{
ee.printStackTrace();
}
}
}
评论0