View rowData in ResultSet

insert image description here

 

The return values ​​of the previous dml statements are all ints indicating the number of rows that have been changed,
so Select is to display the data

The result of the SELECT query
is traversed through this ResultSet,
and then the data is read line by line through the next method.
Similar to an iterator (definitely not an iterator),
it also contains the obtained data elements.
It is equivalent to this not only containing elements, but also iterating its own elements.
Specifically, you look at the bottom

However, it cannot be output directly. We need to obtain data column by column (Get data type (column number)
)
 

package yuan.hsp.JDBC;

import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.mysql.jdbc.Driver;
@SuppressWarnings("all")
public class 结果集 {
public static void main(String[] args) throws SQLException, ClassNotFoundException {
	Class.forName("com.mysql.jdbc.Driver");
	
	Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/db02", "root", "123456");
	
	Statement createStatement = connection.createStatement();
	
	String sql="select id,name,sex,borndate from actor";
	ResultSet executeQuery = createStatement.executeQuery(sql);//和之前的execute不同,executeQuery返回一个ResultSet
	//用while取出数据
	while (executeQuery.next()) {//没有数据返回false
		int id=executeQuery.getInt(1);//获取该行第一列数据
		String name=executeQuery.getString(2);//第二列
		String sex=executeQuery.getString(3);//第三列
		Date date = executeQuery.getDate(4);//第四列
		System.out.println(id+name+sex+date);
	}
	//关闭
	createStatement.close();
	connection.close();
	
}
}

bottom layer

ResultSet is actually an interface
real type

insert image description here

 This yellow line table
is implemented by our data manufacturer. There is a rowData data structure
in ResultSet

insert image description here

There is also an elementData data type,
and the data is actually placed in the elementData array.

insert image description here Of course, all the stored ASCLL codes

insert image description here

 The ASCLL code corresponding to jack -106 97 99 107
stores each corresponding character in a byte array

Guess you like

Origin blog.csdn.net/ok060/article/details/131360556