java.io.IOException: Attempted read on closed stream
原因是 ResultSet 数据读取完成之前已经把 ClickHousePreparedStatement 关闭。
public ResultSet QueryResultSet(Connection connection, String sql, String[] params) {
ResultSet rst=null;
ClickHousePreparedStatement pst=null;
if (connection==null) {
System.out.println("connection is empty");
System.exit(-1);
}
try {
pst= (ClickHousePreparedStatement) connection.prepareStatement(sql);
if (params!=null)
for (int i = 0; i < params.length; i++) {
pst.setObject(i+1,params[i]);
}
rst = pst.executeQuery();
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally{
close(pst,connection);
}
return rst;
}
需要调整代码为
public ResultSet QueryResultSet(Connection connection,ClickHousePreparedStatement pst, String sql, String[] params) {
ResultSet rst=null;
if (connection==null) {
System.out.println("connection is empty");
System.exit(-1);
}
try {
pst= (ClickHousePreparedStatement) connection.prepareStatement(sql);
if (params!=null)
for (int i = 0; i < params.length; i++) {
pst.setObject(i+1,params[i]);
}
rst = pst.executeQuery();
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally{
}
return rst;
}
然后调用该方法后 用
Connection conn1 =null;
ClickHousePreparedStatement pst=null;
ResultSet resultsll =null;
try
{
}catch
{
}
finally
{
ClickHouseDaoUtils.closeClickhouse(resultsll,pst,conn1);
}
文章讨论了一个在使用Java进行数据库操作时遇到的问题,即在尝试读取ResultSet时,由于提前关闭了ClickHousePreparedStatement导致了IOException。解决方案是调整代码,确保在ResultSet处理完毕后再关闭PreparedStatement和Connection。示例代码展示了如何修改方法以及正确的资源关闭顺序。
960

被折叠的 条评论
为什么被折叠?



