使用步骤: 1、游标向下移动一行 2、判断是否有数据 3、获取数据
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class JDBCDemo7 {
public static void main(String[] args) throws Exception {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db3?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC", "root", "sa123");
String sql = "select *from account1";
Statement statement = conn.createStatement();
ResultSet rs = statement.executeQuery(sql);
while(rs.next()){
int id = rs.getInt(1);
String name = rs.getString("name");
double balance = rs.getDouble(3);
System.out.println(id+"------"+name+"------"+balance);
}
System.out.println(rs);
rs.close();
statement.close();
conn.close();
}
}
|