PreparedStatement.executeQuery如何工作? [英] How does PreparedStatement.executeQuery work?

查看:234
本文介绍了PreparedStatement.executeQuery如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PreparedStatement.executeQuery的工作原理?是否从数据库中获取结果,然后遍历它们?还是获取第一个结果,并在record.next上继续sql查询到下一行?

How PreparedStatement.executeQuery works? Does it fetch results from database and I loop through them? Or it fetches the first result, and on record.next continues with sql query to the next row?

ResultSet record = statement.executeQuery("select * from users");
    while (record.next()) {
    //do something
}

谢谢

推荐答案

来自文档:

声明

public ResultSet executeQuery(String sql) throws SQLException

执行给定的SQL语句,该语句返回单个ResultSet 对象.

Executes the given SQL statement, which returns a single ResultSet object.

ResultSet

代表数据库结果集的数据表,通常是 通过执行查询数据库的语句生成.

A table of data representing a database result set, which is usually generated by executing a statement that queries the database.

public boolean next() throws SQLException

将光标从当前位置向下移动一行.结果集 光标最初位于第一行之前;首次致电 接下来的方法使第一行成为当前行;第二次通话 将第二行设置为当前行,依此类推.

Moves the cursor down one row from its current position. A ResultSet cursor is initially positioned before the first row; the first call to the method next makes the first row the current row; the second call makes the second row the current row, and so on.

如果当前行打开了输入流,则调用 next方法将隐式关闭它. ResultSet对象的警告 读取新行时,链被清除.

If an input stream is open for the current row, a call to the method next will implicitly close it. A ResultSet object's warning chain is cleared when a new row is read.


作为一种表示形式,这意味着该语句将执行一次,并且在对其执行结果进行迭代时对其进行迭代.


As a representation this means the statement is executed once and when iterated is iterated over the result of that execution.

但是如何处理来自数据库的结果实际上取决于实现.为了进行对比,我将引用两个数据库MSSQL,MYSQL.

But how the result from database is handled depends really on the implementation. To make a contrast I will refer two databases MSSQL, MYSQL.

可以在此处找到MSSQL驱动程序的文档,其中详细说明了结果的处理方式. :

The documentation of MSSQL driver that comments exactly how the results are handled you can find here:

结果集有两种类型:客户端和服务器端.

There are two types of result sets: client-side and server-side.

当结果可以容纳在客户端结果集中时,使用客户端结果集 客户端进程内存.这些结果提供了最快的性能 并由SQL Server的Microsoft JDBC驱动程序在其 整个数据库.这些结果集不附加其他内容 通过产生创建服务器端的开销在数据库上加载 游标.但是,这些类型的结果集不可更新.

Client-side result sets are used when the results can fit in the client process memory. These results provide the fastest performance and are read by the Microsoft JDBC Driver for SQL Server in their entirety from the database. These result sets do not impose additional load on the database by incurring the overhead of creating server-side cursors. However, these types of result sets are not updatable.

当结果不适合服务器端结果集时,可以使用服务器端结果集 客户端进程内存或结果集是可更新的.和 对于这种类型的结果集,JDBC驱动程序将创建服务器端游标 并在用户滚动时透明地获取结果集的行 通过它.

Server-side result sets can be used when the results do not fit in the client process memory or when the result set is to be updatable. With this type of result set, the JDBC driver creates a server-side cursor and fetches rows of the result set transparently as the user scrolls through it.

MySQL

您可以在此处:

默认情况下,完全检索ResultSet并将其存储在内存中. 在大多数情况下,这是最有效的操作方式,并且由于 MySQL网络协议的设计更易于实现.如果 您正在使用具有大量行的ResultSet或 较大的值,并且无法在JVM中为 需要内存,您可以告诉驱动程序将结果流回 一次一行.

By default, ResultSets are completely retrieved and stored in memory. In most cases this is the most efficient way to operate, and due to the design of the MySQL network protocol is easier to implement. If you are working with ResultSets that have a large number of rows or large values, and cannot allocate heap space in your JVM for the memory required, you can tell the driver to stream the results back one row at a time.

这篇关于PreparedStatement.executeQuery如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