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

📄 stuinfodao.java

📁 用户通过注册后进行登录
💻 JAVA
字号:
package com.svse.dao;

import com.svse.entity.*;
import com.svse.util.DBConnect;

import java.util.ArrayList;
import java.util.List;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class StuInfoDao {
	private Connection conn;
	private PreparedStatement pstmt;
	private ResultSet rs;

	/**
	 * 得到所有学生信息
	 * 
	 * @return学生信息集合
	 */
	public List<StuInfo> getStuInfo() {
		List<StuInfo> list = new ArrayList<StuInfo>();
		String sql = "select * from stuInfo";
		// 获取连接
		conn = DBConnect.getConnect();
		try {
			// 创建SQL语句执行对象
			pstmt = conn.prepareStatement(sql);
			// 执行SQL语句得到的结果集
			rs = pstmt.executeQuery();
			// 遍历结果集,封装对象
			while (rs.next()) {
				StuInfo stu = new StuInfo();
				stu.setStuName(rs.getString("stuName"));
				stu.setStuNo(rs.getString("stuNo"));
				stu.setStuSex(rs.getString("stuSex"));
				stu.setStuAge(rs.getInt("stuAge"));
				stu.setStuSeat(rs.getInt("stuSeat"));
				stu.setStuAddress(rs.getString("stuAddress"));
				// 将封装后的学生信息添加到集合里
				list.add(stu);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBConnect.closeConnect(conn);
		}
		return list;
	}

	/**
	 * 添加新学员
	 * 
	 * @param stuinfo学生信息实体
	 * @return对数据库影响的行数
	 */
	public int addStudent(StuInfo stuinfo) {
		int result = 0;
		String sql = "INSERT INTO stuInfo VALUES(?,?,?,?,?)";
		conn = DBConnect.getConnect();
		try {
			pstmt = conn.prepareStatement(sql);
			// 解释SQL语句中的?设置参数
			pstmt.setString(1, stuinfo.getStuName());
			pstmt.setString(2, stuinfo.getStuNo());
			pstmt.setString(3, stuinfo.getStuSex());
			pstmt.setInt(4, stuinfo.getStuAge());
			pstmt.setString(5, stuinfo.getStuAddress());
			// 更新数据库,返回当前执行SQL语句对数据库影响的行数
			result = pstmt.executeUpdate();
		} catch (SQLException e) {
			System.out.println("插入失败!\n消息:" + e.getMessage());
		} finally {
			DBConnect.closeConnect(conn);
		}
		return result;
	}

	/**
	 * 删除一条学生信息
	 * 
	 * @param stuNo学号
	 * @return对数据库影响的行数
	 */
	public int deleteStudent(String stuNo) {
		int result = 0;
		String sql = "delete from stuInfo where stuNo=?";
		conn = DBConnect.getConnect();
		try {
			pstmt = conn.prepareStatement(sql);
			// 解释SQL语句中的?设置参数
			pstmt.setString(1, stuNo);
			// 更新数据库,返回当前执行SQL语句对数据库影响的行数
			result = pstmt.executeUpdate();
		} catch (SQLException e) {
			System.out.println("删除失败!\n消息:" + e.getMessage());
		} finally {
			DBConnect.closeConnect(conn);
		}
		return result;
	}

	/**
	 * 修改学生信息
	 * 
	 * @param stuinfo学生实体信息
	 * @return对数据库影响的行数
	 */
	public int update(StuInfo stuinfo) {
		int result = 0;
		String sql = "update stuInfo set stuName=?,stuSex=?,stuAge=?,stuAddress=? where stuNo=?";
		conn = DBConnect.getConnect();
		try {
			pstmt = conn.prepareStatement(sql);
			// 解释SQL语句中的?设置参数
			pstmt.setString(1, stuinfo.getStuName());
			pstmt.setString(2, stuinfo.getStuSex());
			pstmt.setInt(3, stuinfo.getStuAge());
			pstmt.setString(4, stuinfo.getStuAddress());
			pstmt.setString(5, stuinfo.getStuNo());
			// 更新数据库,返回当前执行SQL语句对数据库影响的行数
			result = pstmt.executeUpdate();
		} catch (SQLException e) {
			System.out.println("修改失败!\n消息:" + e.getMessage());
		} finally {
			DBConnect.closeConnect(conn);
		}
		return result;
	}
	
	/**
	 * 通过学号查询学生信息
	 * @param stuNo
	 * @return
	 */
	public boolean isExist(String stuNo){
		boolean bool=false;
		String sql="select * from stuInfo where stuNo=?";
		conn=DBConnect.getConnect();
		try {
			pstmt=conn.prepareStatement(sql);
			pstmt.setString(1, stuNo);
			rs=pstmt.executeQuery();
			if(rs.next()){
				bool=true;
			}
		} catch (SQLException e) {
			System.out.println("查询失败!\n消息:"+e.getMessage());
		}finally{
			DBConnect.closeConnect(conn);
		}
		return bool;
	}
}

⌨️ 快捷键说明

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