简体   繁体   中英

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. Using a for loop I run through an array with the names of tables in the DB.

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.

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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