2013-01-20 4 views
1

임은 자바 EE와 더비 작업, 난 내 결과 집합에서 데이터를 얻을 INT 및 문자열에 넣어하려고하지만 내게이 오류 준다 작동하지 않습니다는 결과 집합 오류가

은 java.sql.SQLException을 : 잘못된를 현재 커서 위치에 대한 작업. 당신은 결과 집합 next() 방법을 사용할 필요가

Connection conn = null; 
    Statement stmt = null; 
    ResultSet result = null; 

    Hotel hot = new Hotel(); 
    try { 
     synchronized (dataSource) { 
      conn = dataSource.getConnection(); 
     } 



     stmt = conn.createStatement(); 
     String req = "SELECT * FROM hotel WHERE num = " + num; 
     result = stmt.executeQuery(req);                

     } 

     //result.next();   

     int xxnum = result.getInt(1); 
     String nom = result.getString("nom"); 
     String villeV = result.getString("ville"); 
     int etoilesV = result.getInt("etoiles"); 
     String directeur = result.getString("directeur"); 

     Hotel hol = new Hotel(num, nom, villeV, etoilesV, directeur);     

     result.close(); 
     stmt.close(); 
     return hol; 
    } catch (SQLException ex) { 
     throw new DAOException("probl�me r�cup�ration de la liste des hotels !!", ex); 
    } finally { 
     closeConnection(conn); 
    } 

답변

2

:

내가 result.next()하지만 아무것도 tryed, 여기에 내 코드입니다.

Javadocs here을 확인하고 아래 관련 부분을 잘라 냈습니다.

커서를 현재 위치에서 한 행 froward 이동합니다. ResultSet 커서는 처음에 첫 번째 행 앞에 배치됩니다.; 다음에 메소드를 처음 호출하면 첫 번째 행이 현재 행이됩니다. 두 번째 호출은 두 번째 행을 현재 행으로 만드는 식입니다.

그래서 당신은 당신의 코드에서이 작업을 수행 할 필요가 :

if(result.next()) 
{ 
    int xxnum = result.getInt(1); 
    String nom = result.getString("nom"); 
    String villeV = result.getString("ville"); 
    int etoilesV = result.getInt("etoiles"); 
    String directeur = result.getString("directeur"); 
} 
4

java.sql.SQLException: Invalid operation for the current cursor location. 

을 사용하여 다음 위치로 커서를 설정하지 않음으로써 발생됩니다 오류

result.next(); 

전화 번호는 if

입니다.3210
if (result.next()) { 
    // build Hotel object 
    ... 
} 

여전히 결과가 표시되지 않으면 데이터베이스에 대해 SQL을 직접 실행하고 레코드가 반환되는지 확인하십시오. 그렇지 않으면 쿼리 나 데이터를 적절하게 조정하십시오.


사이드 참고 :

  • 사용 PreparedStatementSQL Injection 공격으로부터 보호 할 수 있습니다.
  • finally 블록에서 result.close();stmt.close();으로 전화를 겁니다.