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

📄 choiceutil.java

📁 J2EE电子商务系统开发从入门到精通---基于Struts和Hibernate技术实现
💻 JAVA
字号:
/*
 * ChoiceUtil.java
 *
 * Created on 2006年8月12日, 下午9:18
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package model.vote.hibernate;
import dbservice.hibernate.HibernateService;
import java.util.HashSet;
import java.util.List;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;

/**
 *
 * @author Administrator
 */
public class ChoiceUtil {
    public static boolean insert(String themeId, Choice choice) {
        Transaction transaction = null;
        Session session = null;
        boolean b = false;
        try {
            session = HibernateService.getSession();
            transaction = session.beginTransaction();
            ThemeUtil.addChoices(themeId, choice);
            session.save(choice);
            transaction.commit();
            b = true;
        } 
        catch (HibernateException he) {
            he.printStackTrace();
            HibernateService.rollbackTransaction(transaction);
            b = false;
        } 
        catch (Exception e) {
            e.printStackTrace();
            b = false;
        } 
        finally {
            HibernateService.closeSession(session);
            return b;
        }
    }
    
    public static boolean update(String id, String themeId, Choice choice) {
        Transaction transaction = null;
        Session session = null;
        boolean b = false;
        try {
            session = HibernateService.getSession();
            transaction = session.beginTransaction();
            Choice c = new Choice();
            session.load(c, Integer.valueOf(id));
            c.setName(choice.getName());
            
            ThemeUtil.addChoices(themeId, c);
            session.update(c);
            transaction.commit();
            b = true;
        } 
        catch (HibernateException he) {
            he.printStackTrace();
            HibernateService.rollbackTransaction(transaction);
            b = false;
        } 
        catch (Exception e) {
            e.printStackTrace();
            b = false;
        } 
        finally {
            HibernateService.closeSession(session);
            return b;
        }
    }
    
    public static List findAll(String themeId) {
        List list = null;
        list = HibernateService.execQuery("from Choice c where c.theme.id=" + themeId);
        return list;
    }
    
    public static Choice find(String id) {
        Choice choice = null;
        Transaction transaction = null;
        Session session = null;
        try {
            session = HibernateService.getSession();
            transaction = session.beginTransaction();
            choice = new Choice();
            session.load(choice, Integer.valueOf(id));
            transaction.commit();
        } 
        catch (HibernateException he) {
            he.printStackTrace();
            HibernateService.rollbackTransaction(transaction);
            choice = null;
        } 
        catch (Exception e) {
            e.printStackTrace();
            choice = null;
        }
        finally {
            HibernateService.closeSession(session);
            return choice;
        }
    }
    
    public static boolean delete(String id) {
        Transaction transaction = null;
        Session session = null;
        boolean b = false;
        try {
            session = HibernateService.getSession();
            transaction = session.beginTransaction();
            Choice choice = new Choice();
            session.load(choice, Integer.valueOf(id));
            session.delete(choice);
            transaction.commit();
            b = true;
        } 
        catch (HibernateException he) {
            he.printStackTrace();
            HibernateService.rollbackTransaction(transaction);
            b = false;
        } 
        catch (Exception e) {
            e.printStackTrace();
            b = false;
        } 
        finally {
            HibernateService.closeSession(session);
            return b;
        }
    }
    
    public static boolean addResults(String id, Result result) {
        Transaction transaction = null;
        Session session = null;
        boolean b = false;
        try {
            session = HibernateService.getSession();
            transaction = session.beginTransaction();
            Choice choice = new Choice();
            session.load(choice, Integer.valueOf(id));
            result.setChoice(choice);
            session.update(choice);
            transaction.commit();
            b = true;
        } 
        catch (HibernateException he) {
            he.printStackTrace();
            HibernateService.rollbackTransaction(transaction);
            b = false;
        } 
        catch (Exception e) {
            e.printStackTrace();
            b = false;
        } 
        finally {
            HibernateService.closeSession(session);
            return b;
        }
    }
}

⌨️ 快捷键说明

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