我正在使用 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 中的所有处理结束后才关闭语句。 我担心如果每次更改参数时都会创建一个新语句(因为我没有关闭所有参数),它可能会以未关闭的语句结束。 我应该担心吗?
谢谢你,
腰带
最佳答案
java.sql.PreparedStatement 被设计为可重用。
当你设置新的参数时,你会覆盖之前的参数,但你不会创建一个新的语句。
您也可以使用 clearParameters() 自行决定清除所有参数
关于java - JDBC PreparedStatement 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43063712/