?? dbteacherimpl.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 + -