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

📄 hibernateoperation.java

📁 考试管理,是的对计算机等级考试报名,管理的具体实现
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package basic;

import net.sf.hibernate.*;
import net.sf.hibernate.cfg.Configuration;

import basic.Student;

import java.util.*;

import javax.servlet.http.HttpSession;

import basic.*;

public class HibernateOperation{
  public static SessionFactory sessionFactory;
  static{
     try{
    
       Configuration config = new Configuration();
       config.addClass(Student.class)
       		 .addClass(Sc.class)
             .addClass(Computer.class)
             .addClass(Admin.class)
             ;
      // Get the session factory we can use for persistence
      sessionFactory = config.buildSessionFactory();
    }catch(Exception e){e.printStackTrace();}
  }
  public Student loadStudent(long id) throws Exception{
	    // Ask for a session using the JDBC information we've configured
	    Session session = sessionFactory.openSession();
	    Transaction tx = null;
	    try {
	      tx = session.beginTransaction();
	      Student s=(Student)session.get(Student.class,new Long(id));
	      Hibernate.initialize(s.getSc());
	      tx.commit();
	      return s;
	    }catch (Exception e) {
	      if (tx != null) {
	        // Something went wrong; discard all partial changes
	        tx.rollback();
	      }
	      throw e;
	    } finally {
	      // No matter what, close the session
	      session.close();
	    }
	  }



  public boolean checkLG(Student stu) throws Exception{
    Session session = sessionFactory.openSession();
    Transaction tx = null;
    try {
      tx = session.beginTransaction();
      List ip=session.find("from Student where name="+stu.getName()+" and pwd="+stu.getPwd());
      tx.commit();
      if(ip.size()>0)
      {
    	  return true;
      }
      else
      {
    	  return false;
      }
    }catch (Exception e) {
     e.printStackTrace();
     return false;
    } finally {
      session.close();
    }
  }
  public boolean Admin(Admin a) throws Exception{
	    Session session = sessionFactory.openSession();
	    Transaction tx = null;
	    try {
	      tx = session.beginTransaction();
	      List as=session.find("from Admin where name="+a.getName()+" and pwd="+a.getPwd());
	      tx.commit();
	      if(as.size()>0)
	      {
	    	  return true;
	      }
	      else
	      {
	    	  return false;
	      }
	    }catch (Exception e) {
	     e.printStackTrace();
	     return false;
	    } finally {
	      session.close();
	    }
	  }
  public boolean checkREG(Student stu) throws Exception{
	    Session session = sessionFactory.openSession();
	    Transaction tx = null;
	    try {
	      tx = session.beginTransaction();
	      List ip=session.find("from Student where num="+stu.getNum());
	      tx.commit();
	      if(ip.size()>0)
	      {
	    	  return true;
	      }
	      else
	      {
	    	  return false;
	      }
	    }catch (Exception e) {
	     e.printStackTrace();
	     return false;
	    } finally {
	      session.close();
	    }
	  }
  public void saveAll(Object user) throws Exception{//选课
  	  // Ask for a session using the JDBC information we've configured
    Session session = sessionFactory.openSession();
    Transaction tx = null;
    try {
      tx = session.beginTransaction();
      session.save(user);
      // We're done; make our changes permanent
      tx.commit();

    }catch (Exception e) {
      if (tx != null) {
        // Something went wrong; discard all partial changes
        tx.rollback();
      }
      throw e;
    } finally {
      // No matter what, close the session
      session.close();
    }
  }

  

