package com.impl;
import java.util.ArrayList;
import java.util.List;
import com.dao.StudentDao;
import com.pojo.Student;
import java.sql.*;
import com.db.DBConnection;
public class StudentDaoImpl implements StudentDao {
public boolean deleteStudent(int sid) {
// TODO Auto-generated method stub
Connection con=null;
PreparedStatement pst=null;
boolean b=false;
try {
con=DBConnection.getConnection();
pst=con.prepareStatement("delete student where sid=?");
pst.setInt(1,sid);
int i=pst.executeUpdate();
if(i>0){
b=true;
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
finally{
DBConnection.closePreparedStatement(pst);
DBConnection.closeConnection(con);
}
return b;
}
public boolean insertStudent(Student s) {
// TODO Auto-generated method stub
Connection con=null;
PreparedStatement pst=null;
boolean b=false;
try {
con=DBConnection.getConnection();
pst=con.prepareStatement("insert into student values(?,?,?,?)");
pst.setString(1,s.getName());
pst.setString(2,s.getSex());
pst.setInt(3,s.getAge());
pst.setInt(4,s.getCid());
int i=pst.executeUpdate();
if(i>0){
b=true;
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
finally{
DBConnection.closePreparedStatement(pst);
DBConnection.closeConnection(con);
}
return b;
}
public List<Student> selectAllStudent() {
// TODO Auto-generated method stub
Connection con=null;
PreparedStatement pst=null;
ResultSet rs=null;
List<Student> list=new ArrayList<Student>();
try {
con=DBConnection.getConnection();
pst=con.prepareStatement("select * from student");
rs=pst.executeQuery();
while(rs.next()){
Student s=new Student();
s.setSid(rs.getInt("sid"));
s.setName(rs.getString("sname"));
s.setAge(rs.getInt("age"));
s.setSex(rs.getString("sex"));
s.setCid(rs.getInt("cid"));
list.add(s);
}
} catch (Exception e) {
// TODO: handle exception
}
finally{
DBConnection.closeResultSet(rs);
DBConnection.closePreparedStatement(pst);
DBConnection.closeConnection(con);
}
return list;
}
public boolean updateStudent(Student s) {
// TODO Auto-generated method stub
return false;
}
}
评论16
最新资源