gpt4 book ai didi

java - JDBC中Connection.getWarnings()和Statement.getWarnings()的区别?

转载 作者:行者123 更新时间:2023-12-02 01:38:21 25 4
gpt4 key购买 nike

我不明白 Connection.getWarning() 和 Statement.getWarnings() 之间有什么区别?Connection.getWarning 包含所有语句的警告和 Statement .getWarnings 包含单个语句的警告是主要区别吗?

最佳答案

Connection.getWarnings 返回连接级警告,而 Statement.getWarnings 返回语句级警告。还有 ResultSet.getWarnings,它(令人惊讶)返回结果集级别的警告。换句话说,如果在 Connection 上调用的方法产生警告,则该警告会添加到连接上、对于在语句上调用 Statement 的方法以及对于结果集上 ResultSet 的方法。

一般来说,Statement 产生的警告不应报告在 Connection 上,并且不应报告 ResultSet 上的警告声明。例如,参见Statement.getWarnings()其中说:

Note: If you are processing a ResultSet object, any warnings associated with reads on that ResultSet object will be chained on it rather than on the Statement object that produced it.

ResultSet.getWarnings()其中说:

Note: This warning chain only covers warnings caused by ResultSet methods. Any warning caused by Statement methods (such as reading OUT parameters) will be chained on the Statement object.

JDBC 规范对于警告不是很详细,它只说( JDBC 4.3 ,第 8.2 SQLWarning 节):

When a method generates an SQLWarning object, the caller is not informed that a data access warning has occurred. The method getWarnings must be called on the appropriate object to retrieve the SQLWarning object. However, the DataTruncation sub-class of SQLWarning may be thrown in some circumstances, see Section 8.3 “DataTruncation” on page 8-46 for more details.

If multiple data access warnings occur, they are chained to the first one and can be retrieved by recursively calling the SQLWarning.getNextWarning method. If there are no more warnings in the chain, getNextWarning returns null.

Subsequent SQLWarning objects continue to be added to the chain until the next statement is executed or, in the case of a ResultSet object, when the cursor is repositioned, at which point all SQLWarning objects in the chain are removed.

以及第 8.3 节数据截断:

When data truncation occurs on a read from the data source, a SQLWarning is reported.

以及第 15.1.1 节中的ResultSet 类型:

If the driver does not support the type supplied to the methods createStatement, prepareStatement, or prepareCall, it generates an SQLWarning on the Connection object that is creating the statement.

类似的文本可以在 15.1.2 ResultSet 并发性 和 15.1.3.1 确定 ResultSet 可保存性中找到。

有时 JDBC API 文档会明确提及警告(例如 Connection.setClientInfo​(String, String)ResultSet.next() )。

虽然 JDBC 没有指定,但驱动程序通常也会在 Connection 上的连接过程中报告警告。

关于java - JDBC中Connection.getWarnings()和Statement.getWarnings()的区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54866326/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com