Orientdb 简明教程

OrientDB - Java Interface

与 RDBMS 类似,OrientDB 支持 JDBC。为此,我们首先需要为 JDBC 编程配置环境。以下是创建应用程序和数据库之间连接的过程。

Similar to RDBMS, OrientDB supports JDBC. For this, first we need to configure the environment for JDBC programming. Following is the procedure to create a connection between your application and database.

首先,我们需要下载 JDBC 驱动程序。访问以下链接 https://code.google.com/archive/p/orient/downloads 下载 OrientDB-JDBC。

First, we need to download the JDBC Driver. Visit the following link https://code.google.com/archive/p/orient/downloads to download OrientDB-JDBC.

以下是实现 OrientDB-jdbc 连接的基本五个步骤。

Following are the basic five steps to achieve OrientDB-jdbc connectivity.

  1. Load JDBC driver

  2. Create Connection

  3. Create statement

  4. Execute statement

  5. Close connection

Example

尝试以下示例来理解 OrientDB-JDBC 连接。让我们考虑一张包含以下字段及其类型的员工表。

Try the following example to understand OrientDB-JDBC connectivity. Let us consider we have an employee table which contains the following fields and its types.

Sr.No.

Field Name

Type

1

Id

Integer

2

Name

String

3

Salary

Integer

4

Join date

Date

您可以通过执行以下命令创建架构(表)。

You can create a Schema (table) by executing the following commands.

CREATE DATABASE PLOCAL:/opt/orientdb/databases/testdb
CREATE CLASS Employee
CREATE PROPERTY Customer.id integer
CREATE PROPERTY Customer.name String
CREATE PROPERTY Customer.salary integer
CREATE PROPERTY Customer.join_date date

执行所有命令后,将生成包含以下字段的 Employee 表,即带 ID 的雇员姓名、年龄和入职日期字段。

After executing all the commands, you will get the Employee table with the following fields, employee name with id, age, and join_date fields.

将以下代码保存在 OrientJdbcDemo.java 文件中。

Save the following code into OrientJdbcDemo.java file.

import com.orientechnologies.common.log.OLogManager;
import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;

import java.io.File;
import java.sql.DriverManager;
import java.util.Properties;

import static com.orientechnologies.orient.jdbc.OrientDbCreationHelper.createSchemaDB;
import static com.orientechnologies.orient.jdbc.OrientDbCreationHelper.loadDB;
import static java.lang.Class.forName;

public abstract class OrientJdbcDemo {

   protected OrientJdbcConnection conn;

   public static void main(String ar[]){

      //load Driver
      forName(OrientJdbcDriver.class.getName());
      String dbUrl = "memory:testdb";
      ODatabaseDocumentTx db = new ODatabaseDocumentTx(dbUrl);
      String username = "admin";
      String password = "admin";
      createSchemaDB(db);
      loadDB(db, 20);
      dbtx.create();

      //Create Connection
      Properties info = new Properties();
      info.put("user", username);
      info.put("password", password);
      conn = (OrientJdbcConnection) DriverManager.getConnection("jdbc:orient:" + dbUrl, info);

      //create and execute statement
      Statement stmt = conn.createStatement();
      int updated = stmt.executeUpdate("INSERT into emplyoee
         (intKey, text, salary, date) values ('001','satish','25000','"
          + date.toString() + "')");

      int updated = stmt.executeUpdate("INSERT into emplyoee
         (intKey, text, salary, date) values ('002','krishna','25000','"
         + date.toString() + "')");

      System.out.println("Records successfully inserted");

      //Close Connection
      if (conn != null && !conn.isClosed())
         conn.close();
   }
}

以下命令用于编译上述程序。

The following command is used to compile the above program.

$ javac –classpath:.:orientdb-jdbc-1.0-SNAPSHOT.jar OrientJdbcDemo.java
$ java –classpath:.:orientdb-jdbc-1.0-SNAPSHOT.jar OrientJdbcDemo

如果上述命令执行成功,您将获得以下输出内容。

If the above command is executed successfully, you will get the following output.

Records Successfully Inserted