私は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...
ここで立ち往生しているので、どうすればよいか教えてください... ジョージ。