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

📄 personinfo.java

📁 Struts加Hibernate示例 用JBUILDER写的希望对初学着有一些帮助。
💻 JAVA
字号:
package com.dao.data;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

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

import com.dao.entity.Student;
import com.dao.service.UPerson;
import com.dao.utrl.HibernateSessionFactory;
import com.struts.form.LoginForm;
import com.struts.form.UpdateForm;

public class PersonInfo implements UPerson {

	// 删除信息
	public void del(Student formbean) {
		Transaction tr = null;
		try {
			Session seion = HibernateSessionFactory.getSession();
			tr = seion.beginTransaction();
			seion.clear();
			seion.delete(formbean);
			tr.commit();
		} catch (Exception ex) {
			tr.rollback();
			ex.printStackTrace();
		}

	}

	// 添加信息
	public void ins(Student formbean) {
		Transaction tr = null;
		try {
			Session seion = HibernateSessionFactory.getSession();
			tr = seion.beginTransaction();
			seion.save(formbean);
			tr.commit();
		} catch (Exception ex) {
			tr.rollback();
			ex.printStackTrace();
		}finally{
			this.close();
		}
	}

	// 登陆
	public boolean login(LoginForm formbean) {
		boolean b = false;
		try {
			Session seion = HibernateSessionFactory.getSession();
			String hql = "from Logininfo where lname=? and lpwd=?";
			Query query = seion.createQuery(hql);
			query.setParameter(0, formbean.getLoginname().trim());
			query.setParameter(1, formbean.getLpwd().trim());
			if (query.list().size() > 0) {
				b = true;
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}finally{
			this.close();
		}
		return b;
	}
	public void close(){
		HibernateSessionFactory.closeSession();
	}
	// 查询信息
	public List sel(Student formbean) {
		// TODO Auto-generated method stub
		List list = new ArrayList();
		try {
			Session seion = HibernateSessionFactory.getSession();
			String hql = "";
			Query query = null;
			hql = "from Student where sname like ? or sage like ?";
			if (formbean.getText().equals("*")) {
				hql = "from Student";
				query = seion.createQuery(hql);
			} else {
				query = seion.createQuery(hql);
				query.setParameter(0, "%" + formbean.getText().trim() + "%");
				query.setParameter(1, "%" + formbean.getText().trim() + "%");
			}
			seion.clear();
			list = query.list();
		} catch (Exception ex) {
			ex.printStackTrace();
		}finally{
			this.close();
		}
		return list;
	}
	//查询
	public static List s(String id){
		List list=new ArrayList();
		try {
			Session seion = HibernateSessionFactory.getSession();			
			String hql = "from Student where sid=?";
			Query query = seion.createQuery(hql);
			query.setParameter(0, new Integer(id));
			list= query.list();
		} catch (Exception ex) {
			ex.printStackTrace();
		}finally{
			HibernateSessionFactory.closeSession();
		}
		return list;
	}
	// 修改信息
	public void up(Student formbean) {
		Transaction tr = null;
		try {
			Session seion = HibernateSessionFactory.getSession();
			tr = seion.beginTransaction();
			seion.clear();
			seion.update(formbean);
			tr.commit();
		} catch (Exception ex) {
			tr.rollback();
			ex.printStackTrace();
		}finally{
			this.close();
		}
	}

	/**
	 * @param args
	 */

//	 public static void main(String[] args) {
//	 PersonInfo p = new PersonInfo();
//	 Student s = new Student();
//	 s.setSage("19");
//	 s.setSname("张三");
//	 s.setSsex("男");
//			
//	 //p.ins(s);
//	 //s.setText("19");
//	 s.setSid(new Integer(3));
//	// p.up(s);
//	 p.del(s);
//	 List list = p.sel(s);
//	 Iterator it = list.iterator();
//	 while (it.hasNext()) {
//	 Student s2 = (Student) it.next();
//	 System.out.println(s2.getSname().trim()+"\t"+s2.getSsex());
//	
//	 }
//	 }
}

⌨️ 快捷键说明

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