JDBC PreparedStatement Query

I am working on JDBC with an Oracle database. I have the following 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 am trying to reuse a prepared statement. Now, my question is that every time I change the parameters a prepared statement is created?

I only close the statement after all processing in method1 has finished. I am worried if a new statement is created every time I change the parameters (since I am not closing them) it might end up in an unblocked statement. Should I be worried?

Thanks,
Sash

+3


source to share


1 answer


java.sql.PreparedStatement is intended to be reusable.

When you set new parameters, you overwrite the previous ones, but you will not create a new expression.



You can also clear all parameters yourself using clearParameters ()

+3


source







All Articles