数据库中的Java JTable结果 [英] Java JTable Result From Database

查看:85
本文介绍了数据库中的Java JTable结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a problem regarding my JTable, I don't know how can I store my fetch records which is of type String from my database to the multidimensional array which is let's say Object[][] data. What I want to do is show my database records to the JTable, I already fetch the records in dtabase and store it in my String variables, The question is how can I store the fetch records to the multidimensional array of Object and use it on my JTable.

这是我的获取记录的代码:

Here are my code for fetching records:

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


推荐答案

您从数据库中提取的数据需要先存储在数组中(按列)...

I'd suggest that the data you're pulling from the database needs to be stored in an array (by columns) first...

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;

这需要以某种行结构存储,我个人使用列表。这个选择的主要原因是你可能不知道预先读取的行数...

This then needs to be stored in some kind of row structure, I'd personally use a List. The main reason for this choice is that you probably don't know in advance the number of rows you will be reading...

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

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

rowList.add(rowData);

读完所有行后,需要将列表转换为数组。 。

Once you've completed reading all the rows, you need to convert the list it an array...

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

同样,你可以做...

Equally, you could do...

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

这更方便...

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

Now you should have a 2D array...

这篇关于数据库中的Java JTable结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