我需要查询数据库,但该查询的参数值需要来自文件,这是代码...
BufferedReader reader = new BufferedReader(new FileReader("C:/DBMigrations/empIDs.txt"));
String line = null;
String query = "select name, address from Employee where id in (";
while ((line = reader.readLine()) != null) {
// the value of line needs to be plugged into query inside the in clause
}
我正在使用 Spring 的 SimpleJdbcTemplate。
预先感谢您的帮助。
最佳答案
您可以简单地使用一种方法来从文件中生成串联 id 的 token 。
String query = "select name, address from Employee where id in ("+geneateEmployeeIds()+")";
private String geneateEmployeeIds(){
// read your file here and convert it to string<br>
// do id concatenation here
return ids;
}
您可以了解如何获取文件 here 的字符串内容
关于java - 如何动态添加参数值到JdbcTemplate查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21802323/