⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 studentinfodao.java

📁 您的文件包然后写出其具体功能(至少要20个字)。尽量不要让站长把时间都花费在为您
💻 JAVA
字号:
package Sys.dao;

import java.sql.*;
import javax.swing.*;

import java.io.*;
import java.util.*;


import Sys.vo.*;
import Sys.db.*;

public class StudentInfoDao {
	private Connection conDB;//将它命名为private防止其他用户错误使用连接
	public StudentInfoDao(){
		conDB=DatabaseTool.conDB();
	}
	
	public void addStudentInfo(StudentInfo student){
		PreparedStatement ps=null;
		if(student.getOLEPhoto()!=null){
			try{
				ps=conDB.prepareStatement("Insert into StudMes(studentid,studentname,sex,age,family" +
						",birthday,identification,photo,remarks,classid,begintime) values(?,?,?,?,?,?,?,?,?,?,?)");
				ps.setString(1, student.getStudentID());
				ps.setString(2, student.getStudentName());
				ps.setString(3, student.getSex());
				ps.setString(4, student.getAge());
				ps.setString(5, student.getFamily());
				ps.setString(6, student.getBirthday());
				ps.setString(7, student.getIdentification());
				ps.setBinaryStream(8, student.getOLEPhoto(), student.getOLEPhoto().available());
				ps.setString(9, student.getRemarks());
				ps.setString(10, student.getClassID());
				ps.setString(11, student.getBeginTime());//这里我将它变成字符串,方便操作,而不是时间类型!
				ps.executeUpdate();
				JOptionPane.showMessageDialog(null, "恭喜!添加成功");
			}catch(SQLException e){
				e.printStackTrace();
				JOptionPane.showMessageDialog(null, "对不起,已经有该学生ID,请重新输入!");
			}catch(IOException ep){
				ep.printStackTrace();//这条语句用于检测错误,在控制台输出
				JOptionPane.showMessageDialog(null, "对不起,获取图片失败,由于选择路径不对,请重新选择!");
			}finally{
				DatabaseTool.closeStatement(ps);
			}
		}else{
			try{
				ps=conDB.prepareStatement("Insert into StudMes(studentid,studentname,sex,age,family" +
						",birthday,identification,remarks,classid,begintime) values(?,?,?,?,?,?,?,?,?,?)");
				ps.setString(1, student.getStudentID());
				ps.setString(2, student.getStudentName());
				ps.setString(3, student.getSex());
				ps.setString(4, student.getAge());
				ps.setString(5, student.getFamily());
				ps.setString(6, student.getBirthday());
				ps.setString(7, student.getIdentification());
				ps.setString(8, student.getRemarks());
				ps.setString(9, student.getClassID());
				ps.setString(10, student.getBeginTime());//这里我将它变成字符串,方便操作,而不是时间类型!
				ps.executeUpdate();
				JOptionPane.showMessageDialog(null, "恭喜添加成功");
			}catch(SQLException e){
				e.printStackTrace();
				JOptionPane.showMessageDialog(null, "对不起,已经有该学生ID,请重新输入!");
			}finally{
				DatabaseTool.closeStatement(ps);
			}
		}
	}
	
	//修改学生信息与查询学生学号要区分好,是应该在修改类里面根据学号查询出来,然后修改更新数据库
	//查询是否可以可以用姓名来查!这样可能查到多个结果,这里先用ID来查
	//这些地方如果我输入到数据库的学号有相同时,会弹出怎样的错误,我应该怎么捕抓?????这里值得考虑
	//还有就是学号是通过代码自动添加的,所以用不着我去修改!但是代码怎么自动添加?????在每次按添加
	//学生信息时,都会自动生成ID,不可编辑的!或者设另一主键,让它自然增,而学号就用ID生成器来做!
	//有一个值得注意的,就是photo流的打开,什么时候应该关闭?????????
	
