我對Java並不是很熟悉,我有一個項目的期限很短。讓我們談談點。我有以下類:創建一個像矩陣一樣的二維數組
public static void getAllDataDB1() // Catching all the data from "bank1" database
{
try
{
MetaData1 = connection1.getMetaData();
catalogs1 = MetaData1.getCatalogs();
String[] types = {"TABLE"};
resTables1 = MetaData1.getTables(null,null,"%",types);
while (resTables1.next())
{
db1TableName = resTables1.getString("TABLE_NAME");
System.out.println("|=====================================================|");
System.out.println("| CATCHING " +dbName1+"."+db1TableName+ " DATA |");
System.out.println("|=====================================================|");
System.out.println("|=====================================================|");
System.out.println("| THE QUERY IS IN PROGRESS. PLEASE WAIT... |");
System.out.println("|=====================================================|");
resTablesStr1 = statement1.executeQuery("SELECT * FROM "+db1TableName+";");
resColumns1 = resTablesStr1.getMetaData();
db1ColNum = resColumns1.getColumnCount();
db1FirstColName = resColumns1.getColumnName(1);
for (int i=1; i<=db1ColNum; i++)
{
db1FirstColName = resColumns1.getColumnName(1);
db1ColName = resColumns1.getColumnName(i);
System.out.println("| COLUMN: " +db1ColName+ " |");
System.out.println("\n");
resTablesData1 = statement1.executeQuery("SELECT "+db1ColName+" FROM "+db1TableName+" GROUP BY "+db1FirstColName+";");
while (resTablesData1.next())
{
db1RowNum++;
}
columnsArrayDB1 = new String[db1RowNum][db1ColNum];
resTablesData1 = statement1.executeQuery("SELECT "+db1ColName+" FROM "+db1TableName+" GROUP BY "+db1FirstColName+";");
resTablesData1.next();
String rowData = resTablesData1.getString(db1ColName);
for (int j=0; j<db1RowNum; j++)
{
System.out.println();
for (int k=0; k<db1ColNum; k++)
{
columnsArrayDB1[j][k] = rowData;
System.out.println("|-----------------------------------------------------|");
System.out.print("| "+columnsArrayDB1[j][k]+" |"+"\t");
System.out.println("|-----------------------------------------------------|");
//columnsArrayDB1[j][x+1]=columnsArrayDB1[j][x];
}
}
/*while (resTablesData1.next())
{
String rowData = resTablesData1.getString(db1ColName);
for (int j=0; j<db1RowNum; j++)
{
System.out.println(rowData);
//columnsArrayDB1[j] = rowData;
//System.out.print(columnsArrayDB1[j]+" ");
}
System.out.println(rowData);
}*/
}
}
}
catch (SQLException e)
{
System.err.println("Connection Failed! Check output console " + e.getMessage());
System.exit(0);
}
}
我想從一個特定的數據庫捕獲所有數據,並將它們存儲到一個數組作爲基質的風格。我收到的時候編譯和運行上面的代碼,結果是類似的東西:
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
| COLUMN: Gender |
female female female female female female female female female female female female female female female female female female female female female female
female female female female female female female female female female female female female female female female female female female female female female
female female female female female female female female female female female female female female female female female female female female female female
female female female female female female female female female female female female female female female female female female female female female female
female female female female female female female female female female female female female female female female female female female female female female
female female female female female female female female female female female female female female female female female female female female female female
female female female female female female female female female female female female female female female female female female female female female female
female female female female female female female female female female female female female female female female female female female female female female
female female female female female female female female female female female female female female female female female female female female female female
female female female female female female female female female female female female female female female female female female female female female female
female female female female female female female female female female female female female female female female female female female female female female
female female female female female female female female female female female female female female female female female female female female female female
female female female female female female female female female female female female female female female female female female female female female female
female female female female female female female female female female female female female female female female female female female female female female
female female female female female female female female female female female female female female female female female female female female female female
female female female female female female female female female female female female female female female female female female female female female female
female female female female female female female female female female female female female female female female female female female female female female
female female female female female female female female female female female female female female female female female female female female female female
female female female female female female female female female female female female female female female female female female female female female female
female female female female female female female female female female female female female female female female female female female female female female
正如你知道我想要這樣的結果:
1 female Mrs.
2 male Mr.
etc... etc...
請告知我應該怎麼做,因爲我被困這裏看電腦... 喬治。
這個有你的循環:'columnsArrayDB1 [J]。[ k] = rowData;'但你永遠不會改變'rowData'。如果你想讓數據不同,你必須在你的循環內部做一些將'rowData'設置爲新的東西。但是,我不熟悉在Java中使用SQL,因此我無法立即幫助您處理該部分。 – ajb
OK,如果你移動'resTablesData1.next'和'resTablesData1.getString'內循環? – ajb
也檢查[this](http://docs.oracle.com/javase/tutorial/jdbc/basics/retrieving.html) –