Result set
package com.itheima.jdbc.jdbc;
import org.junit.Test;
import java.sql.*;
//快速入門
public class JDBCDemo5_Resultset {
? ?@Test
? ?public void testDDL() throws SQLException {
? ? ? ? ? ?//1.注冊驅(qū)動
? ? ? ? ? // Class.forName("com.mysql.cj.jdbc.Driver");
? ? ? ? ? ?//2.獲取鏈接
? ? ? ? ? ?String url = "jdbc:mysql://localhost:3306/db1?useSSL=false";
? ? ? ? ? ?String username="root";
? ? ? ? ? ?String password="123456";
? ? ? ? ? ?Connection conn= DriverManager.getConnection(url, username, password);
? ? ? ? ? ?//3.定義語句
? ? ? ? ? ?String sql ="select * from account";
? ? ? ? ? ?//4.獲取statement對象
? ? ? ?Statement stmt = conn.createStatement();
? ? ? ? ? ?//5.執(zhí)行sql
? ? ? ?ResultSet rs =stmt.executeQuery(sql);
? ? ? ?//6.處理結(jié)果,遍歷rs中的所有數(shù)據(jù)
? ? ? ?//6.1 光標(biāo)向下移動一行,并且判斷當(dāng)前行是否有數(shù)據(jù)
? ? ? ?while(rs.next()){
? ? ? ?//6.2 獲取數(shù)據(jù)
? ? ? ? ? ?int id = rs.getInt(1);
? ? ? ? ? ?String name =rs.getString(2);
? ? ? ? ? ?double money =rs.getDouble(3);
? ? ? ? ? ?System.out.println(id);
? ? ? ? ? ?System.out.println(name);
? ? ? ? ? ?System.out.print(money);
? ? ? ? ? ?System.out.println("------------------------");
? ? ? ?}
? ? ? ?//7.釋放資源
? ? ? ?rs.close();
? ? ? ?stmt.close();
? ? ? ?conn.close();
? ?}
? ?}