简体   繁体   English

将连续的(MySQL)结果集复制到单个多维数组中

[英]Copy consecutive (MySQL) resultsets into single multidimensional array

I would like to copy several resultset I'm obtaining from several tables from a MySQL DB into a single multidimesnsional array. 我想将我从MySQL数据库的几个表中获得的几个结果集复制到一个多维数组中。 Using a for loop I run through an array with the names of tables in the DB. 使用for循环,我遍历了一个带有数据库中表名的数组。

I've successfully selected the data and obtained a resultset and it is these various resultsets corresponding to the table I intend on copying into a single multidimensional array. 我已经成功选择了数据并获得了一个结果集,并且这些各种结果集与我打算复制到单个多维数组中的表相对应。 At the moment when I attempt printing out this multidimensional array it seems to be empty. 当我尝试打印此多维数组时,它似乎是空的。

In case you were wondering the number of rows and columns of the multidimensional array are known beforehand. 如果您想知道多维数组的行数和列数是事先已知的。 So in my code below the number of rows is 12 and number of columns is 18. 所以在下面的代码中,行数为12,列数为18。

Below is my EDITED code: 以下是我的编辑代码:

        // Pass array holding existing tables for selection of all data from each array element
    String[][] arr = null, current = null;
    int i = 0;
    int j = 0;

    for(int k = 0; k <= existingTablesInDBAsArr.length; k++) {

        String sql = "SELECT * FROM " + existingTablesInDBAsArr[k];

        try (
                //Connection conn = DBUtil.getConnection(DBType.MYSQL);
                Statement stmt = conn.createStatement();
                ResultSet rs = stmt.executeQuery(sql);
                ){

            System.out.println("\nRS as a string buffer: ");            

            while (rs.next()) {
                StringBuffer bf = new StringBuffer();

                bf.append(rs.getInt("entryID") + ": ");
                bf.append(rs.getString("agentID") +"~ ");
                bf.append(rs.getString("company") +"~ ");
                bf.append(rs.getString("contact") +"~ ");
                bf.append(rs.getString("telephone") +"~ ");
                bf.append(rs.getString("fax") +"~ ");
                bf.append(rs.getString("address") +"~ ");
                bf.append(rs.getString("email") +"~ ");
                bf.append(rs.getString("county") +"~ ");
                bf.append(rs.getString("postcode") +"~ ");
                bf.append(rs.getString("country") +"~ ");
                bf.append(rs.getString("admin_notes") +"~ ");
                bf.append(rs.getString("agent_notes") +"~ ");
                bf.append(rs.getString("enqr_taken_by") +"~ ");
                bf.append(rs.getString("enqr_type") +"~ ");
                bf.append(rs.getString("enqr_action") +"~ ");
                bf.append(rs.getString("enqr_date") +"~ ");
                bf.append(rs.getString("enqr_time"));

                System.out.println(bf.toString());
            }

            System.out.println("\nRS as an array:");

            try {

                try {
                    rs.beforeFirst();
                } catch(Exception ex) {
                    System.out.println("\nException WRT rs.first(); i.e. \n" + ex.toString());
                }

                ResultSetMetaData rsmd = rs.getMetaData();

                int columnSize = rsmd.getColumnCount();

                arr = new String[numbOfRows][columnSize];

                System.out.println("\ni outside while but before increment is: " +  i);
                System.out.println("j outside while but before increment is: " +  j);

                while(rs.next()) {
                    System.out.println("\ni inside while but before increment is: " +  i);
                    System.out.println("j inside while but before increment is: " +  j);

                    for(j = 0; j < columnSize; j++) {
                        arr[i][j] = rs.getString(j + 1).toUpperCase();

                        System.out.println("arr[" + i + "][" + j + "]: " + arr[i][j]);
                    }

                    System.out.println("\n");

                    i++;

                    System.out.println("\ni inside while but after increment is: " +  i);
                    System.out.println("j inside while but after increment is: " +  j + "\n");

                    if(i == arr.length) {
                        System.out.println("\n---\nEnqrsTableManager - i == arr.length");

                        System.out.println("\n---\nEnqrsTableManager - arr.length > 0\n---\nArray containing all entries from all tables is as follows: ");

                        for (int row = 0; row < arr.length; row++) {

                            System.out.println("arr[" + row + "][0]: " + arr[row][0]);
                            System.out.println("arr[" + row + "][1]: " + arr[row][1]);
                            System.out.println("arr[" + row + "][2]: " + arr[row][2]);
                            System.out.println("arr[" + row + "][3]: " + arr[row][3]);
                            System.out.println("arr[" + row + "][4]: " + arr[row][4]);
                            System.out.println("arr[" + row + "][5]: " + arr[row][5]);
                            System.out.println("arr[" + row + "][6]: " + arr[row][6]);
                            System.out.println("arr[" + row + "][7]: " + arr[row][7]);
                            System.out.println("arr[" + row + "][8]: " + arr[row][8]);
                            System.out.println("arr[" + row + "][9]: " + arr[row][9]);
                            System.out.println("arr[" + row + "][10]: " + arr[row][10]);
                            System.out.println("arr[" + row + "][11]: " + arr[row][11]);
                            System.out.println("arr[" + row + "][12]: " + arr[row][12]);
                            System.out.println("arr[" + row + "][13]: " + arr[row][13]);
                            System.out.println("arr[" + row + "][14]: " + arr[row][14]);
                            System.out.println("arr[" + row + "][15]: " + arr[row][15]);
                            System.out.println("arr[" + row + "][16]: " + arr[row][16]);
                            System.out.println("arr[" + row + "][17]: " + arr[row][17]);
                        }                   
                    }
                }

            } catch (Exception e) {
                System.out.println("\nError in generating 2D array from specific table resultset");
            }
        }
    }

I appreciate any assistance/suggestions. 感谢您的协助/建议。

The way you assign values to your array is probably causing the problem: 您为数组分配值的方式可能会引起问题:

System.out.println("arr[" + i + "][" + j + "]: " + (arr[i][j] = rs.getString(j + 1).toUpperCase()));

Should be changed to: 应更改为:

arr[i][j] = rs.getString(j + 1).toUpperCase();
System.out.println("arr[" + i + "][" + j + "]: " + arr[i][j]);

System.out.println should not contain anything other than a useful message to the console - definitely not variable assignments! System.out.println除了向控制台发送的有用消息外,不应包含任何其他内容-绝对不能包含变量分配!

EDIT: 编辑:

Also, be sure to reset your result set if you want to iterate over it again (for your second loop): 另外,如果要再次遍历结果集,请确保重设它(对于第二个循环):

 resultSet.first();

Otherwise the cursor will be at the end and you will not see any results. 否则,光标将位于末尾,您将看不到任何结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM