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

📄 userexistjudgedservice.java

📁 学校的课程管理及选课系统
💻 JAVA
字号:
package org.wangsong.service;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.wangsong.dao.hibernate.HibernateUtil;
import org.wangsong.exception.StudentIdExistException;

public class UserExistJudgedService
{
    public void studentIdExist(String id)throws StudentIdExistException
    {
		Session session = HibernateUtil.currentSession();
		HibernateUtil.beginTransaction();
	    String str="from Student as stu where stu.id=:id";
	    Query query=session.createQuery(str);
	    query.setString("id", id);
	    List list=query.list();
	    HibernateUtil.commitTransaction();
	    HibernateUtil.closeSession();
	   // session.close();
		if (list.size() > 0)
		{
			throw  new StudentIdExistException();
		} 
    }
    public boolean teacherIdExist(String id) 
    {
		Session session = HibernateUtil.currentSession();
		HibernateUtil.beginTransaction();
	    String str="from Teacher as tea where tea.id=:id";
	    Query query=session.createQuery(str);
	    query.setString("id", id);
	    List list=query.list();
	    HibernateUtil.commitTransaction();
	    HibernateUtil.closeSession();
		if (list.size() > 0)
		{
			 return true;
		} 
		return false;
    }
    public boolean courseIdExist(String id) 
    {
		Session session = HibernateUtil.currentSession();
		HibernateUtil.beginTransaction();
	    String str="from Course as cou where cou.id=:id";
	    Query query=session.createQuery(str);
	    query.setString("id", id);
	    List list=query.list();
	    HibernateUtil.commitTransaction();
	    HibernateUtil.closeSession();

	   // session.close();
		if (list.size() > 0)
		{
			 return true;
		} 
		return false;
    }
    public boolean classIdExist(String id) 
    {
		Session session = HibernateUtil.currentSession();
		HibernateUtil.beginTransaction();
	    String str="from Classes as cla where cla.id=:id";
	    Query query=session.createQuery(str);
	    query.setString("id", id);
	    List list=query.list();
	    HibernateUtil.commitTransaction();
	    HibernateUtil.closeSession();

	    
		if (list.size() > 0)
		{
			 return true;
		} 
		return false;
    }
    public boolean enrolExist(String classid,String stuid) 
    {
		try
		{
			Session session = HibernateUtil.currentSession();
			Connection conn = session.connection();
			Statement statment = conn.createStatement();
			String sql="select * from enrol where enrol.classes_id='"+classid+"' and enrol.stu_id='"+stuid+"'"; 
			//sql.execute("insert into enrol ( classes_id, stu_id) values('120','56451234');");
			 ResultSet result=statment.executeQuery(sql);  
			System.out.println("查询完毕!");

			
			if (result.next())
			{
				statment.close();
	             HibernateUtil.closeSession();

				 return true;
				 
			}
		}
		catch (HibernateException e)
		{
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}
		catch (SQLException e)
		{
			// TODO 自动生成 catch 块
			e.printStackTrace();
		} 
		 
		 HibernateUtil.closeSession();

		return false;
    }
}

⌨️ 快捷键说明

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