2016-03-13 93 views
1

我已經有一個準備語句的方法,它與Connection類一起使用。如何讓我可以使用Hibernate會話?用Hibernate Session和Spring MVC編寫語句

@SuppressWarnings("unchecked") 
    @Override 
    public List<Users> listUsersSort(int weight, String gender, String place, int ageTo, String currentUser) { 
     System.out.println(weight+gender+place+ageTo+currentUser); 
     Connection dbConnection = null; 
     PreparedStatement preparedStatement = null; 


     int iterator=0; 
     List<Users> usersList =null; 
     String selectSQL="select users.username, users.checkusr, users.password, users.name, users.enabled, users.surname, users.email, users.gender, users.age, users.weight, users.height, users.sport, users.place, users.photo from users where users.enabled = true"; 
     System.out.println(selectSQL); 
     try { 
      dbConnection.prepareStatement(selectSQL); 
     } catch (SQLException e) { 
      System.out.println("first catch block"); 
      e.printStackTrace(); 
     } 
     if (weight<40 == false) { 

      String weightParam = " AND users.weight <= ?"; 
      selectSQL=selectSQL.concat(weightParam); 
      System.out.println(selectSQL); 
      try { 
       preparedStatement.setInt(iterator++, weight); 
      } catch (SQLException e) { 
       System.out.println("second catch block"); 
       e.printStackTrace(); 
      } 
      System.out.println(selectSQL); 
     } 
     if (gender.isEmpty() == false) { 
      String genderParam = " AND users.gender LIKE '?'"; 
      selectSQL=selectSQL.concat(genderParam); 
      try { 
       preparedStatement.setString(iterator++, gender); 
      } catch (SQLException e) { 
       e.printStackTrace(); 
      } 
     } 

     if (place.isEmpty() == false) { 
      String placeParam = " AND users.place LIKE '?'"; 
      selectSQL=selectSQL.concat(placeParam); 
      try { 
       preparedStatement.setString(iterator++, place); 
      } catch (SQLException e) { 
       e.printStackTrace(); 
      } 
     } 
     if (ageTo<40 == false) { 
      String age = " AND users.age <= ?"; 
      selectSQL=selectSQL.concat(age); 
      try { 
       preparedStatement.setInt(iterator++, ageTo); 
      } catch (SQLException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
     String withoutUser=" AND users.username NOT LIKE '?'"; 
     selectSQL=selectSQL.concat(withoutUser); 
     try { 
      preparedStatement.setString(iterator++, currentUser); 
     } catch (SQLException e) { 
      e.printStackTrace(); 
     } 
     System.out.println("FINAL QUERY IS: " + selectSQL); 
     ResultSet rs=null; 
     try { 
      rs = preparedStatement.executeQuery(); 
     } catch (SQLException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     try { 
      while (rs.next()) { 

       String username = rs.getString("username"); 

       System.out.println("username : " + username); 

      } 
     } catch (SQLException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     return usersList; 
    } 

回答

0

與Hibernate實現這一目標的最好方法是使用native queries。例如,如果您使用的Session接口:

SQLQuery sqlQuery = session.createSQLQuery(selectSQL); 
+0

它不工作 –

+0

@SergShapoval什麼是你的錯誤? –

+0

我有一個NullPointer。也許你有一些與PS會話的例子? –