Skip to content

AergoLite﹢Java

Bernardo Ramos edited this page Dec 1, 2020 · 11 revisions

The AergoLite JDBC Driver is based on the SQLite JDBC Driver

The jar file can be downloaded from here

Put it on the same folder as your java/class files.

Using

Compile and run your java application with the commands:

On Linux and Mac:

javac -classpath ".:aergolite-jdbc-3.27.2.jar" <your_app.java>
java -classpath ".:aergolite-jdbc-3.27.2.jar" <your_app>

On Windows:

javac -classpath ".;aergolite-jdbc-3.27.2.jar" <your_app.java>
java -classpath ".;aergolite-jdbc-3.27.2.jar" <your_app>

Example code

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Node2 {

  public static void main(String[] args) {
    Connection connection = null;

    try {

      // create a database connection
      String admin_pubkey = "Am...";
      String uri = "file:data.db?blockchain=on&discovery=127.0.0.1:1234&password=test&admin=" + admin_pubkey;
      connection = DriverManager.getConnection("jdbc:sqlite:" + uri);

      // print the status
      System.out.println("the local database is open");

      Statement statement = connection.createStatement();
      ResultSet rs;
      String result;

      // check if the db connection is using AergoLite
      rs = statement.executeQuery("PRAGMA blockchain_status");
      rs.next();
      result = rs.getString(1);
      System.out.println("status: " + result);

      // check if this node is allowed on the blockchain network
      System.out.print("waiting approval... ");
      while (true) {
        rs = statement.executeQuery("PRAGMA db_is_ready");
        rs.next();
        result = rs.getString(1);
        if (!result.equals("1")) break;
        Thread.sleep(1000);
      }
      System.out.println("OK. this node is part of the blockchain network and it is ready");

      // execute some query
      rs = statement.executeQuery("select * from products");
      while (rs.next()) {
        System.out.println("id = " + rs.getInt("id"));
        System.out.println("name = " + rs.getString("name"));
      }

      // keep the app open
      while (true) { Thread.sleep(1000); }
    }
    catch (SQLException e) {
      System.err.println(e.getMessage());
    }
    catch (Exception e) {
      System.out.println(e);
    }
    finally {
      try {
        if (connection != null) connection.close();
      }
      catch (SQLException e) {
        // connection close failed.
        System.err.println(e);
      }
    }
  }
}

Signing a transaction from the blockchain admin:

      Function.create(connection, "sign_transaction", new Function() {
        protected void xFunc() {
          String txn = value_text(0);
          System.out.println("txn to be signed: " + txn);
          byte[] signature = sign(txn, privkey);
          result(bytesToHex(pubkey) + ":" + bytesToHex(signature));
        }
        private byte[] sign(String data, byte[] pk) {
          return ...;  // it must use the secp256k1 elliptic curve
        }
      });

Receiving a notification of a processed transaction:

      Function.create(connection, "transaction_notification", new Function() {
        protected void xFunc() {
          long nonce = value_long(0);
          String status = value_text(1);
          System.out.println("transaction " + nonce + ": " + status);
          result();
        }
      });

Receiving notification of local db update:

      Function.create(connection, "update_notification", new Function() {
        protected void xFunc() {
          System.out.println("update received");
          result();
        }
      });

Troubleshooting

If your platform or architecture is not supported on the jar you can follow these steps:

1. Install AergoLite on your computer

2. Compile a native AergoLite wrapper

cd aergolite
make amalgamation
cd ..
git clone --depth=1 https://github.com/aergoio/aergolite-jdbc
cd aergolite-jdbc
make native SQLITE_SOURCE="/path/to/aergolite/amalgamation"
pwd
cd ..

Take note of the above printed path. It will be used in the code bellow.

3. Add these 2 lines on your java application

Put these lines at the beginning of the application. They must be executed before the database connection is open.

Replace /path/to/libsqlitejdbc by the path you took note above.

      // set the path to and name of the generated native driver
      System.setProperty("org.sqlite.lib.path", "/path/to/libsqlitejdbc");
      System.setProperty("org.sqlite.lib.name", "libsqlitejdbc.jnilib");