  public List findStudent(String num) throws Exception{//登陆时用,将name存到session
	    // Ask for a session using the JDBC information we've configured
	    Session session = sessionFactory.openSession();
	    Transaction tx = null;
	    try {
	      tx = session.beginTransaction();
	      Query query=session.createQuery("from Student as c where c.num=:num");
	      query.setString("num",num);
	      List users=query.list();
	      tx.commit();
	      return users;
	    }catch (Exception e) {
	      if (tx != null) {
	        // Something went wrong; discard all partial changes
	        tx.rollback();
	      }
	      throw e;
	    } finally {
	      // No matter what, close the session
	      session.close();
	    }
	  }
  public Sc findSc(Student student,Computer computer) throws Exception{
	    // Ask for a session using the JDBC information we've configured
	    Session session = sessionFactory.openSession();
	    Transaction tx = null;
	    try {
	      tx = session.beginTransaction();
	      Query query=session.createQuery("from Sc where sid="+student.getId()+" and cid="+computer.getId());
	      List result=query.list();
	      Sc sc=(Sc)result.iterator().next();
	      tx.commit();
	      return sc;
	    }catch (Exception e) {
	      if (tx != null) {
	        // Something went wrong; discard all partial changes
	        tx.rollback();
	      }
	      throw e;
	    } finally {
	      // No matter what, close the session
	      session.close();
	    }
	  }

  public void deleteStudent(long a) throws Exception{
	    Session session = sessionFactory.openSession();
	    Transaction tx = null;
	    try {
	      tx = session.beginTransaction();
	      
	      //Sc sc=new Sc();	      
	      //student.getSc().add(sc);
//	      HibernateOperation h=new HibernateOperation(); 
//	      List list=(List) h.loadStudent(a);
//	      Student student=(Student)list;
	     // Student student=(Student)session.load(Student.class,new Long(a));
	      session.delete("from Student as c where c.id="+a+"");
	      tx.commit();	                
	    }catch (Exception e) {
	      if (tx != null) {
	        // Something went wrong; discard all partial changes
	        tx.rollback();
	      }
	      throw e;
	    } finally {
	      // No matter what, close the session
	      session.close();
	    }
}
  public void updateStudent(Student s) throws Exception{
	    Session session = sessionFactory.openSession();
	    Transaction tx = null;
	    try {
	      tx = session.beginTransaction();
	      session.update(s);
	      tx.commit();

	    }catch (Exception e) {
	      if (tx != null) {
	        tx.rollback();
	      }
	      throw e;
	    } finally {
	      // No matter what, close the session
	      session.close();
	    }
	  }
  public void updateSc(Sc sc) throws Exception{
	    Session session = sessionFactory.openSession();
	    Transaction tx = null;
	    try {
	      tx = session.beginTransaction();
	      session.update(sc);
	      tx.commit();

	    }catch (Exception e) {
	      if (tx != null) {
	        tx.rollback();
	      }
	      throw e;
	    } finally {
	      // No matter what, close the session
	      session.close();
	    }
	  }
  
  /**
   * 根据学号,课程号来判断这个学生是否选过这门课
  */
  public boolean checkComputerSelected(long sno,int cno) throws Exception{
////	    // Ask for a session using the JDBC information we've configured
	    Session session = sessionFactory.openSession();
	    boolean f;
	    Transaction tx = null;
	    try {
	      tx = session.beginTransaction();
	      Student s=(Student) session.load(Student.class,new Long(sno));
	      Computer c=loadComputer(cno);
	      Query query=session.createQuery("from Sc as sc where sc.student=:studentId and sc.computer=:computerId");
	      query.setLong("studentId",s.getId());
	      query.setLong("computerId",c.getId());
	      List computer=query.list();
	      Iterator it=computer.iterator();
	      if(it.hasNext())
	      {
	    	  f=true;
	      }
	      else
	      {
	    	  f=false;
	      }
	      

	      // We're done; make our changes permanent
	      tx.commit();
	      return f;

	    }catch (Exception e) {
	      if (tx != null) {
	        // Something went wrong; discard all partial changes
	        tx.rollback();
	      }
	      throw e;
	    } finally {
	      // No matter what, close the session
	      session.close();
	    }
	  }
  
  /**
   * 根据等级id加载课程对象;
  */
//  public Computer findComputer(String cno) throws Exception{
//	    // Ask for a session using the JDBC information we've configured
//	    Session session = sessionFactory.openSession();
//	    Transaction tx = null;
//	    try {
//	      tx = session.beginTransaction();
//	      Query query=session.createQuery("from Computer c where c.cno=:computerNumber");
//	      query.setString("computerNumber",cno);

⌨️ 快捷键说明

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