在Java代码中将字符串参数插入密码查询时出错 [英] Error in inserting a string parameter to a cypher query in a java code

查看:90
本文介绍了在Java代码中将字符串参数插入密码查询时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Java中向密码查询插入字符串参数.下面是我使用的代码,我有一个名为"piyumi"的人员节点,并且我想与活动节点建立关系.活动节点的名称为"walking".执行代码时,我会得到http状态代码400.有人可以帮助我修改密码查询,以便我可以无误地插入字符串变量s.

I want to insert a string parameter to a cypher query in Java. Below is the code I used and I have a person node named 'piyumi' and I want to make relationship with an activity node. The name of the activity node is 'walking'. When I execute the code I get http status code 400. Can anyone help me to modify the cypher query so that I can insert the string variable s without error.

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import javax.ws.rs.core.MediaType;

public class NeoAPIClass {
    private final String baseUri = "http://localhost:7474/db/data/cypher";
    private final String user = "neo4j";
    private final String password = "12345";

    public static void main(String args[]) throws JSONException {
            NeoAPIClass n=new NeoAPIClass();
            n.runQuery();
    }

    void runQuery() throws JSONException {
        Client client = Client.create();
        client.addFilter(new HTTPBasicAuthFilter(user, password));
        WebResource cypherResource = client.resource(baseUri);


        String s="walking";
        JSONObject obj = new JSONObject();
        obj.put("query", "Match(piyumi : Person{name:\"Piyumi\"}) create (piyumi)-[:doing{timestamp:4789}]->(n:activity) WHERE n.name=s");
        String st = obj.toString();

        ClientResponse cypherResponse = cypherResource.accept(MediaType.APPLICATION_JSON)
                .type(MediaType.APPLICATION_JSON_TYPE).entity(st).post(ClientResponse.class);

        System.out.println("Output from Server .... "+ cypherResponse.getStatus());


    }

}

推荐答案

不可能将createwhere组合在一起.您需要先匹配人员和活动,然后再建立关系.

It is impossible to combine the create and where. You need first match person and activity, and then create a relationship.

并且还建议简化请求的单独部分中的传输参数( https://neo4j.com/docs/rest-docs/current/#rest-api-use-parameters ):

And also recommend to simplify the transfer parameters in a separate part of the request (https://neo4j.com/docs/rest-docs/current/#rest-api-use-parameters):

JSONObject request = new JSONObject();
JSONObject params =  new JSONObject();

String query =  "MATCH (P:Person { name:{personName} }) \n";
query = query + "MATCH (A:activity { name:{activityName} }) \n";
query = query + "CREATE (P)-[rel:doing { timestamp:{activityTimestamp} }]->(A) \n";
query = query + "RETURN P, A, rel";

request.put("query", query);

params.put("personName", "Piyumi");
params.put("activityName", "walking");
params.put("activityTimestamp", 4789);

request.put("params", params);

ClientResponse cypherResponse = cypherResource.accept(MediaType.APPLICATION_JSON)
            .type(MediaType.APPLICATION_JSON_TYPE)
            .entity(request.toString())
            .post(ClientResponse.class);

System.out.println("Response: " + cypherResponse.getEntity(String.class));

这篇关于在Java代码中将字符串参数插入密码查询时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