我连接到我的数据库并获取数据和列。
我想打印它们:
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
}
}
println
向量如何?
.................................................. .................................................. ................
答案 0 :(得分: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);
}