0

I am connection to my DB and get data and column.

I want to print them:

public class JDBC1 {

static Vector<String> columns = new Vector<String>();
Vector<Vector<String>> data = new Vector<Vector<String>>();
Connection con;
Statement statement;
ResultSet result;

public Vector<String> getColumns() throws SQLException {

    con = DriverManager.getConnection(...);
    statement = con.createStatement();
    result = statement.executeQuery("Select * from table");

    int c = result.getMetaData().getColumnCount();
    for (int i = 1; i <= c; i++) {
        columns.add(result.getMetaData().getColumnName(i));
    }
    return columns;
}

public Vector<Vector<String>> getData() throws SQLException {
    con = DriverManager.getConnection(...);
    statement = con.createStatement();
    result = statement.executeQuery("Select * from table");
    int c = result.getMetaData().getColumnCount();

    while (result.next()) {
        Vector<String> newRow = new Vector<String>(c);

        for (int i = 1; i <= c; i++) {
            newRow.add(result.getString(i));
        }
        data.add(newRow);
    }
    return data;
 }

public static void main(String[] args) {
    System.out.println(String.valueOf(getColumns()));  // Error
    System.out.println(String.valueOf(getgetData()));  // Error
}
}

How println vectors? ....................................................................................................................

4

1 回答 1

0

您必须使用带有适当索引的 get() 方法从 Vector 中提取每个元素,如下所示:

String str = vector.get(i);
System.out.print(str);

或使用 for 循环遍历向量:

for (String str : vector)
{
    System.out.println(str);
}

在您的情况下,您需要像这样进行迭代:

for (String str : getColumns())
{
    System.out.println(str);
}
于 2013-09-04T19:42:22.523 回答