创建一个像矩阵一样的二维数组

时间:2013-10-08 22:45:34

标签: java arrays

我对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...

请告知我该怎么办,因为我被困在这里看着电脑...... 乔治。

2 个答案:

答案 0 :(得分: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++;
}

但是,你最好创建一个代表行数据的Object。然后,对于收到的每一行,您可以创建此Object的新实例,然后将其添加到这些对象的List中。然后你也只需要查询一次数据库。

例如:

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 :(得分: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);
    }
}