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

📄 dbteacherimpl.java

📁 我们采用了JSP技术为主要手段,本系统采用了多级角色管理:包括系统管理员、系主任
💻 JAVA
字号:
package com.middle.graduate.biz.dao.impl;

import java.util.Set;

import org.hibernate.HibernateException;
import org.hibernate.Session;

import com.middle.graduate.biz.dao.DBTeacherDao;
import com.middle.graduate.biz.entity.Teacher;
import com.middle.graduate.biz.entity.Topic;
import com.middle.graduate.biz.exce.DataException;
import com.middle.graduate.util.HbnUtil;

public class DBTeacherImpl implements DBTeacherDao {

	public synchronized void changePassword(int teacherId, String password) throws DataException {
		Session session = HbnUtil.getSession();
		try {
			session.beginTransaction();
			String sql = "update t_teacher set t_password = ? where t_teacherId = ?";
			session.createSQLQuery(sql).setString(0, password).setInteger(1, teacherId).executeUpdate();
			session.getTransaction().commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
			throw new DataException("db: teacher change password error");
		} finally {
			HbnUtil.closeSession();
		}
	}

	public boolean login(int teacherId, String password) throws DataException {
		Session session = HbnUtil.getSession();
		try {
			session.beginTransaction();
			String sql = "select t_password from t_teacher where t_teacherId = ?";
			String pwd = (String)session.createSQLQuery(sql).setInteger(0, teacherId).uniqueResult();
			if(pwd.equals(password)) {
				return true;
			} else {
				return false;
			}
		} catch (HibernateException e) {
			e.printStackTrace();
			throw new DataException("teacher login error");
		}
	}

	public synchronized void insert(int teacherId, Topic topic) throws DataException {
		insertTopic(topic);
		int topicId = topic.getTopicId();
		update_fk(teacherId, topicId);
	}

	public void insertTopic(Topic topic) throws DataException {
		Session session = HbnUtil.getSession();
		try {
			session.beginTransaction();
			session.save(topic);
			session.getTransaction().commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
			throw new DataException("db: teacher inset topic into db error");
		} finally {
			HbnUtil.closeSession();
		}
	}

	public void update_fk(int teacherId, int topicId) throws DataException {
		Session session = HbnUtil.getSession();
		try {
			session.beginTransaction();
			String sql = "update t_topic set f_teacher = ? where t_topicId = ?";
			session.createSQLQuery(sql).setInteger(0, teacherId).setInteger(1, topicId).executeUpdate();
			session.getTransaction().commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
			throw new DataException("建立topic与teacher之间的关系失败");
		} finally {
			HbnUtil.closeSession();
		}
	}

	public Set<Topic> queryTopic(int teacherId) throws DataException {
		Session session = HbnUtil.getSession();
		try {
			session.beginTransaction();
			String hql = "from Teacher t where t.teacherId = ? order by t.teacherId";
			Teacher teacher = (Teacher)session.createQuery(hql).setInteger(0, teacherId).uniqueResult();
			Set<Topic> set = teacher.getTopics();
			return set;
		} catch (HibernateException e) {
			e.printStackTrace();
			throw new DataException("db: teacher query topic by teacherId error");
		}
	}

	public synchronized void update(int topicId, Topic topic) throws DataException {
		Session session = HbnUtil.getSession();
		try {
			session.beginTransaction();
			String sql = "update t_topic set t_topicName = ?, t_topicContent = ?, t_topicProperty = ? where t_topicId = ?";
			session.createSQLQuery(sql)
				.setString(0, topic.getTopicName())
				.setString(1, topic.getTopicContent())
				.setString(2, topic.getTopicProperty())
				.setInteger(3, topicId)
				.executeUpdate();
			session.getTransaction().commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
			throw new DataException("teacher update topic error");
		} finally {
			HbnUtil.closeSession();
		}
	}

}

⌨️ 快捷键说明

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