簡體   English   中英

JDBC PreparedStatement查詢

[英]JDBC PreparedStatement Query

我正在使用Oracle數據庫進行JDBC。 我有以下方法:

// 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*/
    ...
}

如您所見,我正在嘗試重用已准備好的語句。 現在,我的問題是每次更改參數時都會創建一個准備好的語句?

我只是在method1中的所有處理結束后關閉語句。 我擔心如果每次更改參數時都會創建一個新語句(因為我沒有關閉所有參數),它可能會以非閉合語句結束。 我應該擔心嗎?

謝謝,
窗扇

java.sql.PreparedStatement旨在可重用。

設置新參數時,將覆蓋之前的參數,但不會創建新的Statement。

您還可以決定使用clearParameters()自行清除所有參數

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM