JDBCResultset
- ResultSet是用於封裝DQL查詢語句返回的結果
- 有游標機制
//定義sql
String sql = "select * from stu";
//獲取statement對象
Statement stmt = conn.createStatement();
//執行sql
ResultSet resultSet = stmt.executeQuery(sql);
//處理結果
//遍歷resultSet中所有數據
//如果有.next(),就處理,直到.next()返回false代表沒數據了
while (resultSet.next()) {
//獲取數據 getXXX int id = resultSet.getInt("id");
String name = resultSet.getString("name");
double money = resultSet.getDouble("money");
System.out.println(id+" "+ name+ " "+money);
System.out.println("====================");
}
//釋放資源
resultSet.close();
stmt.close();
conn.close();
//定義sql
String sql = "select * from stu";
//獲取statement對象
Statement stmt = conn.createStatement();
//執行sql
ResultSet resultSet = stmt.executeQuery(sql);
//創建ArrayList
List<Account> list = new ArrayList<>();
//處理結果
while (resultSet.next()) {
//獲取數據 getXXX int id = resultSet.getInt("id");
String name = resultSet.getString("name");
double money = resultSet.getDouble("money");
//封裝結果到對象中
Account account = new Account(id, name, money);
//把對象存到list中
list.add(account);
}
System.out.println(list);
//釋放資源
resultSet.close();
stmt.close();
conn.close();