0

我的 JTable 有问题,我不知道如何将 String 类型的提取记录从我的数据库存储到多维数组中,比如 Object[][] 数据。我想要做的是向 JTable 显示我的数据库记录,我已经在数据库中获取记录并将其存储在我的 String 变量中,问题是如何将获取记录存储到 Object 的多维数组并在我的表。

这是我获取记录的代码:

static class TableData{
    Object[][] data;
    int count = 0;
    Statement sql = null;
    String query, user = "JEROME", pass = "Perbert101", driver = "oracle.jdbc.OracleDriver", conString = "jdbc:oracle:thin:@127.0.0.1:1521:XE";
    Connection con = null;
    ResultSet rs = null;
    TableData(){
        try{
            Class.forName(driver);
        }
        catch(ClassNotFoundException e){
            JOptionPane.showMessageDialog(null, "Problem Loading Driver");
        }
        try{
            con = DriverManager.getConnection(conString, user, pass);
            sql = con.createStatement();
            sql.executeQuery("SELECT * FROM INVENTORY");
            rs = sql.getResultSet();
            int key = 0;
            String val = null, val1 = null, val2 = null, val3 = null, val4 = null, val5 = null;
            System.out.println("Results: ");
            while(rs.next()){


                    key = rs.getInt(1);
                    if(rs.wasNull()){
                        key = -1;
                    }
                    val = rs.getString(2);
                    if(rs.wasNull()){
                        val = null;
                    }
                    val1 = rs.getString(3);
                    if(rs.wasNull()){
                        val = null;
                    }
                    val2 = rs.getString(4);
                    if(rs.wasNull()){
                        val = null;
                    }

                    val3 = rs.getString(5);
                    if(rs.wasNull()){
                        val = null;
                    }
                    val4 = rs.getString(6);
                    if(rs.wasNull()){
                        val = null;
                    }
                    val5 = rs.getString(7);
                    if(rs.wasNull()){
                        val = null;
                    }

                System.out.println("Key = " + key);
                System.out.println("value = " + val);
                System.out.println("value = " + val1);
                System.out.println("value = " + val2);
                System.out.println("value = " + val3);
                System.out.println("value = " + val4);
                System.out.println("value = " + val5);

            }


            sql.close();
            con.close();
        }
        catch(SQLException e){
            JOptionPane.showMessageDialog(null, "Error Loading Database Data");
        }

    }
}
//----------END------------
public static void main(String[] args){
    POSModel.TableData data = new POSModel.TableData();
}
4

1 回答 1

1

我建议您从数据库中提取的数据首先需要存储在一个数组中(按列)...

Object[] rowData = new Object[7];
rowData[0] = key;
rowData[1] = val;
rowData[2] = val1;
rowData[3] = val2;
rowData[4] = val3;
rowData[5] = val4;
rowData[6] = val5;

然后需要将其存储在某种行结构中,我个人会使用List。这种选择的主要原因是您可能事先不知道您将阅读的行数......

List<Object[]> rowList = new ArrayList<Object[]>(25);

// Process the resultset...
// Create the column array from above...

rowList.add(rowData);

完成读取所有行后,您需要将列表转换为数组...

data = rowList.toArray(new Object[](rowList.size())); // I like to provide my own array

同样,你可以做...

data = new Object[rowList.size()][7];
rowList.toArray(data);

哪个更方便...

现在你应该有一个二维数组......

于 2012-09-03T05:19:17.220 回答