2012-01-11 4 views

답변

5

Count이 경우 더 좋습니다. 당신과 같이 사용할 수 있습니다 :

public static int countRows(Connection conn, String tableName) throws SQLException { 
    // select the number of rows in the table 
    Statement stmt = null; 
    ResultSet rs = null; 
    int rowCount = -1; 
    try { 
     stmt = conn.createStatement(); 
     rs = stmt.executeQuery("SELECT COUNT(*) FROM " + tableName + " WHERE.... "); 
     // get the number of rows from the result set 
     rs.next(); 
     rowCount = rs.getInt(1); 
    } finally { 
     rs.close(); 
     stmt.close(); 
    } 
    return rowCount; 
    } 

here에서 촬영.

+0

뭔가를 할 수 있지만,이 경우에도 묻는 질문에 대답하지 않습니다. – user1377392

+0

@ user1377392 : 주어진 조건을 만족하는 요소의 양을 계산하고 있습니다. 결과가 0보다 크면 존재합니다. 전체 결과 집합을 반환하지 않으므로 계산이 더 좋을 수 있으므로 데이터 전송이 최소화됩니다. – npinti

1

당신이 할 수있는을 그 네 단계이다.

  1. SQL을 작성하십시오. select count(1) from table where column = 34343과 같은 것입니다.
  2. JDBC를 사용하여 연결하는 방법을 익히십시오.
  3. 자바에서 PreparedStatement에 대해 자세히 알아보십시오.
  4. ResultSet에서 값을 읽는 방법에 대해 자세히 알아보십시오.
1
select case 
      when exists (select 1 
         from table 
         where column_ = '<value>' and rownum=1) 
      then 'Y' 
      else 'N' 
     end as rec_exists 
from dual; 
+0

그냥 똑똑한 !!!!. 대답으로 받아 들여 졌어야만합니다. 백작 (*)은 실제로 오랜 시간이 걸릴 것입니다. 이것을 사용하는 것이 훨씬 빠릅니다. – Algorithmist

3

내가이 이전 게시물 알고

private boolean hasRecord(String id) throws SQLException { 
    String sql = "Select 1 from MyTable where id = ?"; 

    PreparedStatement ps = dbConn.prepareStatement(sql); 
    ps.setString(1,id); 
    ResultSet rs = ps.executeQuery(); 

    return rs.next(); 
} 
관련 문제