使用JdbcTemplate进行动态查询

问题描述

**StringJoiner joiner2 = new StringJoiner(",", "(", ")");**
while (mapIterator.hasNext()) {

 //change in value1 as
value = "\"" + (String) mapEntry.getValue() +  "\"";
 value1 = joiner2.add(value);
 myvalue = joiner2.add("?");

}

 //change  insertQueries  as with value as (? ,?)
insertQueries += " VALUES "+ myvalue+"" ;


//get value1 as  ("abc", "xyz")

//update query as
int row = jdbcTemplate.update(insertQueries, value1);

解决方法

我有一个关于使用JdbcTemplate进行动态查询的问题。

我的代码如下:

String insertQueries = "INSERT INTO " + tablename;

StringJoiner joiner = new StringJoiner(",");
StringJoiner joiner1 = new StringJoiner(",");
StringJoiner joiner2 = new StringJoiner(",");

while (mapIterator.hasNext()) {
        Map.Entry mapEntry = (Map.Entry) mapIterator.next();
        key = joiner.add((String) mapEntry.getKey()).toString();
        // value = joiner1.add("\"" + (String) mapEntry.getValue() + "\"").toString();
        value = joiner1.add("\"" + (String) mapEntry.getValue() + "\"").toString();
        value1 = joiner2.add("?").toString();
}

insertQueries += " (" + key + ")";
insertQueries += " VALUES ("+value1+")" ;

int row = jdbcTemplate.update(insertQueries,value);

现在,我的问题是,我想要与插入查询中的自动生成问号相同数量的“值”。

现在,值变量考虑为一个字符串,因此,如果我有2个或更多问号,则在值变量中只有一个用逗号分隔的完整字符串,因此它不起作用。

见下面我的查询:

INSERT INTO tablename (fname,lname) VALUES ("abc,xyz") ;

我想要如下:

INSERT INTO tablename (fname,lname) VALUES ("abc","xyz") ;

猜你在找的技术问答相关文章

如何检查配对的蓝牙设备是打印机还是扫描仪(Android)
是否允许实体正文进行HTTP DELETE请求?
如何将ZipInputStream转换为InputStream?
java.util.logging Java 8中的变量
PowerMockito.doReturn返回null
Java中的RESTful调用
Swing / Java:如何正确使用getText和setText字符串
特殊字符和重音字符
Android Studio中的ndk.dir错误
错误“找不到主类”