当运行通过java与mySQL数据库时,SQL可以查询提供相同参数return不同数据吗?
Can SQL Query which is provided the same parameters return different data when run through java vs mySQL database?
我有两个查询:(一个是计算记录总和,另一个是选择所有记录)
Query 1:
select sum(tb_nominal) from tb_sec_tran where tb_account_id = 112 and tb_security_id = 234
and trunc(tb_execution_time) < trunc(to_date('31-12-2017','dd-mm-yyyy');
Query 2: select tb_nominal from tb_sec_tran where tb_account_id = 112 and tb_security_id =
234 and trunc(tb_execution_time) < trunc(to_date('31-12-2017','dd-mm-yyyy');
数据库中的内容。
tb_nominal | tb_execution_time
520 | 08-MAR-17
486 | 04-JAN-18
520 | 01-JAN-18
-520 | 31-DEC-17
案例1:我运行这两个数据库查询(sql开发者)。对于第一个查询,我得到 520.0 作为输出。对于第二个查询,我在 tb_nominal=520.0.
的输出中得到 1 条记录
案例2:在java平台上,我实现了以下代码:
PreparedStatement pstmt = null;
ResultSet rs = null;
Double sum = null;
StringBuffer sql = new StringBuffer(" select sum(tb_nominal) from tb_sec_tran where
tb_account_id = ? and tb_security_id = ? and trunc(tb_execution_time) < trunc(?) ");
pstmt = m_Connection.prepareStatement(sql.toString());
pstmt.setLong(1, spekuVO.getContent().getAccountId());
pstmt.setLong(2, spekuVO.getContent().getInstrumentId());
pstmt.setDate(3, toDate.getSQLDatum());
rs = pstmt.executeQuery();
if (rs.next()) {
sum = rs.getDouble(1);
}
问题: 所以当我 运行 代码,而不是 520.0 标称值时,我得到的总和为 0.0,这与返回的值不同数据库。同样,如果我通过删除 sum 词来更改 sql 变量以使其类似于查询 2,我将获得两条记录,如我所附的图像所示。
我完全被 st运行ge 的行为搞糊涂了。 JAVA 8 相比 JAVA 6 的处理有没有与此相关的变化?
如果此行为存在任何原因,请建议我。
不,Java 不会神奇地给出错误的结果。
您的 Java 查询不包含您在其他查询中使用的 to_date
部分。两个不同的查询,两个不同的结果。
我有两个查询:(一个是计算记录总和,另一个是选择所有记录)
Query 1:
select sum(tb_nominal) from tb_sec_tran where tb_account_id = 112 and tb_security_id = 234
and trunc(tb_execution_time) < trunc(to_date('31-12-2017','dd-mm-yyyy');
Query 2: select tb_nominal from tb_sec_tran where tb_account_id = 112 and tb_security_id =
234 and trunc(tb_execution_time) < trunc(to_date('31-12-2017','dd-mm-yyyy');
数据库中的内容。
tb_nominal | tb_execution_time
520 | 08-MAR-17
486 | 04-JAN-18
520 | 01-JAN-18
-520 | 31-DEC-17
案例1:我运行这两个数据库查询(sql开发者)。对于第一个查询,我得到 520.0 作为输出。对于第二个查询,我在 tb_nominal=520.0.
的输出中得到 1 条记录案例2:在java平台上,我实现了以下代码:
PreparedStatement pstmt = null;
ResultSet rs = null;
Double sum = null;
StringBuffer sql = new StringBuffer(" select sum(tb_nominal) from tb_sec_tran where
tb_account_id = ? and tb_security_id = ? and trunc(tb_execution_time) < trunc(?) ");
pstmt = m_Connection.prepareStatement(sql.toString());
pstmt.setLong(1, spekuVO.getContent().getAccountId());
pstmt.setLong(2, spekuVO.getContent().getInstrumentId());
pstmt.setDate(3, toDate.getSQLDatum());
rs = pstmt.executeQuery();
if (rs.next()) {
sum = rs.getDouble(1);
}
问题: 所以当我 运行 代码,而不是 520.0 标称值时,我得到的总和为 0.0,这与返回的值不同数据库。同样,如果我通过删除 sum 词来更改 sql 变量以使其类似于查询 2,我将获得两条记录,如我所附的图像所示。
我完全被 st运行ge 的行为搞糊涂了。 JAVA 8 相比 JAVA 6 的处理有没有与此相关的变化? 如果此行为存在任何原因,请建议我。
不,Java 不会神奇地给出错误的结果。
您的 Java 查询不包含您在其他查询中使用的 to_date
部分。两个不同的查询,两个不同的结果。