Tuesday 15 July 2014

java - Copy consecutive (MySQL) resultsets into single multidimensional array -



java - Copy consecutive (MySQL) resultsets into single multidimensional array -

i re-create several resultset i'm obtaining several tables mysql db single multidimesnsional array. using loop run through array names of tables in db.

i've selected info , obtained resultset , these various resultsets corresponding table intend on copying single multidimensional array. @ moment when effort printing out multidimensional array seems empty.

in case wondering number of rows , columns of multidimensional array known beforehand. in code below number of rows 12 , number of columns 18.

below edited code:

// pass array holding existing tables selection of info each array element string[][] arr = null, current = null; int = 0; int j = 0; for(int k = 0; k <= existingtablesindbasarr.length; k++) { string sql = "select * " + existingtablesindbasarr[k]; seek ( //connection conn = dbutil.getconnection(dbtype.mysql); statement stmt = conn.createstatement(); resultset rs = stmt.executequery(sql); ){ system.out.println("\nrs 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 array:"); seek { seek { 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 before increment is: " + i); system.out.println("j outside while before increment is: " + j); while(rs.next()) { system.out.println("\ni within while before increment is: " + i); system.out.println("j within while before increment is: " + j); for(j = 0; j < columnsize; j++) { arr[i][j] = rs.getstring(j + 1).touppercase(); system.out.println("arr[" + + "][" + j + "]: " + arr[i][j]); } system.out.println("\n"); i++; system.out.println("\ni within while after increment is: " + i); system.out.println("j within while after increment is: " + j + "\n"); if(i == arr.length) { system.out.println("\n---\nenqrstablemanager - == arr.length"); system.out.println("\n---\nenqrstablemanager - arr.length > 0\n---\narray containing entries tables follows: "); (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]); } } } } grab (exception e) { system.out.println("\nerror in generating 2d array specific table resultset"); } } }

i appreciate assistance/suggestions.

the way assign values array causing problem:

system.out.println("arr[" + + "][" + j + "]: " + (arr[i][j] = rs.getstring(j + 1).touppercase()));

should changed to:

arr[i][j] = rs.getstring(j + 1).touppercase(); system.out.println("arr[" + + "][" + j + "]: " + arr[i][j]);

system.out.println should not contain other useful message console - not variable assignments!

edit:

also, sure reset result set if want iterate on 1 time again (for sec loop):

resultset.first();

otherwise cursor @ end , not see results.

java mysql

No comments:

Post a Comment