package sedion.xq.DAO;
import java.util.List;
import javax.swing.JOptionPane;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import sedion.xq.ORM.Stuinfo;
import sedion.xq.hibernate.HibernateSessionFactory;
import sun.swing.UIAction;
public class StudentDao {
private Transaction transaction;
private Session session;
private Query query;
public StudentDao() {
}
// 显示
public List findAllInfo() {
session = HibernateSessionFactory.getSession();
try {
transaction = session.beginTransaction();
String querysString = "from Stuinfo" ;
query = session.createQuery(querysString);
List<Stuinfo> list = query.list();
System.out.println(list);
transaction.commit();
session.close();
return list;
} catch (Exception e) {
message( "findInfo.error" + e);
e.printStackTrace();
return null ;
}
}
// save message
public boolean saveInfo(Stuinfo info) {
try {
session = HibernateSessionFactory.getSession();
transaction = session.beginTransaction();
session.save(info);
transaction.commit();
session.close();
return true ;
} catch (Exception e) {
message( "saveInfo.error" + e);
e.printStackTrace();
return false ;
}
}
public boolean deleteInfo(String id) {
try {
session = HibernateSessionFactory.getSession();
transaction = session.beginTransaction();
Stuinfo info = new Stuinfo();
info = (Stuinfo) session.get(Stuinfo. class , id);
session.delete(info);
transaction.commit();
session.close();
return true ;
} catch (Exception e) {
message( "deleteInfo.error" + e);
e.printStackTrace();
return false ;
}
}
public List<Stuinfo> findInfo(String id) {
try {
session = HibernateSessionFactory.getSession();
transaction = session.beginTransaction();
query = session.createQuery( "from Stuinfo stu where stu.id like?" );
query.setString( 0 , id);
List<Stuinfo> list = query.list();
System.out.println(list);
transaction.commit();
session.close();
return list;
} catch (Exception e) {
message( "deleteInfo.error" + e);
e.printStackTrace();
return null ;
}
}
public boolean updateInfo(Stuinfo info,String id) {
try {
System.out.println(id+ "didiididid" );
session = HibernateSessionFactory.getSession();
transaction = session.beginTransaction();
query = session.createQuery( "delete from Stuinfo stu where stu.id like?" );
query.setString( 0 , id);
query.executeUpdate();
session.saveOrUpdate(info);
transaction.commit();
session.close();
return true ;
} catch (Exception e) {
message( "updateInfo.error" + e);
e.printStackTrace();
return false ;
}
}
private void message(String mess) {
int type = JOptionPane.YES_NO_CANCEL_OPTION;
String title = "提示消息" ;
JOptionPane.showMessageDialog( null , mess, title, type);
}
}
|