package xuesheng.com.cn;
import java.util.*;
import java.sql.*;
import javax.swing.JOptionPane;
public class StudentDao {
public ArrayList<Student> getStudents(String studNo) {
Connection con=null; //定义连接对象
PreparedStatement pstmt=null; //定义预处理对象
ResultSet rs=null; //定义结果集对象
List<Student> list=new ArrayList<Student>();//定义列表对象
try{
con=ConnectDB.connect(); //获得连接对象
String sql="select *from students where studNo like ?";
pstmt=con.prepareStatement(sql);
pstmt.setString(1, "%"+studNo+"%");
rs=pstmt.executeQuery(); //执行查询
Student student=null;
while(rs.next()){
student=new Student();
student.setStudNo(rs.getString("studNo"));
student.setStudName(rs.getString("studName"));
student.setStudSex(rs.getString("studSex"));
student.setStudBirthDate(rs.getDate("studBirthDate"));
student.setStudIsMember(rs.getBoolean("studIsMember"));
student.setStudAddress(rs.getString("studAddress"));
list.add(student); //将学生对象存放在列表中
}
}catch (SQLException e){
e.printStackTrace();
}finally{
try{
rs.close();
}catch (SQLException ex){
}
try{
pstmt.close();
}catch (SQLException ex){
}
try{
con.close();
}catch(SQLException ex){
}
}
return (ArrayList<Student>) list;
}
public Student getStudent(String studNo){
Connection con=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
Student student =null;
try{
con=ConnectDB.connect();
pstmt=con.prepareStatement("select * from students where studNo=?");
pstmt.setString(1, studNo);
rs=pstmt.executeQuery();
if(rs.next()){
student=new Student();
student.setStudNo(rs.getString("studNo"));
student.setStudName(rs.getString("studName"));
student.setStudSex(rs.getString("studSex"));
student.setStudBirthDate(rs.getDate("studBirthDate"));
student.setStudIsMember(rs.getBoolean("studIsMember"));
student.setStudAddress(rs.getString("studAddress"));
}
}catch(SQLException e){
e.printStackTrace();
}finally{
try{
pstmt.close();
}catch(SQLException ex){
}
try{
rs.close();
}catch(SQLException ex){
ex.printStackTrace();
}
try{
con.close();
}catch(SQLException ex){
}
}
return student;
}
public Student getStudent2(String studName){
Connection con=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
Student student =null;
try{
con=ConnectDB.connect();
pstmt=con.prepareStatement("select * from students where studName=?");
pstmt.setString(1, studName);
rs=pstmt.executeQuery();
if(rs.next()){
student=new Student();
student.setStudNo(rs.getString("studNo"));
student.setStudName(rs.getString("studName"));
student.setStudSex(rs.getString("studSex"));
student.setStudBirthDate(rs.getDate("studBirthDate"));
student.setStudIsMember(rs.getBoolean("studIsMember"));
student.setStudAddress(rs.getString("studAddress"));
}
}catch(SQLException e){
e.printStackTrace();
}finally{
try{
pstmt.close();
}catch(SQLException ex){
}
try{
rs.close();
}catch(SQLException ex){
ex.printStackTrace();
}
try{
con.close();
}catch(SQLException ex){
}
}
return student;
}
public boolean editStudent(Student student){
Connection con=null;//定义连接对象
PreparedStatement pstmt=null;//定义预处理对象
try{
con=ConnectDB.connect();;
String sql="update Students set studName=?,studSex=?,studBirthDate=?,studIsMember=?,studAddress=? where studNo=?";
pstmt=con.prepareStatement(sql);
pstmt.setString(6,student.getStudNo());
pstmt.setString(1, student.getStudName());
pstmt.setString(2,student.getStudSex());
pstmt.setDate(3,student.getStudBirthDate());
pstmt.setBoolean(4,student.isStudIsMember());
pstmt.setString(5,student.getStudAddress());
int n=pstmt.executeUpdate();
return n>0;
}catch(SQLException e){
e.printStackTrace();
return false;
}finally{
try{
pstmt.close();
}catch(SQLException ex){
}
try{
con.close();
}catch(SQLException ex){
}
}
}
public int getNextStudNo(){
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
int maxNo=0;
try{
con=ConnectDB.connect();
String sql="select MAX(studNo) studNo from students";
stmt=con.createStatement();
rs=stmt.executeQuery(sql);
if(rs.next()){
maxNo=Integer.parseInt(rs.getString("studNo"));
}
else
{
maxNo=0;
JOptionPane.showMessageDialog(null, "检索学生学号时出错!");
}
}catch(SQLException e){
e.printStackTrace();
}
return maxNo+1;
}
public boolean addStudent(Student student){
Connection con=null;//定义连接对象
PreparedStatement pstmt=null;//定义预处理对象
try{
con = ConnectDB.connect();
String sql="Insert into Students(studNo,studName,studSex,studBirthDate,studIsMember,studAddress) values (?,?,?,?,?,? )";
pstmt=con.prepareStatement(sql);
pstmt.setString(1,student.getStudNo());
pstmt.setString(2, student.getStudName());
pstmt.setString(3,student.getStudSex());
pstmt.setDate(4,student.getStudBirthDate());
pstmt.setBoolean(5,student.isStudIsMember());
pstmt.setString(6,student.getStudAddress());
int n=pstmt.executeUpdate();
return n>0;
}catch(SQLException e){
e.printStackTrace();
return false;
}finally{
try{
pstmt.close();
}catch(SQLException ex){
}
try{
con.close();
}catch(SQLException ex){
}
}
}
}
评论0