2010-03-23 2 views
0

JTable에 결과 세트를 표시하는 방법. 이 코드를 사용하고 있습니다.JTable의 ResultSet

String [] record= new 
    String[ColCount]; 
    for (i=0; i<ColCount; i++) 
    { 
    record[i]=rset1.getString(i+1); 

    } 
    cell[i] = rset1.getString("loginname"); 
    cell[i] = rset1.getString("role"); 
    System.out.println(cell[i][0]); 
    //ItemGroup = rset1.getString("Status"); 
    } 
    System.out.println(ItemCode); 
    JTable jt = new JTable( 
    cell[i], headers); 

그러나 마지막으로 데이터베이스에 삽입되는 행은 하나뿐입니다.

+5

코드를 주석 처리하여 시작할 수 있습니까? – skaffman

+0

코드는 아무런 의미가 없습니다. 당신이 프로그래머라면 전혀 물어 보지 않는 많은 수준에서 잘못된 것입니다. –

답변

1

결과 집합을 반복 처리하려면 코드 주위에 while 루프를 넣어야합니다. 예를 들어,

while(rset1.next()) 
{ 
//do something 
} 
0

당신이 나열했습니다 코드/불완전 이해할 수 있지만, 아래의 코드는 컬럼의 임의의 수와의 ResultSet을 가지고 JTable로 그 내용을 표시하는 방법을 보여줍니다.

import java.sql.ResultSet; 
import java.sql.ResultSetMetaData; 
import java.util.ArrayList; 

private void viewTable(){ 
    //Perform database query here, which will open a Connection to the database 
    //Assume we use the above query to populate a ResultSet results 

    //Get information about the ResultSet 
    ResultSetMetaData metaData = results.getMetaData(); 

    //Gets the number of columns in results 
    int columnCount = metaData.getColumnCount(); 
    //Gets the name of each column in results 
    String[] columnNames = new String[columnCount]; 
    for(int i = 0; i < columnNames.length; i++){ 
     columnNames[i] = metaData.getColumnLabel(i+1); 
    } 

    //You can use a String[] to keep track of the rows if you know the # 
    //# of rows in the ResultSet, this implementation assumes that we don't 
    //This ArrayList will keep track of each row in results (each row is 
    //represented by a String[] 
    ArrayList<String[]> rows = new ArrayList<>(); 
    while(results.next()){ 
     //Fetch each row from the ResultSet, and add to ArrayList of rows 
     String[] currentRow = new String[columnCount]; 
     for(int i = 0; i < columnCount; i++){ 
      //Again, note that ResultSet column indecies start at 1 
      currentRow[i] = results.getString(i+1); 
     } 
     rows.add(currentRow); 
    } 

    //Close Connection to the database here 

    String[][] rowsArray = new String[rows.size()][columnCount]; 
    rowsArray = rows.toArray(rowsArray); 
    table = new JTable(rowsArray, columnNames); 
}