	public void modifyStudentInfo(StudentInfo student,String save){
		PreparedStatement ps=null;
		if(student.getOLEPhoto()!=null){
			try{
				ps=conDB.prepareStatement("update StudMes set studentid=?,studentname=?,sex=?" +
						",age=?,family=?,birthday=?,identification=?,photo=?,remarks=?" +
						",classid=?,begintime=? where studentid=?");
				ps.setString(1, student.getStudentID());
				ps.setString(2, student.getStudentName());
				ps.setString(3, student.getSex());
				ps.setString(4, student.getAge());
				ps.setString(5, student.getFamily());
				ps.setString(6, student.getBirthday());
				ps.setString(7, student.getIdentification());
				ps.setBinaryStream(8, student.getOLEPhoto(), student.getOLEPhoto().available());
				ps.setString(9, student.getRemarks());
				ps.setString(10, student.getClassID());
				ps.setString(11, student.getBeginTime());
				ps.setString(12, save);
				ps.executeUpdate();
				JOptionPane.showMessageDialog(null, "修改数据成功");
			}catch(SQLException e){
				e.printStackTrace();
				JOptionPane.showMessageDialog(null, "对不起,已经有该学生ID,请重新输入!");
			}catch(IOException ep){
				ep.printStackTrace();
				JOptionPane.showMessageDialog(null, "对不起,获取图片失败,由于选择路径不对,请重新选择!");
			}finally{
				DatabaseTool.closeStatement(ps);
			}
		}else{
			try{
				ps=conDB.prepareStatement("update StudMes set studentid=?,studentname=?,sex=?" +
						",age=?,family=?,birthday=?,identification=?,remarks=?" +
						",classid=?,begintime=? where studentid=?");
				ps.setString(1, student.getStudentID());
				ps.setString(2, student.getStudentName());
				ps.setString(3, student.getSex());
				ps.setString(4, student.getAge());
				ps.setString(5, student.getFamily());
				ps.setString(6, student.getBirthday());
				ps.setString(7, student.getIdentification());
				ps.setString(8, student.getRemarks());
				ps.setString(9, student.getClassID());
				ps.setString(10, student.getBeginTime());
				ps.setString(11, save);
				ps.executeUpdate();
				JOptionPane.showMessageDialog(null, "修改数据成功");
			}catch(SQLException e){
				e.printStackTrace();
				JOptionPane.showMessageDialog(null, "对不起,已经有该学生ID,请重新输入!");
			}finally{
				DatabaseTool.closeStatement(ps);
			}
		}
	}
	
