在Java中的预编写语句中将参数传递到子查询的外部表
我在java中实现了一个子查询,它需要使用一个准备好的语句提供两个参数 在内部查询中传递参数是有效的,但随后参数不会传递给外部查询 在执行过程中,我在stacktrace上得到一个错误:语句关闭后不允许操作。因此,prepared语句返回错误的结果。问题:如何最好地将参数成功传递到外部表 下面是我的实现的示例代码在Java中的预编写语句中将参数传递到子查询的外部表,java,mysql,jdbc,Java,Mysql,Jdbc,我在java中实现了一个子查询,它需要使用一个准备好的语句提供两个参数 在内部查询中传递参数是有效的,但随后参数不会传递给外部查询 在执行过程中,我在stacktrace上得到一个错误:语句关闭后不允许操作。因此,prepared语句返回错误的结果。问题:如何最好地将参数成功传递到外部表 下面是我的实现的示例代码 pst = conn.prepareStatement("SELECT slot.time_slot_id,slot.`date`,slot.count FROM (SELE
pst = conn.prepareStatement("SELECT slot.time_slot_id,slot.`date`,slot.count FROM
(SELECT time_slot_id,ue.`date`,COUNT(ue.user_id) AS count FROM user_event ue
RIGHT JOIN `time_slot` t ON ue.time_slot_id = t.id
WHERE ue.status= 1 AND event_id=?
GROUP BY ue.`date`,time_slot_id
) AS slot WHERE slot.count >=? ");
pst.setInt(1,eventId);
//here is the parameter that is passed to the outer table
pst.setInt(2,count);
rs = pst.executeQuery();
下面是整个方法am的更新,如下所示
public static DefaultListModel Fetch(int eventId){
DBconnection.connect();
DefaultListModel fetchedSlots= new DefaultListModel();
try{
//
pst = conn.prepareStatement("SELECT slot.time_slot_id,slot.`date`,slot.count FROM \n" +
"(SELECT time_slot_id,ue.`date`,COUNT(ue.user_id) AS count FROM user_event ue \n" +
"RIGHT JOIN `time_slot` t ON ue.time_slot_id = t.id\n" +
"WHERE ue.status= 1 AND event_id=?\n" +
"GROUP BY ue.`date`,time_slot_id) AS slot\n" +
"WHERE slot.count >? ");
pst.setInt(1,eventId);
//here a function is invocked using eventId to get counts
pst.setInt(2,FetchCounts(eventId));
rs = pst.executeQuery();
while(rs.next()){
fetchedSlots.addElement(new ModelSlot(rs.getInt("time_slot_id"),rs.getString("date")));
}
DBconnection.CloseConnection();
}catch (SQLException ex){
System.out.println(ex.getMessage());
}
return fetchedSlots;
}
我假设错误发生在如下情况:
Open statement
...
Use statement
...
Close statement <---------- You can't use your statement after close it
...
Use same statement again <---------- You are here, your statement is close
事实上,这会打开一个新连接,然后在最后关闭它,因此当您执行查询时,它
rs=pst.executeQuery()
,连接已关闭,这会导致此问题,因此不要在方法中关闭连接FetchCounts(t eventId)代码>在试验之后,我将第二个参数连接到准备好的语句中,而不是之前,它起作用了。
如下所示
pst = conn.prepareStatement("SELECT
slot.time_slot_id,slot.`date`,slot.count FROM \n" +
"(SELECT time_slot_id,ue.`date`,COUNT(ue.user_id) AS count FROM user_event ue \n" +
"RIGHT JOIN `time_slot` t ON ue.time_slot_id = t.id\n" +
"WHERE ue.status= 1 AND event_id=?\n" +
"GROUP BY ue.`date`,time_slot_id) AS slot\n" +
"WHERE slot.count >" + FetchCounts(eventId));
pst.setInt(1,eventId);
rs = pst.executeQuery();
如上所述,调用的方法FetchCounts(eventId)
的结果直接传递到准备好的语句中子查询的外部表。这看起来像是PHP和Java代码的奇怪混合。请发布实际的Java代码。您提到的错误绝不是因为不允许使用子查询参数。请发布完整的代码,包括你得到这个错误的那一行。你能告诉我们这个错误和一些其他的代码你是如何打开和关闭你的连接的吗?堆栈跟踪中的错误是:语句关闭后不允许操作。@Ford还有其他错误吗?因为这(无操作…)不是导致代码出现问题的原因,可能是您看到的日志不正确file@YCF_L.i有一个用于管理数据库连接的类,其中包含用于打开和关闭ie DBconnection.connect()的方法,用于打开连接实现和DBconnection.CloseConnection();谢谢结束。如上面的更新所示,..no@Ford it not connection检查语句是否为close或opentry it,并让我知道结果@Ford please我已经研究并做了几次试验。我稍后会回来更新。再次感谢。
pst = conn.prepareStatement("SELECT
slot.time_slot_id,slot.`date`,slot.count FROM \n" +
"(SELECT time_slot_id,ue.`date`,COUNT(ue.user_id) AS count FROM user_event ue \n" +
"RIGHT JOIN `time_slot` t ON ue.time_slot_id = t.id\n" +
"WHERE ue.status= 1 AND event_id=?\n" +
"GROUP BY ue.`date`,time_slot_id) AS slot\n" +
"WHERE slot.count >" + FetchCounts(eventId));
pst.setInt(1,eventId);
rs = pst.executeQuery();