我想复制几个结果集,我从MySQL DB中的几个表中获取一个多维数组。使用for循环我运行一个数组,其中包含数据库中表的名称。
我已经成功选择了数据并获得了结果集,这些不同的结果集对应于我打算复制到单个多维数组中的表。在我尝试打印出这个多维数组时,它似乎是空的。
如果您想知道事先知道多维数组的行数和列数。因此,在我的代码中,行数为12,列数为18。
以下是我的编辑代码:
// 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");
}
}
}
我感谢任何帮助/建议。
答案 0 :(得分:0)
为数组赋值的方法可能会导致问题:
System.out.println("arr[" + i + "][" + j + "]: " + (arr[i][j] = rs.getString(j + 1).toUpperCase()));
应改为:
arr[i][j] = rs.getString(j + 1).toUpperCase();
System.out.println("arr[" + i + "][" + j + "]: " + arr[i][j]);
System.out.println
不应包含除控制台有用信息之外的任何内容 - 绝对不是变量分配!
编辑:
另外,如果要再次迭代结果集(第二次循环),请务必重置结果集:
resultSet.first();
否则光标将在结尾处,您将看不到任何结果。