来自数据库的Java JTable结果

时间:2012-09-03 05:16:15

标签: java database swing jdbc jtable

关于我的JTable我有一个问题,我不知道如何将我的数据库中的类型为String的fetch记录存储到多维数组中,这就是说Object [] []数据。我想要做的是将我的数据库记录显示给JTable,我已经在dtabase中获取记录并将其存储在我的String变量中。问题是我如何将获取记录存储到Object的多维数组并在我的上使用它JTable中。

以下是我获取记录的代码:

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

1 个答案:

答案 0 :(得分: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);

哪个更方便......

现在你应该有一个2D数组...