public Vector findAll() { con = JDBCconn.getConnection(); Vector rowData = new Vector(); try { String sql = "select * from employee order by id"; pstmt = con.prepareStatement(sql); rs = pstmt.executeQuery(); while(rs.next()) { Vector row =new Vector(); row.add(rs.getString(1)); row.add(rs.getString(2)); row.add(rs.getString(3)); rowData.add(row); } return rowData; } catch (Exception e) { // TODO: handle exception e.printStackTrace(); return null; }finally{ JDBCconn.closeConnection(); } } 该方法如何从rowdata中输出getString(1)
时间: 2024-03-05 16:53:25 浏览: 44
可以使用以下代码从rowData中输出第一个元素:
```java
Vector row = (Vector) rowData.get(index); // index为所需行号
String str = (String) row.get(0);
System.out.println(str);
```
其中,`(String) row.get(0)`表示获取当前行的第一个元素并强制转换为字符串类型。
相关问题
public Vector findAll() { con = JDBCconn.getConnection(); Vector rowData = new Vector(); try { String sql = "select * from employee order by id"; pstmt = con.prepareStatement(sql); rs = pstmt.executeQuery(); while(rs.next()) { Vector row =new Vector(); row.add(rs.getString(1)); row.add(rs.getString(2)); row.add(rs.getString(3)); rowData.add(row); } return rowData; } catch (Exception e) { // TODO: handle exception e.printStackTrace(); return null; }finally{ JDBCconn.closeConnection(); } } 该方法如何输出getString(1)
您可以使用以下代码从 `rowData` 中输出第一列的值:
```java
for (int i = 0; i < rowData.size(); i++) {
Vector row = (Vector) rowData.get(i);
String value = (String) row.get(0);
System.out.println(value);
}
```
其中,`Vector row = (Vector) rowData.get(i)` 表示获取第 i 行数据,`(String) row.get(0)` 表示获取当前行的第一个元素并强制转换为字符串类型,`System.out.println(value)` 表示输出该元素的值。
public Vector findAll() { con = JDBCconn.getConnection(); Vector rowData = new Vector(); try { String sql = "select * from users order by id"; pstmt = con.prepareStatement(sql); rs = pstmt.executeQuery(); while(rs.next()) { Vector row =new Vector(); row.add(rs.getString(1)); row.add(rs.getString(2)); row.add(rs.getString(3)); rowData.add(row); } return rowData; } catch (Exception e) { // TODO: handle exception e.printStackTrace(); return null; }finally{ JDBCconn.closeConnection(); } } 根据此代码如何修改为只找name
可以将 SQL 语句修改为 `select name from users order by id`,即只查询 `name` 字段。同时,Vector 的元素个数也应该修改为 1,修改后的代码如下:
```java
public Vector findAllNames() {
con = JDBCconn.getConnection();
Vector rowData = new Vector();
try {
String sql = "select name from users order by id";
pstmt = con.prepareStatement(sql);
rs = pstmt.executeQuery();
while(rs.next()) {
Vector row = new Vector();
row.add(rs.getString(1));
rowData.add(row);
}
return rowData;
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
JDBCconn.closeConnection();
}
}
```
阅读全文