2017-11-18 12 views
0

다른 클래스에서 검색하려고하는 결과 집합은 쿼리가 작동하더라도 null을 반환합니다. 데이터베이스에 보관 된 레코드를 기반으로 개체를 초기화하려고합니다. 거기에 처음에 sqlite에 레코드가, 나는 최신 date.Else와 하나를 검색, 나는 mysql 데이터베이스에서 가장 빠른 것을 검색하려고합니다. MySQL 데이터베이스에서 설정 결과를 검색하도록되어 코드는 다음과 같다 :다른 클래스의 null 결과 문

public ResultSet lowestDate() throws SQLException { 
    ResultSet rs1 = null; 
    String resultQuery = "SELECT * FROM alarm ORDER BY `timestamp` ASC LIMIT 1"; 
    rs1 = stmt.executeQuery(resultQuery); 
    return rs1; 
} 

문이 초기화 globally.And이 같은 다른 클래스에서이 호출된다

public void setLastAlarm() throws SQLException, ParseException { 
    String liteQuery = "SELECT * FROM alarm_entries ORDER BY date(`timestamp`) DESC LIMIT 1"; 
    conn.connectLite(); 
    Connection getCon = conn.getLiteConnection(); 
    try { 
     stmt = getCon.createStatement(); 
    } catch (SQLException e) { 
     e.printStackTrace(); 
    } 
    try { 
     rs = stmt.executeQuery(liteQuery); 
     if (rs.next()) { 
      //while (rs.next()) { 
      nuDate = rs.getString("timestamp"); 
      newDate = format.parse(nuDate); 
      lastAlarm.setBacklogId(rs.getBytes("backlog_id")); 
      lastAlarm.setTimestamp(newDate); 
      //} 
     } 
     else{ 
      rsq=mysqlConnection.lowestDate(); 
      lastAlarm.setTimestamp(format.parse(rsq.getString("timestamp"))); 
      lastAlarm.setBacklogId(rsq.getBytes("backlog_id")); 
     } 
    }catch (Exception e){ 
     e.printStackTrace(); 
    } 
} 

public void run() { 
    try { 
     setLastAlarm(); 

답변

0

당신은에 ResultSet#next()를 호출하지 결과 집합은 lowestDate() 도우미 메서드에서 반환됩니다. 따라서 커서는 결과 집합의 첫 번째 (및 유일한) 레코드로 진행되지 않습니다. 하지만 이런 식으로 JDBC 코드를 고려하는 것은 나쁜 생각이라고 생각합니다. 대신, 그냥이처럼 두 개의 쿼리를 인라인 :

try { 
    rs = stmt.executeQuery(liteQuery); 
    if (rs.next()) { 
     nuDate = rs.getString("timestamp"); 
     newDate = format.parse(nuDate); 
     lastAlarm.setBacklogId(rs.getBytes("backlog_id")); 
     lastAlarm.setTimestamp(newDate); 
    } 
    else { 
     String resultQuery = "SELECT * FROM alarm ORDER BY timestamp LIMIT 1"; 
     rs = stmt.executeQuery(resultQuery); 
     if (rs.next()) { 
      String ts = rs.getString("timestamp"); 
      lastAlarm.setTimestamp(format.parse(ts)); 
      lastAlarm.setBacklogId(rs.getBytes("backlog_id")); 
     } 
    } 
} catch (Exception e){ 
    e.printStackTrace(); 
} 
+0

그것은 작동하지 않았다, 그냥 모든 일 위에 점프이야, 왜 이런 일이 내가 같은 쿼리를 조회 할 때 데이터가 – Morbidity

+0

아마도 첫 번째 쿼리가 반환 절대로 비어 있지 않습니다. 테이블에서 실제 데이터를 확인 했습니까? –

+0

그래, 내가 무슨 뜻인지 테이블에있는 데이터를 확인하고 쿼리가 거기에서 작동하고있다 – Morbidity