2017  Kodetalk | Feedback | Privacy Policy | Terms | About
userimage

How to execute a neo4j create statement using JDBC preparedstatement in java.

I am trying to execute a neo4j query CREATE (Test {NEODATA :?});

Here my ? is the dynamic parameter which i will pass.

But every time i am executing i am getting exception. Anybody have any solution:

Below i am mentioning my code snipet:

while (uCursor.hasNext()) {
    String userId = document.getString("_id");
    String insert = "CREATE (Test {NEODATA :?});";
    try (PreparedStatement preparedStatement = connection.prepareStatement(insert)){
        preparedStatement.setString(1, userId);
        preparedStatement.executeQuery();
    } catch (SQLException e) {
        e.printStackTrace();
    }
   
    connection.commit();
    System.out.println("done");
}

userimage

I have updated your code to below code and try the below process which i have tested and working fine:


while (uCursor.hasNext()) {
    String userId = document.getString("_id");
    String insert = "CREATE (Test {NEODATA :{1}});";
    try (PreparedStatement preparedStatement = connection.prepareStatement(insert)){
        preparedStatement.setString(1, userId);
        preparedStatement.executeQuery();
    } catch (SQLException e) {
        e.printStackTrace();
    }
   
    connection.commit();
    System.out.println("done");
}


Basically the dynamic parameter we provide in case of neo4j JDBC driver bit different than usual RDBMS process.

Please read below link for more information about the dynamically passing values to neo4j statements. Click Here.


Hope this will help you a lot.


Answer is