Data persistence operation of the Java class library with NEO4J JDBC Packaging

Data persistence operation of the Java class library with NEO4J JDBC Packaging Neo4J is a high -performance graph database, and one of the ways to use Neo4J is to operate through the JDBC connection.This article will introduce you to how to use the JAVA library's data for Java libraries with the JDBC Packaging of NEO4J. 1. Introduction to NEO4J JDBC Packaging First, you need to add Neo4J's JDBC Packaging to the library to your Java project.The following dependencies can be added to project management tools such as Maven or Gradle: <dependency> <groupId>org.neo4j</groupId> <artifactId>neo4j-jdbc-driver</artifactId> <version>4.2.11</version> </dependency> 2. Establish database connection In your Java code, use the following statements to establish a connection with the NEO4J database: import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class Neo4jConnector { private static final String NEO4J_URL = "jdbc:neo4j:bolt://localhost"; private static final String NEO4J_USERNAME = "neo4j"; private static final String NEO4J_PASSWORD = "password"; public Connection createConnection() throws SQLException { return DriverManager.getConnection(NEO4J_URL, NEO4J_USERNAME, NEO4J_PASSWORD); } } Please note that you need to replace the `LocalHost` with the host name or IP address of your NEO4J database, replace the user name of the` neo4j` with your database, and replace the code of `Password`. 3. Execute data persistent operation After creating a database connection, you can use the JDBC API to perform data persistent operations.The following is a simple example. Insert a node called "Person" and a relationship called "Knows" into the NEO4J database: import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class Neo4jDataPersistence { private Connection connection; private PreparedStatement statement; public Neo4jDataPersistence(Connection connection) { this.connection = connection; } public void createPerson(String name) throws SQLException { String query = "CREATE (p:Person {name: ?})"; statement = connection.prepareStatement(query); statement.setString(1, name); statement.executeUpdate(); } public void createKnowsRelationship(String sourceName, String targetName) throws SQLException { String query = "MATCH (source:Person {name: ?}), (target:Person {name: ?}) " + "CREATE (source)-[:KNOWS]->(target)"; statement = connection.prepareStatement(query); statement.setString(1, sourceName); statement.setString(2, targetName); statement.executeUpdate(); } public void close() throws SQLException { if (statement != null) { statement.close(); } if (connection != null) { connection.close(); } } } In the above code, the database connection and pre -processing statement variables are first defined, and then the connection is passed to the instance of the connection to the `neo4JDataPersistencence` class by the constructor.Next, the `Createperson` method creates a node called" Person ", and uses the relationship between the two nodes with the method of` CreateknowsRelationship`.Finally, turn off the sentences and connections by calling the `Close` method. 4. Example code Below is an example code using the above class: import java.sql.Connection; import java.sql.SQLException; public class Main { public static void main(String[] args) { Neo4jConnector connector = new Neo4jConnector(); try { Connection connection = connector.createConnection(); Neo4jDataPersistence persistence = new Neo4jDataPersistence(connection); persistence.createPerson("John"); persistence.createPerson("Alice"); persistence.createKnowsRelationship("John", "Alice"); persistence.close(); } catch (SQLException e) { e.printStackTrace(); } } } The above code first creates an instance of `neo4jconnector`, and build a connection with the database by calling the` CreateConnection` method.Then create an instance of the `neo4jdataPersistence, and use its method to create nodes and relationships.Finally, turn off the connection by calling the `Close` method. Through this Java code example, you can start using Neo4J's JDBC Packaging for the durable operation of the Java library.You can modify and extend these examples according to your needs to adapt to more complex application scenarios.