2017-02-10 25 views
0

該數據存在返回的Java布爾函數是,我目前的功能。但是,它沒有返回任何東西。如果數據庫

public boolean authenticateDisabledRegions(int analystId, List<Integer> regionIds) { 
    Integer query = jdbcTemplate.queryForObject(
     "select count(*) from ANALYST_REGION_MAPPING WHERE STATUS = 0 AND ANALYST_ID = ? AND REGION_ID = ?", 
     Integer.class, analystId, regionIds); 

    return query != null && query > 0; 
} 

我試圖做到這一點,但它似乎並不奏效, 我一直在尋找它,有很多的建議,說fo與rs.next ResultSet中的RS() 我只是想知道這種類型的功能最好的方法是什麼。

這樣做的目的是檢查它是否存在,如果存在,它會調用更新(&更改狀態),如果沒有,它會調用插入。

+0

什麼是錯誤你得到你的querry? – Gatusko

+1

好吧,它會返回* * *。什麼是迴歸,它與你想要的有什麼不同?有沒有錯誤? – Carcigenicate

+0

請提供正確的源代碼。對於當前的方法參數不同於創建查詢的方法參數。 –

回答

0

這對我來說到底有什麼工作:

@Override 
public boolean doesRegionExist(int userId, List<Integer> regionIds) { 
    HashMap<String, Object> paramMap = new HashMap<String, Object>(); 
    paramMap.put("userId", analystId); 
    paramMap.put("regionIds", regionIds); 

    SqlRowSet result = namedParameterJdbcTemplate.queryForRowSet("SELECT * FROM USER_REGION_TABLE WHERE STATUS = 0 AND USER_ID = (:userId) AND REGION_ID IN (:regionIds)", paramMap); 

     if (result.next()) 
      return true; 
     else 
      return false; 

    } 
0

試試這個:

public boolean doesRegionExist(int userId, List<Integer> regionIds) { 
    Integer result = jdbcTemplate.queryForObject("select count(*) from USER_REGION_TABLE WHERE STATUS = 1 AND USER_ID = ? AND REGION_ID = ?", Integer.class, analystId, region); 

      return result != null & result > 0; 
    } 
+0

這不是一個解決方案,因爲'&'運算符只是一個二進制AND。 SQL查詢本身就是問題。 –

0

蹊蹺的代碼

public boolean doesRegionExist(int userId, List<Integer> regionIds) { 
Integer query = jdbcTemplate.queryForObject("select count(*) from USER_REGION_TABLE WHERE STATUS = 1 AND USER_ID = ? AND REGION_ID = ?", Integer.class, analystId, region); 

     return query != null && query > 0; 
} 

參數是帳戶及regionIds,但你傳遞analystId和地區成爲queryForObject()方法。

1

如果輸入的是你應該使用SQL的「IN」的條款和Spring的NamedParameterJdbcTemplate ID的列表。

public boolean doesRegionExist(int analystId, List<Integer> regionIds) { 
    MapSqlParameterSource params = new MapSqlParameterSource(); 
    params.addValue("analystId", analystId); 
    params.addValue("regionIds", regionIds); 

    Integer result = jdbcTemplate.queryForObject("SELECT COUNT(*) FROM USER_REGION_TABLE WHERE STATUS = 1 AND USER_ID = :analystId AND REGION_ID IN (:regionIds)", params, Integer.class); 

    return result != null && result > 0; 
} 

另見How to execute IN() SQL queries with Spring's JDBCTemplate effectivly?

+0

我收到此錯誤:HTTP狀態500 - 請求處理失敗;嵌套異常是org.springframework.jdbc.UncategorizedSQLException:PreparedStatementCallback;未分類SQLException USER_REGION_TABLE WHERE STATUS = 1 AND USER_ID =(:analystId)AND REGION_ID IN(:regionIds)]; SQL狀態[99999];錯誤代碼[17004];無效的列類型;嵌套的異常是java.sql.SQLException中:無效的列類型 – user7470849

+0

也許你需要定義參數類型使用'addValue'時 - http://docs.spring.io/spring-framework/docs/2.5.x/api/org /springframework/jdbc/core/namedparam/MapSqlParameterSource.html#addValue(java.lang.String,%20java.lang.Object,%20int,%20java.lang.String)和http://stackoverflow.com/questions/22444447/mapsqlparametersource - 不 - 不 - 地圖 - 正常與 - 是NamedParameterJdbcTemplate換 –