Database Reference
In-Depth Information
Creating a Node
The maintenance of nodes is set in motion with the creation process, as shown in Listing 12-5. Creating a node begins
with setting up a connection to the database. Next, the properties are put into a Map , and then the Node can be saved to
the database.
Listing 12-5. Creating a Node
import static org.neo4j.helpers.collection.MapUtil.map;
// other imports
// class
public void createUserNode(){
// Make sure Neo4j Driver is registered
Class.forName("org.neo4j.jdbc.Driver");
// Connect
Connection conn = DriverManager.getConnection("jdbc:neo4j://localhost:7474/");
// create map
HashMap<String, Object> userMap=new HashMap<String, Object>();
userMap.put("name", "Greg");
userMap.put("business","Graph Story");
Map<String, Object> params = map("1", userMap);
String query= " CREATE (user:User {1}) ";
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();
}
The map keys (parameter index) that are passed into a prepared statement should use numbers and begin with
“{1}” and so on.
Note
 
 
Search WWH ::




Custom Search