Database Reference
In-Depth Information
Retrieving and Updating a Node
Once nodes have been added to the database, you need a way to retrieve and modify them. Listing 12-6 shows the
process for finding a node by its node id value and updating it.
Listing 12-6. Retrieving and Updating a Node
//class
public void updateNode() {
// Make sure Neo4j Driver is registered
Class.forName("org.neo4j.jdbc.Driver");
// Connect
Connection conn = DriverManager.getConnection("jdbc:neo4j://localhost:7474/");
Map<String, Object> params = map("1", "Greg", "2", "Greg", "3", "Jordan");
String query= " MATCH (user:User {name:{1}}) " +
" SET user.firstname={2}, user.lastname={3} " +
" RETURN user ";
final PreparedStatement statement = conn.prepareStatement(query);
for (Map.Entry<String, Object> entry : params.entrySet()) {
int index = Integer.parseInt(entry.getKey());
statement.setObject(index, entry.getValue());
}
final ResultSet result = statement.executeQuery();
// result contains user data, which can be mapped to bean
}
Removing a Node
Once a node's graph id has been set and saved into the database, it becomes eligible to be removed when necessary.
To remove a node, set a variable as a node object instance and then call the delete method for the node (Listing 12-7).
You cannot delete any node that is currently set as the start point or end point of any relationship. You must
remove the relationship before you can delete the node.
Note
 
 
Search WWH ::




Custom Search