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

📄 coursedaoimpl.java

📁 基于ssh框架
💻 JAVA
字号:
package com.zzu.dao.impl;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.zzu.dao.CourseDao;
import com.zzu.dao.entity.Course;
import com.zzu.dao.entity.Student;

public class CourseDaoImpl implements CourseDao {
	Session se=null;
	Transaction tx=null;
	public List serch(Course condition) {
		// TODO Auto-generated method stub
		return null;
	}

	public Course serchById(String courseid) {
		Course course=null;
		Session se=HibernateSessionFactory.getSession();
		//Transaction tx=se.beginTransaction();
		String hql="from Course course where course.cno='"+courseid+"'";
		Query query=se.createQuery(hql);
		List list=query.list();
		if(list.size()>0)
		{
			
			course=(Course)list.get(0);
			System.out.println(course.getCname());
		}
		//tx.commit();
		
		return course;
	}
	
	public static void main(String[] args)
	{
		//new StudentDaoImpl().login("20052430202", "1");
		
		//new StudentDaoImpl().serchById("20052430206");
		
		//Student student=new StudentDaoImpl().serchById("20052430206");
		//Course course=new CourseDaoImpl().serchById("01");
		short a=2;
		Course course=new Course();
		
		course.setCno("05");
		course.setCcredit(a);
		course.setCname("收到");
		
		new CourseDaoImpl().add(course);
		
	}

	public void add(Course course) {
		try {
			se = HibernateSessionFactory.getSession();
			tx = se.beginTransaction();
			System.out.println(course.getCno());
			
			se.save(course);
			tx.commit();

			
			System.out.println("课程添加成功!");
			
		} catch (Exception e) {
			if (null != tx)
				tx.rollback();
			e.printStackTrace();
		} finally {
			se.close();
		}
		
	}

	public void del(Course course) {
		try {
			se = HibernateSessionFactory.getSession();
			tx = se.beginTransaction();
			se.delete(course);
			tx.commit();
			System.out.println("课程删除成功!");
			
		} catch (Exception e) {
			if (null != tx)
				tx.rollback();
			e.printStackTrace();
		} finally {
			se.close();// 关闭 Session
		}
		
	}

	public void update(Course course) {
		
		try{
			 se=HibernateSessionFactory.getSession();
			 tx=se.beginTransaction();

			se.update(course);
			
			System.out.println("课程信息修改成功!");
			tx.commit();
		}catch(Exception e)
		{
			if(null!=tx)
			{
				tx.rollback();
			}
		}finally{
			se.close();
		}
		
	}

	public Course getCourse(Class clz, Serializable id) {
		
		Course course=null;
		se=HibernateSessionFactory.getSession();
		try{
			
			course=(Course)se.get(clz, id);
		}catch(Exception e)
		{
			e.printStackTrace();
		}finally{
			se.close();
		}
		return course;
		
	}

}

⌨️ 快捷键说明

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