繁体   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