java - 如何动态添加参数值到JdbcTemplate查询?

标签 java file-io spring-jdbc

我需要查询数据库,但该查询的参数值需要来自文件,这是代码...

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/

相关文章:

java - JOptionPane.showInputDialog() 是否返回不同的字符串

file-io - 将 clojure 映射/数组写入文件并读取它们

java - 使用 MySQL 的 SimpleJdbcInsert

spring-boot - 使用 JdbcTemplate 将记录插入数据库时​​出错

java - 使用 Spring JdbcTemplate 进行多个数据库操作

java - 在 GUI 中切换颜色

java - JacocoReport 配置的 Gradle 6.0 弃用警告

java - 在 Java 中调用多个方法

c - 另一个令人头疼的winapi文件权限C。为什么我的方法不起作用?

python - 在Python中查找扩展名为.txt的目录中的所有文件