@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";
?}
///////////////////////////////////////////删除/////////////////////////////////////////////?
?//按学号删除学生信息
?public String stuDelBySno(){
??Configuration config = new Configuration().configure();
??SessionFactory sessionFactory = config.buildSessionFactory();
??Session session = sessionFactory.openSession();
??Transaction ts = session.beginTransaction();
??try{
???//ts = session.beginTransaction();
???stu_info = session.createQuery("from StudentInfo where no='"+sno+"' and grade='"+grade_hidden+"'").list();
???if(stu_info.size() == 0){
????return "no_id";
???}
???else
???{
????session.delete(stu_info.get(0));
????ts.commit();
???}
??}catch(RuntimeException e){
???if(ts != null)
????ts.rollback();
???throw e;
??}
??finally{
???session.close();
??}
??return "stu_del_success";
?}
?
?//按学号删除成绩
?public String ScoreDelBySno(){
??Configuration config = new Configuration().configure();
??SessionFactory sessionFactory = config.buildSessionFactory();
??Session session = sessionFactory.openSession();
??Transaction ts = session.beginTransaction();
??try{
???ts = session.beginTransaction();
???score = session.createQuery("from Score where no='"+score.get(0).getNo()+"' and c_name='"+score.get(0).getC_name()+"' and grade='"+grade_hidden+"'").list();
???if(score.size() == 0){
????return "no_id";
???}
???else
???{
????session.delete(score.get(0));
????ts.commit();
???}
??}catch(RuntimeException e){
???if(ts != null)
????ts.rollback();
???throw e;
??}
??finally{
???session.close();
??}
??return "score_del_success";
?}
?
///////////////////////////////////////修改学生信息/////////////////////////////////////////////////?
?//按学号修改学生信息(先查询)
?public String StuModBySno(){
??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_no";
???}
???else
???{
????no_mod_query = stu_info.get(0).getNo();
????grade_mod_query = stu_info.get(0).getGrade();?
????return "stu_mod_query_success";
???}
??}catch(RuntimeException e){
???throw e;
??}
??finally{
???session.close();
??}
?}
??
?//按学号修改学生信息(修改)
?public String StuModBySnoY(){
??System.out.println("cccccccccccccccccccccccccccccc________________________ccccccccccccccccccccccccccccccccccccccc");
??Configuration config = new Configuration().configure();
??SessionFactory sessionFactory = config.buildSessionFactory();
??Session session = sessionFactory.openSession();
??Transaction ts = session.beginTransaction();
??try{
???System.out.println("dddddddddddddddddddddddd________________________dddddddddddddddddd");
???ts = session.beginTransaction();
???session.createQuery("update StudentInfo set password='"+stu_info.get(0).getPassword()+"' ,name='"+stu_info.get(0).getName()+"' ,phone='"+stu_info.get(0).getPhone()+"' ,address='"+stu_info.get(0).getAddress()+"' where no='"+no_mod_query+"' and grade='"+grade_mod_query+"'").executeUpdate();
???ts.commit();????
??}catch(RuntimeException e){
???if(ts != null)
????ts.rollback();
???throw e;
??}
??finally{
???session.close();
??}
??return "stu_mod_success";
?}
???
///////////////////////////////////////修改成绩/////////////////////////////////////////////////?
?//按学号修改成绩(先查询)
?public String ScoreModBySno(){
??Configuration config = new Configuration().configure();
??SessionFactory sessionFactory = config.buildSessionFactory();
??Session session = sessionFactory.openSession();
??try{
???score = session.createQuery("from Score where no='"+score.get(0).getNo()+"' and c_name='"+score.get(0).getC_name()+"' and grade='"+grade_hidden+"'").list();
???if(score.size() == 0){
????return "no_id";
???}
???else
???{
????no_mod_query = score.get(0).getNo();
????grade_mod_query = score.get(0).getGrade();
????c_name_mod_query = score.get(0).getC_name();
???}
??}catch(RuntimeException e){
???throw e;
??}
??finally{
???session.close();
??}
??return "score_mod_query_success";
?}
??
?//按学号修改成绩(修改)
?public String ScoreModBySnoY(){
??Configuration config = new Configuration().configure();
??SessionFactory sessionFactory = config.buildSessionFactory();
??Session session = sessionFactory.openSession();
??Transaction ts = session.beginTransaction();
??try{
???ts = session.beginTransaction();
???session.createQuery("update Score set score='"+score.get(0).getScore()+"' where no='"+no_mod_query+"' and c_name='"+c_name_mod_query+"' and grade='"+grade_mod_query+"'").executeUpdate();
???ts.commit();????
??}catch(RuntimeException e){
???if(ts != null)
????ts.rollback();
???throw e;
??}
??finally{
???session.close();
??}
??return "score_mod_success";
?}
?