Using Java to Operate OrientDB

Using Java to operate OrientDB requires the following steps: 1. Add Maven dependencies for OrientDB: Add the following dependencies in the pom.xml file. <dependencies> <dependency> <groupId>com.orientechnologies</groupId> <artifactId>orientdb-client</artifactId> <version>3.2.2</version> </dependency> <dependency> <groupId>com.orientechnologies</groupId> <artifactId>orientdb-core</artifactId> <version>3.2.2</version> </dependency> </dependencies> 2. Connect to the OrientDB database: Create an OrientDB client object in Java code and use it to connect to the specified database. import com.orientechnologies.orient.core.db.OrientDB; import com.orientechnologies.orient.core.db.OrientDBConfig; import com.orientechnologies.orient.core.db.document.ODatabaseDocument; String dbName = "your_database_name"; String dbUsername = "your_username"; String dbPassword = "your_password"; OrientDB orientDB = new OrientDB("remote:localhost", OrientDBConfig.defaultConfig()); ODatabaseDocument db = orientDB.open(dbName, dbUsername, dbPassword); 3. Data insertion: Use the API operation provided by OrientDB to insert data. import com.orientechnologies.orient.core.record.OVertex; //Create a new vertex object OVertex vertex = db.newVertex("Person"); vertex.setProperty("firstName", "John"); vertex.setProperty("lastName", "Doe"); //Save vertices to database db.save(vertex); 4. Data modification: Use the API provided by OrientDB to modify data. import com.orientechnologies.orient.core.record.ODocument; //Query specific documents ODocument doc = db.query(new OSQLSynchQuery<>("SELECT FROM Person WHERE firstName = 'John'")).get(0); //Modifying Document Properties doc.field("lastName", "Smith"); //Save modifications to database db.save(doc); 5. Data query: Use the API provided by OrientDB to perform query operations. import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery; //Execute query and obtain result set List<ODocument> results = db.query(new OSQLSynchQuery<>("SELECT FROM Person WHERE firstName = 'John'")); //Traverse the result set and output query results for (ODocument doc : results) { System.out.println(doc.toJSON()); } 6. Data deletion: Use the API provided by OrientDB to delete data. import com.orientechnologies.orient.core.id.ORecordId; //Delete a document through its ID ORecordId recordId = new ORecordId("#12:0"); db.delete(recordId); 7. Close database connection: After using the database, make sure to close the database connection. db.close(); orientDB.close(); This is a complete Java code example that implements the functions of inserting, modifying, querying, and deleting data. You can modify and expand according to your own needs. import com.orientechnologies.orient.core.db.OrientDB; import com.orientechnologies.orient.core.db.OrientDBConfig; import com.orientechnologies.orient.core.db.document.ODatabaseDocument; import com.orientechnologies.orient.core.id.ORecordId; import com.orientechnologies.orient.core.record.OVertex; import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery; import java.util.List; public class OrientDBExample { public static void main(String[] args) { String dbName = "your_database_name"; String dbUsername = "your_username"; String dbPassword = "your_password"; OrientDB orientDB = new OrientDB("remote:localhost", OrientDBConfig.defaultConfig()); ODatabaseDocument db = orientDB.open(dbName, dbUsername, dbPassword); //Insert Data OVertex vertex = db.newVertex("Person"); vertex.setProperty("firstName", "John"); vertex.setProperty("lastName", "Doe"); db.save(vertex); //Modify data List<ODocument> results = db.query(new OSQLSynchQuery<>("SELECT FROM Person WHERE firstName = 'John'")); if (!results.isEmpty()) { ODocument doc = results.get(0); doc.field("lastName", "Smith"); db.save(doc); } //Query data List<ODocument> queryResults = db.query(new OSQLSynchQuery<>("SELECT FROM Person WHERE firstName = 'John'")); for (ODocument doc : queryResults) { System.out.println(doc.toJSON()); } //Delete data ORecordId recordId = new ORecordId("#12:0"); db.delete(recordId); db.close(); orientDB.close(); } } This example demonstrates how to use Java operations on OrientDB for data insertion, modification, query, and deletion. You can make changes and extensions according to your own needs.