ResultSet基本使用和ResultSet遍历结果集
ResultSet:结果集对象,封装查询结果
next():游标向下移动一行
getXxx(参数):获取数据
Xxx:代表数据类型 如:int getInt(),String getString()
参数:
int:代表列的编号,从1开始 如getString(1)
String:代表列名称 如 getDouble("balance");
package cn.lhy.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class JdbcDemo04 {
public static void main(String[] args) throws Exception {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/root", "root", "root");
String sql = "select * from account";
Statement sttm = conn.createStatement();
ResultSet resultSet = sttm.executeQuery(sql);
resultSet.next();
String string = resultSet.getString("name");
System.out.println(string);
conn.close();
sttm.close();
}
}
package cn.lhy.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class JdbcDemo04 {
public static void main(String[] args) throws Exception {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/root", "root", "root");
String sql = "select * from account";
Statement sttm = conn.createStatement();
ResultSet resultSet = sttm.executeQuery(sql);
while(resultSet.next()){
int anInt = resultSet.getInt(1);
String name = resultSet.getString("name");
double aDouble = resultSet.getDouble(3);
System.out.println(anInt+"====="+name+"====="+aDouble);
}
conn.close();
sttm.close();
}
}