我有下面的方法,它将接受两个参数-


  final String userId- Primary Key for the database
  
  final Collection<String> attributeNames- list of column names that I want to retrieve


下面是代码

public Map<String, String> getAttributes(final String userId, final Collection<String> attributeNames) {

     //Below line doesn't works out the way I wanted
     String query="SELECT" +attributeNames.toString()+ ", * from test where id = "+userId+ ";";
     ResultSet result = CassandraDatastaxConnection.getInstance().getSession().execute(query);
     for (Row rows: result){
         System.out.println(rows.getString("key"));
     }

    return attributes;
}


让我们举个例子,userId as 40

示例attributeNames看起来像这样-

[account, behavior, segmentation]


现在,我需要生成与输入相对应的SQL。因此,对于上面的示例,sql应该看起来像这样-

SELECT account, behavior, segmentation from test where id = "40";


如何从上述输入生成这样的SQL?谢谢您的帮助。

最佳答案

您可以使用类似attributeNames.toString().substring(1, attributeNames.toString().length()-1)的名称

关于java - 如何从方法的输入生成sql,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16127245/

10-10 19:09