JTable 中的结果集

发布于 2024-08-26 08:47:31 字数 482 浏览 5 评论 0原文

如何在 JTable 中显示结果集。我正在使用这段代码

 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);

,但我只得到最后插入数据库的一行。

How to display ResultSet in JTable. i am using this code

 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);

but I get only one row which is lastly inserted to database.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

新雨望断虹 2024-09-02 08:47:31

您需要在代码周围放置一个 while 循环来迭代结果集。例如,

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

You need to put a while loop around your code to iterate over the result set. eg,

while(rset1.next())
{
//do something
}
乖不如嘢 2024-09-02 08:47:31

您列出的代码不完整/难以理解,但下面的代码显示了如何获取具有任意列数的 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);
}

The code that you have listed is incomplete/incomprehensible, but the code below shows how to take a ResultSet with an arbitrary number of columns and display its contents in a 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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文