简体   繁体   English

MySQL 准备好的语句不返回结果 java

[英]MySQL prepared statement not returning results java

i have a simple stored procedure in mysql and am trying to call it from jdbc.我在 mysql 中有一个简单的存储过程,我试图从 jdbc 调用它。 i keep getting an empty result set.我不断得到一个空的结果集。 i've searched and tried several solutions on similar threads to no avail.我已经在类似的线程上搜索并尝试了几种解决方案,但均无济于事。 note that table.quantity is not a fixed decimal, which is why i'm using double/BigDecimal.请注意,table.quantity 不是固定小数,这就是我使用 double/BigDecimal 的原因。

the relevant columns in my table are:我表中的相关列是:

quantity   DOUBLE UNSIGNED
orderTime  TIMESTAMP

and here is the stored procedure:这是存储过程:

CREATE DEFINER=`root`@`localhost` PROCEDURE `getTotal`( IN pSince TIMESTAMP, OUT pVolume DOUBLE UNSIGNED )
BEGIN
    SET pVolume = ( SELECT SUM( quantity ) FROM db.table WHERE orderTime > pSince );
END

i've also tried it like this:我也试过这样:

BEGIN
    SELECT SUM( quantity ) INTO pVolume FROM db.table WHERE orderTime > pSince;
END

neither seems to work when i call it like this:当我这样称呼它时,似乎都不起作用:

CallableStatement lCall = getConnection().prepareCall( "{call getTotal(?,?)}" );
lCall.setTimestamp( 1, new Timestamp( System.currentTimeMillis() - 600000 ));
lCall.registerOutParameter( 2, Types.DOUBLE );
lCall.execute();
ResultSet lResults = lCall.getResultSet();
lResults.next();
return lResults.getBigDecimal( 1 );

this returns an empty result set.这将返回一个空结果集。

adding lResults.beforeFirst() throws a null pointer exception because the lResults.rowData is null.添加lResults.beforeFirst()会引发 null 指针异常,因为lResults.rowData是 null。 i've also tried lResults.getBigDecimal( "pVolume" ) with the same result.我也试过lResults.getBigDecimal( "pVolume" )得到相同的结果。

i've also tried:我也试过:

ResultSet lResults = lCall.executeQuery();
lResults.next();
return lResults.getBigDecimal( 1 );

which throws a SQLException on lResults.next() because it says the call was an UPDATE and, therefore, doesn't have any results.它会在lResults.next()上引发 SQLException,因为它说调用是 UPDATE,因此没有任何结果。

i suspect that i'm missing something in my stored procedure.我怀疑我的存储过程中遗漏了一些东西。 any ideas??有任何想法吗?? oh, and i'm logging in as dev, not root.哦,我正在以 dev 身份登录,而不是 root。 i checked the permissions and i can execute.我检查了权限,我可以执行。

Results from CallableStatement are retrieved directly through its (own) declared methods: CallableStatement的结果通过其(自己的)声明的方法直接检索:

lCall.execute();
return lCall.getBigDecimal( 2 );
// do not forget to close the statement - try-with-resource

no need to call getResultSet() (from Statement ).无需调用getResultSet() (来自Statement )。

Documentation : 文档

The type of all OUT parameters must be registered prior to executing the stored procedure;所有 OUT 参数的类型必须在执行存储过程之前注册; their values are retrieved after execution via the get methods provided here .它们的值在执行后通过此处提供的 get 方法检索

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM