0

私は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...

ここで立ち往生しているので、どうすればよいか教えてください... ジョージ。

4

2 に答える 2

1

申し訳ありませんが、あなたのコードは多くの点で間違っています。ですから、あなたがやろうとしていることについて、まずこれを正しく理解させてください。基本的にデータベース全体をダンプしたい、つまり、すべてのテーブルのすべての行からデータを出力したいと考えています。そうですか?それがあなたのやりたいことであると仮定すると、それがどのように機能するかは次のとおりです(プログラムをテストしていないため、コンパイルエラーが発生する可能性があります):

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()) // Loop for all the tables
        {   
            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); // Dont need this

            while (resTablesStr1.next ()) // Loop for each row of data
            {
              System.out.println("\n ------ Row -----\n");
              for (int i=1; i<=db1ColNum; i++) // Loop for each column in the row
              {
                db1ColName = resColumns1.getColumnName(i);
                System.out.println("| COLUMN: " +db1ColName+ " |");
                System.out.println("\n");

                String rowData = resTablesData1.getString(db1ColName);
                System.out.println(); 
                System.out.println("|-----------------------------------------------------|");
                System.out.print("| "+rowData+" |"+"\t");
                System.out.println("|-----------------------------------------------------|");
             }
           }
        } 
    }
    catch (SQLException e) 
    { 
        System.err.println("Connection Failed! Check output console " + e.getMessage());
        System.exit(0);
    }
}
于 2013-10-08T23:34:14.697 に答える
1

言及したように、あなたのエラーはrowData、データベースから再読み取りすることはなく、配列内のすべてのインデックスは、次のコードによって 1 つの列からのみ読み取られる同じ値に設定されていることです。

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("|-----------------------------------------------------|");
    }
}

行ごとにループしてから、各列からデータを取得する必要があります(が a であるresTablesData1と仮定します)resTablesData1ResultSet

int j = 0;
while(resTablesData1.next())
{
    ResultSetMetaData rsmd = resTablesData1.getMetaData();
    int colCount = rsmd.getColumnCount();
    System.out.println();
    for (int k=0; k<colCount ; k++)
    {
        String colName = rsmd.getColumnName(i);
        Object o = resTablesData1.getObject(colName);
        columnsArrayDB1[j][k] = o.toString();
        System.out.println("|-----------------------------------------------------|");
        System.out.print("| "+columnsArrayDB1[j][k]+" |"+"\t");
        System.out.println("|-----------------------------------------------------|");
    }
    j++;
}

ただし、行のデータを表すオブジェクトを作成する方がはるかに優れています。次に、受信した行ごとに、このオブジェクトの新しいインスタンスを作成し、これをこれらのオブジェクトのリストに追加できます。その後、データベースに 1 回だけクエリを実行する必要があります。

たとえば、次のようなものです。

public class RowDataObject
{
     public String col1;
     public String col2;
     // Etc
     public RowDataObject(String aCol1, String aCol2 /*.....etc */ )
     {
         col1 = aCol1;
         col2 = aCol2;  //...etc
     }
}

次に、データを読み取る

List<RowDataObject> allRows = new ArrayList<RowDataObject>();

ResultSet rs = //Your Query
while (rs.next())
{
     String c1 = rs.getString("A Column Name or Index");
     String c2 = rs.getString("A Column second Name or Index");
     //...etc
     allRows.add(new RowDataObject(c1,c2......));
}

次に、リストから印刷操作を行います

于 2013-10-08T23:20:28.497 に答える