Young87

SmartCat's Blog

So happy to code my life!

游戏开发交流QQ群号60398951

当前位置:首页 >跨站数据

使用JdbcTemplate/JDBC流式读取数据库

JDBC原生实现

注意MySql中

conn.prepareStatement("select * from table",
                      ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); 
// 这里因为MySQL驱动实现使用Integer.MIN_VALUE来判断是否使用流的方式
preparedStatement.setFetchSize(Integer.MIN_VALUE);
@Test
public void testCursor() throws ClassNotFoundException, SQLException {
    Class.forName("com.mysql.cj.jdbc.Driver");
    Connection connection = DriverManager
            .getConnection("jdbc:mysql://127.0.0.1:3306/db?useUnicode=true&characterEncoding=utf8&useSSL=false", 
                    "root", "root");
    PreparedStatement preparedStatement =
            connection.prepareStatement("select * from table",
                    ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
    preparedStatement.setFetchSize(Integer.MIN_VALUE);

    ResultSet resultSet = preparedStatement.executeQuery();

    while (resultSet.next()) {
        System.err.println(resultSet.getString("id"));
    }
    connection.close();
}复制代码

 JdbcTemplate使用

void query(PreparedStatementCreator psc, RowCallbackHandler rch) throws DataAccessException;复制代码

 

关键点在于参数PreparedStatementCreator与RowCallbackHandler,我们可以控制PreparedStatement的创建与ResultSet的提取,这样就简单了,具体代码实现如下:

MySQL

 

 

 

jdbcTemplate.query(con -> {
            PreparedStatement preparedStatement =
                    con.prepareStatement("select * from table",
                                         ResultSet.TYPE_FORWARD_ONLY, 
                                                   ResultSet.CONCUR_READ_ONLY);
            preparedStatement.setFetchSize(Integer.MIN_VALUE);
            preparedStatement.setFetchDirection(ResultSet.FETCH_FORWARD);
            return preparedStatement;
        }, rs -> {
            while (rs.next()) {
                System.err.println(resultSet.getString("id"));
            }
        });

手动控制提交 

ransactionSynchronizationManager.initSynchronization();
DataSource dataSource = ((JdbcTemplate)jdbcTemplate.getJdbcOperations()).getDataSource();
Connection connection = DataSourceUtils.getConnection(dataSource);
try {
    connection.setAutoCommit(false);
    //需要操作数据库的两个insert,或者提供回调给业务开发人员
    connection.commit();
} catch (SQLException e) {
} finally {
    try {
        TransactionSynchronizationManager.clearSynchronization();
    } catch (IllegalStateException e) {
    }
    try {
        connection.setAutoCommit(true);
    } catch (SQLException e) {
    }
}

除特别声明,本站所有文章均为原创,如需转载请以超级链接形式注明出处:SmartCat's Blog

上一篇: 基于NB-IoT的智慧路灯监控系统(NB-IoT专栏—实战篇5:手机应用开发)

下一篇: 【华为云技术分享】【一统江湖的大前端(9)】TensorFlow.js 开箱即用的深度学习工具

精华推荐