有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Neo4jjdbc hashmap或json在准备好的语句中

另一个问题是neo4j jdbc驱动程序。根据Rest api文档,有一种通过传递映射来创建节点的方法:

Map<String, Object> props = new HashMap<String, Object>();
props.put( "name", "Andres" );
props.put( "position", "Developer" );

Map<String, Object> params = new HashMap<String, Object>();
params.put( "props", props );
String query = "CREATE ({props})";
engine.execute( query, params );

我必须创建一个有很多属性的节点,我得到了一个json。有没有办法创建这样的节点

JSONObject jsonObject = (JSONObject) JSONSerializer.toJSON(json);
Map<String, Object> map = (Map<String, Object>) JSONObject.toBean(jsonObject, Map.class);
...
connection = dataSource.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("CREATE (n{1}");
preparedStatement.setObject(1, map);
preparedStatement.executeQuery();

谢谢大家


共 (1) 个答案

  1. # 1 楼答案

    具有多个属性的节点:-

     Map<String, Object> courseNodeInfo=new HashMap<String, Object>();
    
                        courseNodeInfo.put("name",courseId);
                        courseNodeInfo.put("createtime",createTime);
                        courseNodeInfo.put("title",title);
                        courseNodeInfo.put("status", "draft");
                        courseNodeInfo.put("coursetype", courseType);
                        courseNodeInfo.put("sectionexist",isSectionExist);
                        courseNodeInfo.put("coverimage", "coursecover.jpg");
    
        // TODO Auto-generated method stub
                Connection connect = null;
                int status = 00;
                try {
                    connect = graphdbConnect();
    
                    //String insert = "CREATE (n:Test {1})";
                    String query = "CREATE (n:" + nodeLabel +"{1}) RETURN n";
                    query=query.toLowerCase();
    
                    try (PreparedStatement preparedStatement = connect.prepareStatement(query)){
    
                        preparedStatement.setObject(1,courseNodeInfo);
                        System.out.println(query+"  -> query");
                        preparedStatement.executeQuery();
                        status = ServerStatusReport.OK();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
    
    
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }