所以我想知道哪种方法更好,我将从一个文件中读取每一行,并将其存储到我的数据库中。我将有很多准备好的语句,因为我将执行lof选择(检查条目是否存在以从表中获取id值),或者在不存在时插入。这种sql将执行5次,就像我将插入5个不同的表一样。
因此,我不确定是否可以创建许多准备好的语句,因为我将留下大量打开的连接,但是大多数语句将在我从文件中读取时使用。或者因为我有很多前置语句,在while循环中创建它们,并将它们放在自己的try资源块中,这样我就不会留下太多打开的连接了?
try {PreparedStatement stmt1 = connection.PrepareStatement(Sql);
PreparedStatement stmt2 = connection.PrepareStatement(Sql);
PreparedStatement stmt3 = connection.PrepareStatement(Sql);
PreparedStatement stmt4 = connection.PrepareStatement(Sql);
etc . . . )
while read line from file {
//etc
}
}或者这是优先考虑的:
while read line from file {
try {PreparedStatement stmt1 = connection.PrepareStatement(Sql);
PreparedStatement stmt2 = connection.PrepareStatement(Sql))
// stmt1.setint(1, blah)
// stmt1.execute()
//etc
}
try {PreparedStatement stmt3 = connection.PrepareStatement(Sql);
PreparedStatement stmt4 = connection.PrepareStatement(Sql))
// stmt1.setint(2, blah)
// stmt1.execute()
//etc
}
}发布于 2020-05-24 17:59:07
在这两种方式中,object.
发布于 2020-05-24 17:53:33
我会用第一个变体。第二个打开并关闭每个行的PreparedStatement,这将比一直打开它们要慢,特别是当您有很多行要处理的时候。准备声明本身也不是一种廉价的行动。
https://stackoverflow.com/questions/61989926
复制相似问题