ResultSet를 사용한 행 갯수



SQL 문 (COUNT()) 에 의한 Row 갯수

public class count {
 
  public static void main(String[] args) throws Exception {
        String jdbcUrl = "jdbc:mysql://localhost:3306/"+database;
        String dbId = id;
        String dbPass= passwd;
       
        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = DriverManager.getConnection(jdbcUrl,dbId,dbPass);       
        Statement stmt = conn.createStatement();
     
        int rowcount = 0;
        
        ResultSet rs = stmt.executeQuery("SELECT COUNT(*) FROM employee_tbl");
        if(rs.next()) {
          rowcount = rs.getInt(1);
        }
        System.out.println("Total rows : " + rowcount);
  }
}


ResultSet에 검색된 결과에 의한 Row 갯수 

public class count {
 
  public static void main(String[] args) throws Exception {
        String jdbcUrl = "jdbc:mysql://localhost:3306/"+database;
        String dbId = id;
        String dbPass= passwd;
       
        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = DriverManager.getConnection(jdbcUrl,dbId,dbPass);       
        Statement stmt = conn.createStatement();

        int rowcount = 0;
    
        ResultSet rs = stmt.executeQuery("SELECT COUNT(*) FROM employee_tbl");
        rs.last();     
        int rowcount = rs.getRow();
        rs.beforeFirst();

        System.out.println("Total rows : " + rowcount);         
  }
}


크리에이티브 커먼즈 라이센스
Creative Commons License

Posted by gwlee

2009/04/28 21:39 2009/04/28 21:39
, , , ,
Response
0 Trackbacks , 0 Comments
RSS :
http://thegreatgoodplace.com/tt/study/rss/response/121

Trackback URL : http://thegreatgoodplace.com/tt/study/trackback/121

Leave a comment

블로그 이미지

Stay Hungry Stary Foolish!

- gwlee

TC-Cumulus by reznoa requires Flash Player 9 or better.

Site Stats

Total hits:
60665
Today:
27
Yesterday:
48