我正在使用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*/
...
}
您可以看到,我正在尝试重用准备好的语句。 现在,我的问题是每次更改参数时是否会创建一个准备好的语句?
我只有在方法1中所有处理结束后才关闭语句。 我担心如果每次更改参数时都会创建一个新语句(因为我没有关闭所有语句),可能会出现未关闭的语句。 我应该担心吗?
谢谢,
Sash
fetchRecords(userStatement,); startRow=startRow+50; endRow=endRow+50;
? - Scary Wombatstatement
时,在fetchRecords
内部使用userStatement
。 - BackSlashPreparedStatement
,你正在使用不同的参数多次使用它。这没有问题,并且不会生成额外的语句。同时,你也可以查看这个主题,可能会对你有所帮助:https://dev59.com/OnE95IYBdhLWcg3wE52C - Arnaud