I'm working on JDBC with Oracle database. I have the below methods:
// Method1
public void method1(){
Connection connection=ConnectionFactory.getRemoteConnection();
selectSQL = "select * tablename where num>=? and num<=?";
PreparedStatement userStatement=connection.prepareStatement(selectSQL);
userStatement.setFetchSize(500);
int i=0;
int startRow=0;
int endRow=50;
do{
// Reusing the statement
fetchRecords(userStatement,startRow,endRow);
startRow=startRow+50;
endRow=endRow+50;
i++;
if(i==50) endOfRows=true;
}while(!endOfRows);
userStatement.close();
}
// Method2
public List<String> fetchRecords(PreparedStatement userStatement,int startRow,int endRow){
userStatement.setInt(1, startRow);
userStatement.setInt(2, endRow);
resultSet = userStatement.executeQuery();
/*Some logic*/
...
}
As you can see, I'm trying to reuse a prepared statement. Now, my question is while a prepared statement be created everytime I change parameters?
I'm closing the statement only after the end of all the processing in method1. I'm worried if a new statement is created everytime I change the parameters (since I'm not closing all of them), it may end up in un-closed statement. Should I be worried?
Thank you,
Sash