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

📄 businessservice.java

📁 学生选课管理系统 struts hibernate
💻 JAVA
字号:
package com.hibernate;



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


import java.util.*;

public class BusinessService{
  public static SessionFactory sessionFactory;
  static{
    try{
      // Create a configuration based on the properties file we've put
      // in the standard place.
      Configuration config = new Configuration();
      config.addClass(Student.class);
      config.addClass(Course.class);
      config.addClass(StudentCourse.class);
      config.addClass(Teacher.class);
      config.addClass(Message.class);
      // Get the session factory we can use for persistence
      sessionFactory = config.buildSessionFactory();
    }catch(Exception e){e.printStackTrace();}
  }

  public List findAllCourse() throws Exception{
    // Ask for a session using the JDBC information we've configured
    Session session = sessionFactory.openSession();
    Transaction tx = null;
    try {
      tx = session.beginTransaction();
      List courses=session.find("from Course as c order by c.name asc");
      tx.commit();
      return courses;
    }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 findAllMessage() 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 Message as m order by m.id asc");
	      tx.commit();
	      List message=query.list();
	      return message;
	    }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 saveStudent(Student student) 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.saveOrUpdate(student);
      // 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 void saveCourse(Course course) 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.saveOrUpdate(course);
      // 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 boolean findSc(Student s,Course c) throws Exception{
	    // Ask for a session using the JDBC information we've configured
	    Session session = sessionFactory.openSession();
	    Transaction tx = null;
	    try {
	      tx = session.beginTransaction();
	      String sql="from StudentCourse as sc";
	      List rs=session.find(sql);
	      Iterator it =  rs.iterator();
	      boolean a=false;
	      while(it.hasNext())
	      {
		      StudentCourse sc=(StudentCourse)it.next();
		      Student student=sc.getStudent();
		      Course course=sc.getCourse();     
		      if(course.equals(c)&&student.equals(s))
		      {
		    	   a=true;
		    	   break;
		      }
	      }
	      tx.commit();
	      return a;
	    }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 Student findStudent(String username) throws Exception{
	    // Ask for a session using the JDBC information we've configured
	    Session session = sessionFactory.openSession();
	    Transaction tx = null;
	    try {
	      tx = session.beginTransaction();
	      String sql="from Student as s where s.username='"+username+"'";

	      List rs=session.find(sql);
		 // List rs1=session.find(sql);
		  //Iterator if1=rs1.iterator();
//		  Student stu=(Student)rs.iterator().next();
      Iterator it = rs.iterator();
	      Student student=(Student)it.next();
	      tx.commit();
	      return student;
	    }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 checkLogin(Student stu) throws Exception{
      boolean flag = false;  
      try
      {
    	  Session session = sessionFactory.openSession();
    	  //创建一个Query对象
          Query query = session.createQuery("from Student as student where student.username=:username and student.password=:password");
          //动态绑定参数
          query.setString("username", stu.getUsername());
          query.setString("password", stu.getPassword());
          //执行查询语句,返回查询结果
          List list = query.list();
          if (list.size() > 0)
          {
              flag = true;
          }
          else
          {
              flag = false;
          }
      }
      catch (Exception e)
      {
          flag = false;
          e.printStackTrace();
      }
      return flag;
	  }
  
  public Student loadStudent(Integer 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,id);
	 
      Hibernate.initialize(s.getStudentCourses());
      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 StudentCourse loadSc(int 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();
	      StudentCourse sc=(StudentCourse)session.load(StudentCourse.class,new Integer(id));
		
	      Hibernate.initialize(sc.getStudent());
	      Hibernate.initialize(sc.getCourse());
	      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 Course loadCourse(int 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();
	      Course c=(Course)session.load(Course.class,new Integer(id));
	      Hibernate.initialize(c.getStudentCourses());
	      tx.commit();
	      return c;
	    }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 deleteSc(int 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();
	      session.delete("from StudentCourse as sc where id="+id);    
	      // 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 void deleteStu(int 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();
	      session.delete("from Student as s where s.id="+id);    
	      // 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 void deleteStu(Student stu) 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.delete("from Studentas s where id="+stu.getId());    
	      // 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 String findTime(Student s,String timename) throws Exception{
	    // Ask for a session using the JDBC information we've configured
	    Session session = sessionFactory.openSession();
	    Transaction tx = null;
	    tx = session.beginTransaction();
	    String name="rest";
	    try {
	        s=loadStudent(s.getId());
	    	Iterator it = s.getStudentCourses().iterator();
		  	while(it.hasNext())
		  	  {
		  		StudentCourse sc=(StudentCourse)it.next();
		  		  String time=sc.getCourse().getTime();
		  		  if(time.indexOf(timename)!=(-1))
		  			 {
		  			  name= sc.getCourse().getName();
		  		  	  break;
		  		  	  }
		  	  }
	      tx.commit();	  
	      return name;
	    }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 loadCourseteacher(int cid,int tid) throws Exception
  {
 	 Session session = sessionFactory.openSession();
 	    Transaction tx = null;
 	    try {
 	      // Create some data and persist it
 	      tx = session.beginTransaction();
 	      
 	    Course c=(Course)session.load(Course.class,new Integer(cid));
 	    Teacher t=(Teacher)session.load(Teacher.class, new Integer(tid));
 	   c.setTea(t);
 	 t.getCourses().add(c);
 	      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 findPageCourse(int m,int n)throws Exception{
	  Session session = sessionFactory.openSession();
	    Transaction tx = null;
	    try {
	      tx = session.beginTransaction();
	      Query query=session.createQuery("from Course as c order by c.id desc");
	      query.setFirstResult(m);
	      query.setMaxResults(n);
	      List list=query.list();
	      return list;
	    }catch (Exception e) {
			if (tx != null) {
		        tx.rollback();
		        e.printStackTrace();
		      }
		      throw e;
		    } finally {
		      session.close();
		    }
  }
  
	 
	  
	 
	  

	  

	  public static void main(String args[]) throws Exception {
		  Teacher teacher=new Teacher();
	      Course course=new Course();
	    
	     // findPageCourse
		  // saveTeacherAndCourse(teacher,course);
		 //  loadCourse(1);
		  
//new BusinessService().loadCourseteacher(1,2);
	      //new BusinessService().findPageCourse(1, 2);
	      //new BusinessService().checkLogin(null);
	   // Student student=new BusinessService().loadStudent(new Integer(8));
	  
     List list=new BusinessService().findAllMessage();
     Iterator it=list.iterator();
     while(it.hasNext()){
    	 Message m=(Message) it.next();
  String a= m.getAddress();
  int b= m.getId().intValue();
  System.out.println(b);
  System.out.println("********");
 
     }
     
//   Message message=new Message("3","3","3","3",student);
//	     student.setMessage(message);
//////	      
//     new BusinessService().saveStudent(student);
	    sessionFactory.close();
	  }
}

⌨️ 快捷键说明

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