日期:2014-05-16 浏览次数:20483 次
@Jsp中Java实现增删改查@
/////////////////////////////////////////////////查询/////////////////////////////////
?//查询本班全部信息
?public String listStuAll(){
??Session session =new Configuration().configure().buildSessionFactory().openSession();
??try{
????Query query = session.createQuery("from StudentInfo where grade='"+grade_hidden+"'");
????stu_info = query.list();
????ActionContext.getContext().getSession().put("stu_list",stu_info);
??}
??catch(Exception e){
???e.printStackTrace();
??}
??session.close();
??return "stu_success";
?}
?
?//查询本班全部成绩
?public String listScoreAll(){
??Session session =new Configuration().configure().buildSessionFactory().openSession();
??try{
????score = session.createQuery("from Score where grade='"+grade_hidden+"'").list();
????ActionContext.getContext().getSession().put("score_list",score);
??}
??catch(Exception e){
???e.printStackTrace();
??}
??session.close();
??return "score_success";
?}
?
?
?//按学号查询学生信息
?public String stuFindBySno(){
??Configuration config = new Configuration().configure();
??SessionFactory sessionFactory = config.buildSessionFactory();
??Session session = sessionFactory.openSession();
??try{
???stu_info = session.createQuery("from StudentInfo where no='"+sno+"' and grade='"+grade_hidden+"'").list();
???if(stu_info.size() == 0){
????return "no_id";
???}
??}catch(RuntimeException e){
???throw e;
??}
??finally{
???session.close();
??}
??return "stu_success";
?}
?
?//按学号查询成绩
?public String ScoreFindBySno(){
??Configuration config = new Configuration().configure();
??SessionFactory sessionFactory = config.buildSessionFactory();
??Session session = sessionFactory.openSession();
??try{
???score = session.createQuery("from Score where no='"+sno+"' and grade='"+grade_hidden+"'").list();
???if(score.size() == 0){
????return "no_id";
???}
??}catch(RuntimeException e){
???throw e;
??}
??finally{
???session.close();
??}
??return "score_success";
?}
?
/////////////////////////////////////////////添加//////////////////////////////////////?
?//增加学生信息
?public String addStuInfo(){
??Configuration config = new Configuration().configure();
??SessionFactory sessionFactory = config.buildSessionFactory();
??Session session = sessionFactory.openSession();
??Transaction tx = session.beginTransaction();
??try{
???if(grade_hidden == "0")
???{
????session.save(stu_info.get(0));
????tx.commit();
???}
???else
???{
????stu_info.get(0).setGrade(grade_hidden);
????session.save(stu_info.get(0));
????tx.commit();
???}
??}catch(RuntimeException e){
???if(tx != null)
????tx.rollback();
???return "error_throw";
??}
??finally{
???session.close();
??}
??return "stu_success";
?}
?
?//增加学生成绩
?public String addStuScore(){
??System.out.println("grade:~~~~~~~~~~~grade:"+grade_hidden);
??Configuration config = new Configuration().configure();
??SessionFactory sessionFactory = config.buildSessionFactory();
??Session session = sessionFactory.openSession();
??Transaction tx = session.beginTransaction();
??try{
???stu_info = session.createQuery("from StudentInfo where no='"+score.get(0).getNo()+"' and grade='"+grade_hidden+"'").list();
???if(stu_info.size() == 0){
????return "no_id";
???}
???if(session.createQuery("from Score where no='"+score.get(0).getNo()+"' and c_name='"+score.get(0).getC_name()+"' and grade='"+grade_hidden+"'").list().size() == 0)
???{
????score.get(0).setGrade(grade_hidden);
????session.save(score.get(0));
????tx.commit();
???}
???else
????return "no_repeat";
??}catch(RuntimeException e){
???if(tx != null)
????tx.rollback();
???throw e;
??}
??finally{
???session.close();
??}
??return "score_success";
?}<