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

如前所述,您的错误是您永远不会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然后从每一列获取数据(假设resTablesData1是 a ResultSet

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

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

例如这样的事情:

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 回答
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 回答