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

📄 studentutil.java

📁 登陆原代码 包含了怎样登陆和相关的查询
💻 JAVA
字号:
package ch6;

import java.sql.*;
import java.util.*;

/**
 * StudentUtil包含和Student表相关的操作
 */
public class StudentUtil {
	private Connection con;

	//构造方法,获得数据库的连接。
	public StudentUtil() {
		this.con = DataBaseConnection.getConnection();
	}

	/**
	 * 搜索所有的学生信息,返回由Student值对象组成的Collection
	 */
	public Collection getAllStudent() throws Exception {
		Statement stmt = con.createStatement();
		ResultSet rst = stmt.executeQuery("select * from student");
		Collection ret = new ArrayList();
		while (rst.next()) {
			Student tempSt = new Student();
			tempSt.setSid(rst.getString("sid"));
			tempSt.setName(rst.getString("name"));
			tempSt.setSex(rst.getInt("sex"));
			tempSt.setPhone(rst.getString("phone"));
			tempSt.setBirth(rst.getString("birth"));

			ret.add(tempSt);
		}
		stmt.close();
		con.close();
		return ret;
	}

	/**
	 * 按照学生的学号查找学生,返回由Student值对象组成的Collection
	 */
	public Collection getStudentBySid(String sid) throws Exception {
		Statement stmt = con.createStatement();
		ResultSet rst = stmt.executeQuery("select * from student where sid='"
				+ sid + "'");
		Collection ret = new ArrayList();
		while (rst.next()) {
			Student tempSt = new Student();
			tempSt.setSid(rst.getString("sid"));
			tempSt.setName(rst.getString("name"));
			tempSt.setSex(rst.getInt("sex"));
			tempSt.setPhone(rst.getString("phone"));
			tempSt.setBirth(rst.getString("birth"));

			ret.add(tempSt);
		}
		stmt.close();
		con.close();
		return ret;
	}

	/**
	 * 添加一个学生,使用Student值对象作为参数传给这个方法。
	 */
	public void addStudent(Student student) throws Exception {
	
		PreparedStatement pstmt = con
				.prepareStatement("insert into student values(?,?,?,?,?)");
		pstmt.setString(1, student.getSid());
		pstmt.setString(2, student.getName());
		pstmt.setInt(3, student.getSex());
		pstmt.setString(4, student.getPhone());
		pstmt.setString(5, student.getBirth());

		pstmt.execute();
		pstmt.close();
		con.close();
	}

	/**
	 * 更改学生的信息,使用Student值对象作为参数传给这个方法。
	 */
	public void modifyStudent(Student student) throws Exception {

		PreparedStatement pstmt = con
				.prepareStatement("update student set name=?, sex=?,phone=?,birth=? where sid=?");
		pstmt.setString(1, student.getName());
		pstmt.setInt(2, student.getSex());
		pstmt.setString(3, student.getPhone());
		pstmt.setString(4, student.getBirth());
		pstmt.setString(5, student.getSid());
		pstmt.execute();
		pstmt.close();
		con.close();
	}

	/**
	 * 删除指定学号的学生
	 */
	public void deleteStudent(String sid) throws Exception {
		Statement stmt = con.createStatement();
		stmt.execute("delete from student where sid='" + sid + "'");
		stmt.close();
		con.close();
	}

	/**
	 * 返回给定学号的学生的信息,返回的是值对象
	 */
	public Student getStudentInfo(String sid) throws Exception {

		Statement stmt = con.createStatement();
		ResultSet rst = stmt.executeQuery("select * from student where sid='"
				+ sid + "'");
		Student student = null;
		while (rst.next()) {
			student = new Student();
			student.setSid(rst.getString("sid"));
			student.setName(rst.getString("name"));
			student.setSex(rst.getInt("sex"));
			student.setPhone(rst.getString("phone"));
			student.setBirth(rst.getString("birth"));
		}
		stmt.close();
		con.close();
		return student;
	}

	public synchronized static java.util.Date getStringToDate(String date,
			String type) throws Exception {
		if (type == null || type.equals("") || type.equals("null")) {
			type = "yyyyMMdd";
		}
		java.text.SimpleDateFormat jts = new java.text.SimpleDateFormat(type);
		java.util.Date fact = jts.parse(date);
		return fact;
	}
}

⌨️ 快捷键说明

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