2013-10-08 223 views
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... 

請告知我應該怎麼做,因爲我被困這裏看電腦... 喬治。

+0

這個有你的循環:'columnsArrayDB1 [J]。[ k] = rowData;'但你永遠不會改變'rowData'。如果你想讓數據不同,你必須在你的循環內部做一些將'rowData'設置爲新的東西。但是,我不熟悉在Java中使用SQL,因此我無法立即幫助您處理該部分。 – ajb

+2

OK,如果你移動'resTablesData1.next'和'resTablesData1.getString'內循環? – ajb

+1

也檢查[this](http://docs.oracle.com/javase/tutorial/jdbc/basics/retrieving.html) –

回答

1

所提到的錯誤是,你永遠不會重新讀取數據庫,並且您的陣列中的每個指標設置爲僅根據一列讀取相同的值,這個代碼在這裏rowData

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

你需要循環在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++; 
} 

但是,你好得多創建一個對象,禾uld表示該行的數據。然後,對於接收到的每一行,您都可以創建該對象的新實例,然後將其添加到這些對象的列表中。那麼你也只需要查詢一次數據庫。

事情是這樣的,例如:

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......)); 
} 

然後從列表中

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