neo4j連接java目前主要有嵌入式、jdbc和rest api。
jdbc:需要使用到的lib包:neo4j-jdbc-2.0.1-SNAPSHOT-jar-with-dependencies.jar
Connection con = DriverManager .getConnection("jdbc:neo4j://localhost:7474/"); //創建連接 String query = "start n = node({1}) return n.name"; PreparedStatement stmt = null; //采用預編譯,和關系數據庫不一樣的是,參數需要使用{1},{2},而不是? ResultSet rs = null; try { stmt = con.prepareStatement(query); stmt.setInt(1, 14); rs = stmt.executeQuery(); System.out.println(rs.getRow()); while (rs.next()) { System.out.println("a " + rs.getString("n.name")); } } catch (Exception e) { throw e; } finally { if (null != rs) { rs.close(); } if (null != stmt) { stmt.close(); } }
jdbc連接的是服務式neo4j。
目前cypher語言提供的shorestPath方法僅僅支持計算兩個節點間經過的節點數最小的路徑,不支持關系之間的權重計算,如果需要計算權重的最短路徑,則需要使用內嵌式,或者是服務式的rest API。