	public void delStudentInfo(String studentID){
		PreparedStatement ps=null;	
		try{
			StudentInfoDao studentinfodao=new StudentInfoDao();
			StudentInfo studentinfo=studentinfodao.getStudentInfo(studentID);
			if(studentinfo!=null){
				ps=conDB.prepareStatement("delete from StudMes where studentid=?");
				//很多处地方都是因为没有该ID号,都会查询,他不会抛出异常,而是直接执行下面的语句
				//有时会不返回值!而是没有反应!我觉得应该先通过查询来判断
				ps.setString(1, studentID);
				ps.executeUpdate();
				JOptionPane.showMessageDialog(null, "恭喜,删除成功");
			}else{
				JOptionPane.showMessageDialog(null, "删除学生信息失败,可能已经没有该学生ID!");
			}
		}catch(SQLException e){
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, "删除学生信息失败,可能已经没有该学生ID!");
		}finally{
			DatabaseTool.closeStatement(ps);
		}
	}
	
	public StudentInfo getStudentInfo(String studentID){
		byte[] by=new byte[1000*1024];
		PreparedStatement ps=null;
		ResultSet rs=null;
		StudentInfo student=null;
		try{
			ps=conDB.prepareStatement("select * from StudMes where studentid=?");
			ps.setString(1, studentID);
			rs=ps.executeQuery();
			if(rs.next()){
				student=new StudentInfo();
				student.SetStudentID(rs.getString("studentid"));
				student.SetStudentName(rs.getString("studentname"));
				student.SetSex(rs.getString("sex"));
				student.SetAge(rs.getString("age"));
				student.SetFamily(rs.getString("family"));
				student.SetBirthday(rs.getString("birthday"));//接口方法错了,要重新修改tools包!在vo层与数据库的接口应该一样
				student.SetIdentification(rs.getString("identification"));
				ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
			    int ch;
			    InputStream input=rs.getBinaryStream("photo");
			    if(input!=null){
			    	while ((ch = input.read()) != -1)
			    		bytestream.write(ch);
				    by=bytestream.toByteArray();
				    student.SetOLEPhoto(new ByteArrayInputStream(by));
			    }
					//student.SetOLEPhoto(rs.getBinaryStream("photo"));
				student.SetClassID(rs.getString("classid"));
				//这里错了,因为顺序错了,请留意顺序
				/*student.SetRemarks(rs.getString("remarks"));
				student.setBeginTime(rs.getString("begintime"));*/
				student.setBeginTime(rs.getString("begintime"));
				student.SetRemarks(rs.getString("remarks"));
			}
		}catch(SQLException e){
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, "对不起,没有该学生ID,请重新输入!");
		}catch(IOException ex){
			ex.printStackTrace();
			JOptionPane.showMessageDialog(null, "对不起,获得学生相片失败,数据已经损坏!");
		}finally{
			DatabaseTool.closeResultSet(rs);
			DatabaseTool.closeStatement(ps);
		}
		return student;
	}
	
	//以下的操作让我有个问题,虽然将对象的操作都放在同一个类,但是在面板上使用功能的操作都是在不同的地方,如果我实例一个对象
	//会不会比较浪费资源,应该在每个功能上都实例一个对象吗?如果是在网络上的使用应该不能这样做吧!不过反正怎么使用该对象的问题
	//不影响后面操作
	public List<StudentInfo> getClassStudentInfo(String classid){
		PreparedStatement ps=null;
		ResultSet rs=null;
		List<StudentInfo> studentinfolist=null;
		try{
			ps=conDB.prepareStatement("select * from StudMes where classid=?");
			ps.setString(1, classid);
			rs=ps.executeQuery();
			studentinfolist=new ArrayList<StudentInfo>();
			StudentInfo studentinfo=null;
			while(rs.next()){
				studentinfo=new StudentInfo();
				studentinfo.SetStudentID(rs.getString("studentid"));
				studentinfo.SetStudentName(rs.getString("studentname"));
				studentinfo.SetSex(rs.getString("sex"));
				studentinfolist.add(studentinfo);
			}
		}catch(SQLException e){
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, "对不起,获取班级学生失败!");
		}finally{
			DatabaseTool.closeResultSet(rs);
			DatabaseTool.closeStatement(ps);
		}
		return studentinfolist;
	}
	public List<StudentInfo> getAllStudentInfo(){
		PreparedStatement ps=null;
		ResultSet rs=null;
		List<StudentInfo> studentlist=null;
		try{
			ps=conDB.prepareStatement("select *  from StudMes");
			rs=ps.executeQuery();
			studentlist=new ArrayList<StudentInfo>();
			StudentInfo student=null;//这里不用在一开始出起化,因为还不知道有没有资料,如果那样做,效率会比较差
			while(rs.next()){
				student=new StudentInfo();
				student.SetStudentID(rs.getString("studentid"));
				student.SetStudentName(rs.getString("studentname"));
				student.SetSex(rs.getString("sex"));
				student.SetAge(rs.getString("age"));
				student.SetFamily(rs.getString("family"));
				student.SetBirthday(rs.getString("birthday"));
				student.SetIdentification(rs.getString("identification"));
				student.SetOLEPhoto(rs.getBinaryStream("photo"));
				student.SetClassID(rs.getString("classid"));
				student.setBeginTime(rs.getString("begintime"));
				student.SetRemarks(rs.getString("remarks"));
				studentlist.add(student);
			}
		}catch(SQLException e){
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, "对不起,获取学生列表失败!");
		}finally{
			DatabaseTool.closeResultSet(rs);
			DatabaseTool.closeStatement(ps);
		}
		return studentlist;
	}
	
	public boolean getStudentClass(String classid){
		PreparedStatement ps=null;
		ResultSet rs=null;
		try{
			ps=conDB.prepareStatement("select * from StudMes where classid=?");
			ps.setString(1, classid);
			rs=ps.executeQuery();
			if(rs.next()){
				JOptionPane.showMessageDialog(null, "有学生在该班级,不可以删除");
				return false;
			}else{
				JOptionPane.showMessageDialog(null, "没有学生在该班级,可以删除");
				return true;
			}
		}catch(SQLException e){
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, "没有学生在该班级,可以删除");
			return true;
		}finally{
			DatabaseTool.closeResultSet(rs);
			DatabaseTool.closeStatement(ps);
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -