1、处理流程:
2、标记位
单批次返回时,如果数据集合很大,就需要多次返回,中间采用标记位 SERVER_MORE_RESULTS_EXISTS
从命令COM_STMT_EXECUTE切换到COM_STMT_FETCH需要设置标记位:SERVER_STATUS_CURSOR_EXISTS
fetch命令字,最后一批次数据发送,需要设置标记位:SERVER_STATUS_LAST_ROW_SENT
3、验证代码:
package com.sql;
import java.sql.*;
public class ConnMySQLMain {
public static void main(String[] args) throws SQLException {
// TODO Auto-generated method stub
System.out.println("Hello Java");
testReadTimeout();
}
public static void testReadTimeout() throws SQLException {
System.out.println(DriverManager.getDrivers());
Connection connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:15307/kundb1?" +
"continueBatchOnError=false" +
"&useServerPrepStmts=true&alwaysSendSetIsolation=false&useLocalSessionState=true&" +
"zeroDateTimeBehavior=CONVERT_TO_NULL&serverTimezone=UTC&useCursorFetch=true", "vt_app", "123");
//https://jdbc.postgresql.org/documentation/head/query.html
connection.setAutoCommit(false); //NOTE 为了设置fetchSize,必须设置为false
String sql = "select * from Persons order by PersonID;";
PreparedStatement pstmt;
try {
// 决定的 Prepared类型,后面自己编程BinaryResult
pstmt = (PreparedStatement)connection.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
// pstmt.setFetchSize(Integer.MIN_VALUE); //setFetchSize支持后, pstmt.setFetchSize(15);
pstmt.setFetchSize(15); //<-------此处
System.out.println("ps.getQueryTimeout():" + pstmt.getQueryTimeout());
System.out.println("ps.getFetchSize():" + pstmt.getFetchSize());
System.out.println("ps.getFetchDirection():" + pstmt.getFetchDirection());
System.out.println("ps.getMaxFieldSize():" + pstmt.getMaxFieldSize());
ResultSet rs = pstmt.executeQuery();
System.out.println("============================");
while (rs.next()) {
System.out.print(rs.getObject(1) + " --- "+rs.getObject(2) + " --- "+rs.getObject(3) + " ---- "+rs.getObject(4) + " --- "+rs.getObject(5));
System.out.println("");
}
System.out.println("============================");
rs.close();
pstmt.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
//close resources
}
}
}



